-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
253 lines (216 loc) · 6.11 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package hosttech
import (
"fmt"
"github.com/libdns/libdns"
"strconv"
"time"
)
// HosttechRecord must be implemented by each different type of record representation from the Hosttech.ch API, to allow a transformation from and to libdns.record.
type HosttechRecord interface {
toLibdnsRecord(zone string) libdns.Record
fromLibdnsRecord(record libdns.Record) HosttechRecord
}
// Base holds all the values that are present in each record
type Base struct {
Id int `json:"id,omitempty"`
Type string `json:"type,omitempty"`
TTL int `json:"ttl,omitempty"`
Comment string `json:"comment,omitempty"`
}
// AAAARecord is an implementation of the AAAA record type
type AAAARecord struct {
Base
Name string `json:"name,omitempty"`
IPV6 string `json:"ipv6,omitempty"`
}
func (a AAAARecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(a.Id),
Type: a.Type,
Name: libdns.RelativeName(a.Name, zone),
Value: a.IPV6,
TTL: time.Duration(a.TTL * 1000000000),
}
}
func (a AAAARecord) fromLibdnsRecord(record libdns.Record) HosttechRecord {
a.Name = record.Name
a.Type = record.Type
a.IPV6 = record.Value
a.TTL = durationToIntSeconds(record.TTL)
a.Comment = generateComment()
return a
}
// ARecord is an implementation of the A record type
type ARecord struct {
Base
Name string `json:"name,omitempty"`
IPV4 string `json:"ipv4,omitempty"`
}
func (a ARecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(a.Id),
Type: a.Type,
Name: libdns.RelativeName(a.Name, zone),
Value: a.IPV4,
TTL: time.Duration(a.TTL * 1000000000),
}
}
func (a ARecord) fromLibdnsRecord(record libdns.Record) HosttechRecord {
a.Name = record.Name
a.Type = record.Type
a.IPV4 = record.Value
a.TTL = durationToIntSeconds(record.TTL)
a.Comment = generateComment()
return a
}
// CNAMERecord is an implementation of the CNAME record type
type CNAMERecord struct {
Base
Name string `json:"name,omitempty"`
Cname string `json:"cname,omitempty"`
}
func (c CNAMERecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(c.Id),
Type: c.Type,
Name: libdns.RelativeName(c.Name, zone),
Value: c.Cname,
TTL: time.Duration(c.TTL * 1000000000),
}
}
func (c CNAMERecord) fromLibdnsRecord(record libdns.Record) HosttechRecord {
c.Name = record.Name
c.Type = record.Type
c.Cname = record.Value
c.TTL = durationToIntSeconds(record.TTL)
c.Comment = generateComment()
return c
}
// MXRecord is an implementation of the MX record type
type MXRecord struct {
Base
Name string `json:"name,omitempty"`
OwnerName string `json:"ownername,omitempty"`
Pref uint `json:"pref,omitempty"`
}
func (m MXRecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(m.Id),
Type: m.Type,
Name: libdns.RelativeName(m.OwnerName, zone),
Value: m.Name,
TTL: time.Duration(m.TTL * 1000000000),
Priority: m.Pref,
}
}
func (m MXRecord) fromLibdnsRecord(record libdns.Record) HosttechRecord {
m.OwnerName = record.Name
m.Type = record.Type
m.TTL = durationToIntSeconds(record.TTL)
m.Name = record.Value
m.Pref = record.Priority
m.Comment = generateComment()
return m
}
// NSRecord is an implementation of the NS record type
type NSRecord struct {
Base
OwnerName string `json:"ownername,omitempty"`
TargetName string `json:"targetname,omitempty"`
}
func (n NSRecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(n.Id),
Type: n.Type,
Name: libdns.RelativeName(n.OwnerName, zone),
Value: n.TargetName,
TTL: time.Duration(n.TTL * 1000000000),
}
}
func (n NSRecord) fromLibdnsRecord(record libdns.Record) HosttechRecord {
n.OwnerName = record.Name
n.Type = record.Type
n.TargetName = record.Value
n.TTL = durationToIntSeconds(record.TTL)
n.Comment = generateComment()
return n
}
// TXTRecord is an implementation of the TXT record type
type TXTRecord struct {
Base
Name string `json:"name,omitempty"`
Text string `json:"text,omitempty"`
}
func (t TXTRecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(t.Id),
Type: t.Type,
Name: libdns.RelativeName(t.Name, zone),
Value: t.Text,
TTL: time.Duration(t.TTL * 1000000000),
}
}
func (t TXTRecord) fromLibdnsRecord(record libdns.Record) HosttechRecord {
t.Name = RemoveTrailingDot(record.Name)
t.Type = record.Type
t.Text = record.Value
t.TTL = durationToIntSeconds(record.TTL)
t.Comment = generateComment()
return t
}
// TLSARecord is an implementation of the TLSA record type
type TLSARecord struct {
Base
Name string `json:"name,omitempty"`
Text string `json:"text,omitempty"`
}
// HosttechZone is an implementation of the zone without records
type HosttechZone struct {
Id uint `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
TTL uint32 `json:"ttl,omitempty"`
Nameserver string `json:"nameserver,omitempty"`
DNSSEC bool `json:"dnssec,omitempty"`
DNSSECEmail string `json:"dnssec_email,omitempty"`
}
func (z HosttechZone) toLibdnsZone() libdns.Zone {
return libdns.Zone{
Name: z.Name,
}
}
func (t TLSARecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(t.Id),
Type: t.Type,
Name: libdns.RelativeName(t.Name, zone),
Value: t.Text,
TTL: time.Duration(t.TTL * 1000000000),
}
}
func (t TLSARecord) fromLibdnsRecord(record libdns.Record) HosttechRecord {
t.Name = record.Name
t.Type = record.Type
t.Text = record.Value
t.TTL = durationToIntSeconds(record.TTL)
t.Comment = generateComment()
return t
}
func durationToIntSeconds(duration time.Duration) int {
durationInSeconds := duration.Seconds()
// The minimum amount is 600 seconds
if durationInSeconds < 600 {
return 600
}
return int(durationInSeconds)
}
func generateComment() string {
return fmt.Sprintf("This record was created or updated with libdns at %s UTC", time.Now().UTC().Format(time.Stamp))
}
type ApiError struct {
s string
ErrorCode int
}
func (a ApiError) Error() string {
return a.s
}