forked from matschundbrei/cloudns-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudns.go
296 lines (281 loc) · 6.67 KB
/
cloudns.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Package cloudns public structs/functions
package cloudns
import (
"encoding/json"
"errors"
"strconv"
"github.com/tidwall/gjson"
)
// Apiaccess ClouDNS API Credentials, see https://www.cloudns.net/wiki/article/42/
type Apiaccess struct {
Authid int `json:"auth-id,omitempty"`
Subauthid int `json:"sub-auth-id,omitempty"`
Authpassword string `json:"auth-password"`
}
// Zone is the external representation of a zone
// check the ...zone types in api.go for details
type Zone struct {
Domain string `json:"domain-name"`
Ztype string `json:"zone-type"`
Ns []string `json:"ns,omitempty"`
}
// Listzones returns all zones (max: 100)
func (a Apiaccess) Listzones() ([]Zone, error) {
zls := zonelist{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Page: 1,
Hits: 100,
}
resp, err := zls.lszone()
var rz []Zone
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return rz, errors.New(errmsg)
}
var intrz []retzone
err2 := json.Unmarshal(resp.Body(), &intrz)
for _, zn := range intrz {
tmpzn := Zone{
Domain: zn.Domain,
Ztype: zn.Ztype,
}
rz = append(rz, tmpzn)
}
return rz, err2
}
return rz, err
}
// List returns all records from a zone
func (z Zone) List(a *Apiaccess) ([]Record, error) {
var ra []Record
rls := reclist{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Domain: z.Domain,
}
resp, err := rls.lsrec()
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return ra, errors.New(errmsg)
}
var ratmp map[string]retrec
err2 := json.Unmarshal(resp.Body(), &ratmp)
for _, rec := range ratmp {
tmpttl, _ := strconv.Atoi(rec.TTL)
tmppriority, _ := strconv.Atoi(rec.Priority)
rectmp := Record{
Domain: z.Domain,
ID: rec.ID,
Rtype: rec.Rtype,
Host: rec.Host,
TTL: tmpttl,
Record: rec.Record,
Priority: tmppriority,
}
ra = append(ra, rectmp)
}
return ra, err2
}
return ra, err
}
// Create a new zone
func (z Zone) Create(a *Apiaccess) (Zone, error) {
cr := createzone{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Domain: z.Domain,
Ztype: z.Ztype,
Ns: z.Ns,
}
resp, err := cr.create()
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return z, errors.New(errmsg)
}
}
return z, err
}
// Read a zone
func (z Zone) Read(a *Apiaccess) (Zone, error) {
cr := createzone{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Domain: z.Domain,
Ztype: z.Ztype,
Ns: z.Ns,
}
resp, err := cr.read()
var zlint []retzone
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return z, errors.New(errmsg)
}
junerr := json.Unmarshal(resp.Body(), &zlint)
if junerr == nil {
var rz = Zone{
Domain: zlint[0].Domain,
Ztype: zlint[0].Ztype,
Ns: z.Ns,
}
return rz, junerr
}
}
return z, err
}
// Update a zone [dummy]
func (z Zone) Update(a *Apiaccess) (Zone, error) {
err := errors.New("Zone updates are currently not implemented, see https://github.com/sta-travel/cloudns-go/limitations.md")
return z, err
}
// Destroy a zone
func (z Zone) Destroy(a *Apiaccess) (Zone, error) {
cr := createzone{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Domain: z.Domain,
Ztype: z.Ztype,
Ns: z.Ns,
}
resp, err := cr.destroy()
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return z, errors.New(errmsg)
}
}
return z, err
}
// Record is the external representation of a record
// check the ...record types in api.go for details
type Record struct {
ID string `json:"id"`
Domain string `json:"domain-name"`
Host string `json:"host"`
Rtype string `json:"record-type"`
TTL int `json:"ttl"`
Record string `json:"record"`
Priority int `json:"priority,omitempty"`
}
// Create a new record
func (r Record) Create(a *Apiaccess) (Record, error) {
inr := createrec{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Domain: r.Domain,
Host: r.Host,
Rtype: r.Rtype,
TTL: r.TTL,
Record: r.Record,
}
if r.Rtype == "MX" || r.Rtype == "SRV" {
inr.Priority = &r.Priority
}
resp, err := inr.create()
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return r, errors.New(errmsg)
}
newid := gjson.GetBytes(resp.Body(), "data.id")
r.ID = newid.String()
}
return r, err
}
// Read a record
func (r Record) Read(a *Apiaccess) (Record, error) {
lsr := reclist{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Domain: r.Domain,
Host: r.Host,
Rtype: r.Rtype,
}
resp, err := lsr.lsrec()
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return r, errors.New(errmsg)
}
var ratmp map[string]retrec
err2 := json.Unmarshal(resp.Body(), &ratmp)
for _, rec := range ratmp {
tmpttl, _ := strconv.Atoi(rec.TTL)
tmppriority, _ := strconv.Atoi(rec.Priority)
rectmp := Record{
Domain: r.Domain,
ID: rec.ID,
Rtype: rec.Rtype,
Host: rec.Host,
TTL: tmpttl,
Record: rec.Record,
Priority: tmppriority,
}
if r.ID != "" && r.ID == rectmp.ID {
return rectmp, err2
}
// if we do not have an ID match, we return the last one ...
return rectmp, err2
}
return r, err2
}
return r, err
}
// Update a record
func (r Record) Update(a *Apiaccess) (Record, error) {
tmpid, _ := strconv.Atoi(r.ID)
inr := updaterec{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Rid: tmpid,
Domain: r.Domain,
Host: r.Host,
TTL: r.TTL,
Record: r.Record,
}
if r.Rtype == "MX" || r.Rtype == "SRV" {
inr.Priority = &r.Priority
}
resp, err := inr.update()
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return r, errors.New(errmsg)
}
}
return r, err
}
// Destroy a record
func (r Record) Destroy(a *Apiaccess) (Record, error) {
tmpid, _ := strconv.Atoi(r.ID)
inr := updaterec{
Authid: a.Authid,
Subauthid: a.Subauthid,
Authpassword: a.Authpassword,
Rid: tmpid,
Domain: r.Domain,
Host: r.Host,
TTL: r.TTL,
Record: r.Record,
}
resp, err := inr.destroy()
if err == nil {
errmsg, isapierr := checkapierr(resp.Body())
if isapierr {
return r, errors.New(errmsg)
}
}
return r, err
}