-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
390 lines (320 loc) · 10 KB
/
client.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package dnsmadeeasy
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
"github.com/go-resty/resty/v2"
)
const (
DNSManagedPath string = "/dns/managed/"
DNSRecordsPath string = "{domainId}/records"
DNSRecordPath string = "{domainId}/records/{recordId}"
)
type BaseURL string
const (
Sandbox BaseURL = "https://api.sandbox.dnsmadeeasy.com/V2.0/"
Prod BaseURL = "https://api.dnsmadeeasy.com/V2.0/"
)
type Client struct {
APIKey string
SecretKey string
BaseURL BaseURL
resty *resty.Client
zoneIdCache map[string]int
}
// Construct a client using the supplied values
func GetClient(APIKey string, SecretKey string, url BaseURL) *Client {
r := resty.New().SetBaseURL(string(url))
return &Client{APIKey, SecretKey, url, r, nil}
}
// Convenience function to determine the error status of a response
// from DNS Made Easy
func checkRespForError(resp *resty.Response, err error) (*resty.Response, error) {
// first bubble up any error passed to us
if err != nil {
return resp, err
}
var data map[string]interface{}
// next check for json-formatted errors in the response body
err = json.Unmarshal(resp.Body(), &data)
// no error indicates that we were able to de-serialize some json
if err == nil {
if data["error"] != nil {
// translate the array of strings that is DME's error json element
// ie { "error": [ "", "" ] }
resp_errors := data["error"].([]interface{})
if len(resp_errors) > 0 {
var error string
if len(resp_errors) == 1 {
error = resp_errors[0].(string)
} else {
for idx, err := range resp_errors {
error += fmt.Sprintf("%d: %s\n", idx, err.(string))
}
}
return resp, errors.New(error)
}
}
}
// lastly, check for an HTTP error code
status := resp.StatusCode()
if status < 200 || status >= 300 {
return resp, fmt.Errorf("request returned http error code %d", status)
}
// if we got here, there are no errors
return resp, nil
}
// Convenience function to calculate the authentication headers
// expected by DNS Made Easy
func (c *Client) addAuthHeaders(req *resty.Request) {
requestDate := time.Now().UTC().Format(http.TimeFormat)
// Calculate the hexadecimal HMAC SHA1 of requestDate using APIKey
key := []byte(c.SecretKey)
h := hmac.New(sha1.New, key)
h.Write([]byte(requestDate))
hmacString := hex.EncodeToString(h.Sum(nil))
req.Header.Add("X-Dnsme-Apikey", c.APIKey)
req.Header.Add("X-Dnsme-Requestdate", requestDate)
req.Header.Add("X-Dnsme-Hmac", hmacString)
}
// Convenience function to construct a request with common headers
func (c *Client) newRequest() *resty.Request {
req := c.resty.R().ExpectContentType("application/json").
SetHeader("Content-Type", "application/json")
c.addAuthHeaders(req)
return req
}
type Domain struct {
ID int `json:"id"`
Name string `json:"name"`
CreatedAt int `json:"created"`
UpdatedAt int `json:"updated"`
FolderID int `json:"folderId"`
ProcessMulti bool `json:"processMulti"`
ActiveThirdParties []string `json:"activeThirdParties"`
GtdEnabled bool `json:"gtdEnabled"`
// Identifies which action is currently pending
// Values:
// 0 - nothing pending
// 1 - pending creation (usable but not fully created throughout the system)
// 3 - pending deletion
PendingActionID int `json:"pendingActionId"`
}
type DomainsResp struct {
TotalRecords int `json:"totalRecords"`
TotalPages int `json:"totalPages"`
Domains []Domain `json:"data"`
CurrentPage int `json:"page"`
}
// Creates a new domain
func (c *Client) CreateDomain(domainName string) (Domain, error) {
var newDomain Domain
createDomainBody := fmt.Sprintf(`{"name":"%s"}`, domainName)
req := c.newRequest().
SetResult(&newDomain).
SetBody(createDomainBody)
_, err := checkRespForError(req.Post(DNSManagedPath))
if err != nil {
return Domain{}, err
}
return newDomain, nil
}
// Removes a domain and all associated records
func (c *Client) DeleteDomain(domainID int) error {
_, err := checkRespForError(c.newRequest().
Delete(fmt.Sprint(DNSManagedPath, domainID)))
return err
}
// Returns the domain record for a given domain ID
func (c *Client) GetDomain(domainID int) (Domain, error) {
var domain Domain
_, err := checkRespForError(c.newRequest().
SetResult(&domain).
Get(DNSManagedPath + fmt.Sprint(domainID)))
if err != nil {
return Domain{}, err
}
return domain, nil
}
// Returns a map of Name:ID for all domains managed by the
// given account
func (c *Client) EnumerateDomains() (map[string]int, error) {
domains := map[string]int{}
var respDomains DomainsResp
_, err := checkRespForError(c.newRequest().
SetResult(&respDomains).
Get(DNSManagedPath))
if err != nil {
return nil, err
}
for _, domain := range respDomains.Domains {
domains[domain.Name] = domain.ID
}
return domains, nil
}
// Finds the numerical ID for a given domain name
func (c *Client) IdForDomain(domain string) (int, error) {
justPopulated := false
if c.zoneIdCache == nil {
domainMap, err := c.EnumerateDomains()
if err != nil {
return 0, err
}
c.zoneIdCache = domainMap
justPopulated = true
}
zoneId, ok := c.zoneIdCache[domain]
if ok {
return zoneId, nil
} else {
// if we didn't just populate the cache, refresh it in case
// our domain exists now
if !justPopulated {
domainMap, err := c.EnumerateDomains()
if err != nil {
return 0, err
}
c.zoneIdCache = domainMap
justPopulated = true
}
zoneId, ok := c.zoneIdCache[domain]
if ok {
return zoneId, nil
}
}
return 0, errors.New("Domain not found")
}
type Record struct {
// A unique name per record Type
Name string `json:"name"`
// A unique identifier for this record
ID int `json:"id,omitempty"`
// Can be one of: A, AAAA, ANAME, CNAME, HTTPRED, MX
// NS, PTR, SRV, TXT, SPF, or SOA
Type string `json:"type"`
// Differs per record type
Value string `json:"value"`
// 1 if the record is the record is domain specific
// 0 if the record is part of a template
Source int `json:"source,omitempty"`
// The time to live of the record
Ttl int `json:"ttl"`
// Global Traffic Director location.
// Values: DEFAULT, US_EAST, US_WEST, EUROPE,
// ASIA_PAC, OCREANIA, SOUTH_AMERICA
GtdLocation string `json:"gtdLocation"`
// The domain ID of this record
SourceId int `json:"sourceId,omitempty"`
// Indicates if DNS Failover is enabled for an A record
Failover bool `json:"failover,omitempty"`
// Indicates if System Monitoring is enabled for an A record
Monitor bool `json:"monitor,omitempty"`
// For HTTP Redirection Records
HardLink bool `json:"hardLink,omitempty"`
// Indicates if the record has dynamic DNS enabled
DynamicDns bool `json:"dynamicDns,omitempty"`
// Indicates if an A record is in failed status
Failed bool `json:"failed,omitempty"`
// The priority for an MX record
MxLevel int `json:"mxLevel,omitempty"`
// The priority for an SRV record
Priority int `json:"priority,omitempty"`
// The weight for an SRV record
Weight int `json:"weight,omitempty"`
// The port for an SRV record
Port int `json:"port,omitempty"`
}
type RecordsResp struct {
TotalRecords int `json:"totalRecords"`
TotalPages int `json:"totalPages"`
Records []Record `json:"data"`
CurrentPage int `json:"page"`
}
func (c *Client) EnumerateRecords(domainId int) ([]Record, error) {
var respRecords RecordsResp
req := c.newRequest().
SetResult(&respRecords).
SetPathParam("domainId", fmt.Sprint(domainId))
_, err := checkRespForError(req.Get(DNSManagedPath + DNSRecordsPath))
if err != nil {
return nil, err
}
return respRecords.Records, nil
}
// Deletes all records for the supplied domain
func (c *Client) DeleteAllRecords(domainID int) error {
_, err := checkRespForError(c.newRequest().
SetPathParam("domainId", fmt.Sprint(domainID)).
Delete(DNSManagedPath + DNSRecordsPath))
return err
}
// Deletes records with numerical IDs for the supplied domain
//
// NOTE: will silently continue if a recordId that doesn't belong to the
// given domainId is passed
func (c *Client) DeleteRecords(domainId int, recordIds []int) ([]int, error) {
var queryString string
// build query string of ids=X&ids=Y&ids=Z
// we can't use other convenience methods since they use
// map[string] and only the last id would made it
for idx, id := range recordIds {
if idx > 0 {
queryString += "&"
}
queryString += fmt.Sprintf("ids=%d", id)
}
req := c.newRequest().
SetPathParam("domainId", fmt.Sprint(domainId)).
SetPathParam("recordId", "").
SetQueryString(queryString)
_, err := checkRespForError(req.Delete(DNSManagedPath + DNSRecordPath))
if err != nil {
return nil, err
}
return recordIds, nil
}
// Creates a single record in the supplied domain
func (c *Client) CreateRecord(domainId int, record Record) (Record, error) {
var newRecord Record
req := c.newRequest().
SetResult(&newRecord).
SetBody(&record).
SetPathParam("domainId", fmt.Sprint(domainId))
_, err := checkRespForError(req.Post(DNSManagedPath + DNSRecordsPath))
if err != nil {
return Record{}, err
}
return newRecord, nil
}
// Create many records at once in the supplied domain
//
// NOTE: is transactional; an error in creating any record causes none to be created
func (c *Client) CreateRecords(domainId int, records []Record) ([]Record, error) {
var newRecords []Record
req := c.newRequest().
SetResult(&newRecords).
SetBody(&records).
SetPathParam("domainId", fmt.Sprint(domainId))
_, err := checkRespForError(req.Post(DNSManagedPath + DNSRecordsPath + "/createMulti"))
if err != nil {
return []Record{}, err
}
return newRecords, nil
}
func (c *Client) UpdateRecords(domainId int, records []Record) ([]Record, error) {
var updatedRecords []Record
req := c.newRequest().
SetResult(&updatedRecords).
SetBody(&records).
SetPathParam("domainId", fmt.Sprint(domainId))
_, err := checkRespForError(req.Post(DNSManagedPath + DNSRecordsPath + "/updateMulti"))
if err != nil {
return []Record{}, err
}
return updatedRecords, nil
}