| 1 |
package dnsmadeeasy |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"github.com/imdario/mergo" |
| 8 |
"strconv" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
type DataResponse struct { |
| 14 |
Data []Record `json:"data"` |
| 15 |
} |
| 16 |
|
| 17 |
|
| 18 |
type Record struct { |
| 19 |
Name string `json:"name"` |
| 20 |
Value string `json:"value"` |
| 21 |
RecordID int64 `json:"id"` |
| 22 |
Type string `json:"type"` |
| 23 |
Source int64 `json:"source"` |
| 24 |
SourceID int64 `json:"sourceId"` |
| 25 |
DynamicDNS bool `json:"dynamicDns"` |
| 26 |
Password string `json:"password"` |
| 27 |
TTL int64 `json:"ttl"` |
| 28 |
Monitor bool `json:"monitor"` |
| 29 |
Failover bool `json:"failover"` |
| 30 |
Failed bool `json:"failed"` |
| 31 |
GtdLocation string `json:"gtdLocation"` |
| 32 |
Description string `json:"description"` |
| 33 |
Keywords string `json:"keywords"` |
| 34 |
Title string `json:"title"` |
| 35 |
HardLink bool `json:"hardLink"` |
| 36 |
MXLevel int64 `json:"mxLevel"` |
| 37 |
Weight int64 `json:"weight"` |
| 38 |
Priority int64 `json:"priority"` |
| 39 |
Port int64 `json:"port"` |
| 40 |
RedirectType string `json:"redirectType"` |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func (r *Record) StringRecordID() string { |
| 45 |
return strconv.FormatInt(r.RecordID, 10) |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
type requestType int |
| 51 |
|
| 52 |
const ( |
| 53 |
create requestType = iota |
| 54 |
retrieve |
| 55 |
update |
| 56 |
destroy |
| 57 |
) |
| 58 |
|
| 59 |
func (rt requestType) endpoint(domainID string, recordID string) (result string) { |
| 60 |
switch rt { |
| 61 |
case create, retrieve: |
| 62 |
result = fmt.Sprintf("/dns/managed/%s/records/", domainID) |
| 63 |
case update, destroy: |
| 64 |
result = fmt.Sprintf("/dns/managed/%s/records/%s/", domainID, recordID) |
| 65 |
} |
| 66 |
return result |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func (c *Client) CreateRecord(domainID string, cr map[string]interface{}) (string, error) { |
| 73 |
|
| 74 |
path := create.endpoint(domainID, "") |
| 75 |
buf := bytes.NewBuffer(nil) |
| 76 |
enc := json.NewEncoder(buf) |
| 77 |
if err := enc.Encode(cr); err != nil { |
| 78 |
return "", err |
| 79 |
} |
| 80 |
|
| 81 |
req, err := c.NewRequest("POST", path, buf, "") |
| 82 |
if err != nil { |
| 83 |
return "", fmt.Errorf("Error from NewRequest: %s", err) |
| 84 |
} |
| 85 |
|
| 86 |
resp, err := checkResp(c.HTTP.Do(req)) |
| 87 |
if err != nil { |
| 88 |
return "", fmt.Errorf("Error creating record: %s", err) |
| 89 |
} |
| 90 |
|
| 91 |
record := new(Record) |
| 92 |
|
| 93 |
err = decodeBody(resp, &record) |
| 94 |
if err != nil { |
| 95 |
return "", fmt.Errorf("Error parsing record response: %s", err) |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
return record.StringRecordID(), nil |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
func (c *Client) ReadRecord(domainID string, recordID string) (*Record, error) { |
| 105 |
body := bytes.NewBuffer(nil) |
| 106 |
path := retrieve.endpoint(domainID, recordID) |
| 107 |
req, err := c.NewRequest("GET", path, body, "") |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
|
| 112 |
resp, err := checkResp(c.HTTP.Do(req)) |
| 113 |
if err != nil { |
| 114 |
return nil, fmt.Errorf("Error retrieving record: %s", err) |
| 115 |
} |
| 116 |
|
| 117 |
dataResp := DataResponse{} |
| 118 |
err = decodeBody(resp, &dataResp) |
| 119 |
if err != nil { |
| 120 |
return nil, fmt.Errorf("Error decoding data response: %s", err) |
| 121 |
} |
| 122 |
var result Record |
| 123 |
var found bool |
| 124 |
for _, record := range dataResp.Data { |
| 125 |
if record.StringRecordID() == recordID { |
| 126 |
result = record |
| 127 |
found = true |
| 128 |
break |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if !found { |
| 133 |
return nil, fmt.Errorf("Unable to find record %s", recordID) |
| 134 |
} |
| 135 |
return &result, nil |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
func (c *Client) UpdateRecord(domainID string, recordID string, cr map[string]interface{}) (string, error) { |
| 141 |
|
| 142 |
current, err := c.ReadRecord(domainID, recordID) |
| 143 |
if err != nil { |
| 144 |
return "", err |
| 145 |
} |
| 146 |
|
| 147 |
err = mergo.Map(current, cr) |
| 148 |
if err != nil { |
| 149 |
return "", err |
| 150 |
} |
| 151 |
|
| 152 |
buf := bytes.NewBuffer(nil) |
| 153 |
enc := json.NewEncoder(buf) |
| 154 |
if err := enc.Encode(current); err != nil { |
| 155 |
return "", err |
| 156 |
} |
| 157 |
|
| 158 |
path := update.endpoint(domainID, recordID) |
| 159 |
req, err := c.NewRequest("PUT", path, buf, "") |
| 160 |
if err != nil { |
| 161 |
return "", err |
| 162 |
} |
| 163 |
|
| 164 |
_, err = checkResp(c.HTTP.Do(req)) |
| 165 |
if err != nil { |
| 166 |
return "", fmt.Errorf("Error updating record: %s", err) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
return recordID, nil |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
func (c *Client) DeleteRecord(domainID string, recordID string) error { |
| 177 |
body := bytes.NewBuffer(nil) |
| 178 |
path := destroy.endpoint(domainID, recordID) |
| 179 |
req, err := c.NewRequest("DELETE", path, body, "") |
| 180 |
if err != nil { |
| 181 |
return err |
| 182 |
} |
| 183 |
|
| 184 |
_, err = checkResp(c.HTTP.Do(req)) |
| 185 |
if err != nil { |
| 186 |
return fmt.Errorf("Unable to find record %s", recordID) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
return nil |
| 191 |
} |