-
Notifications
You must be signed in to change notification settings - Fork 0
/
records.go
192 lines (179 loc) · 4.31 KB
/
records.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
package godnsmadeeasy
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type RecordsList struct {
TotalRecords int `json:"totalRecords"`
TotalPages int `json:"totalPages"`
Data []Record `json:"data"`
Page int `json:"page"`
}
type Record struct {
Source int `json:"source"`
Ttl int `json:"ttl"`
GtdLocation string `json:"gtdLocation"`
SourceId int `json:"sourceId"`
Failover bool `json:"failover"`
Monitor bool `json:"monitor"`
HardLink bool `json:"hardLink"`
DynamicDns bool `json:"dynamicDns"`
Failed bool `json:"failed"`
Name string `json:"name"`
Value string `json:"value"`
Id int `json:"id"`
Type string `json:"type"`
}
type NewRecord struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
GtdLocation string `json:"gtdLocation"`
Ttl int `json:"ttl"`
}
type UpdateRecord struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
Id int `json:"id"`
GtdLocation string `json:"gtdLocation"`
Ttl int `json:"ttl"`
}
type ErrRecordExists struct {
Type string
Name string
Value string
Err error
}
func (e *ErrRecordExists) Error() string {
return fmt.Sprintf("record [type: %v, name: %v, value: %v] already exists in dme", e.Type, e.Name, e.Value)
}
type ErrDomainIdOrRecordIdNotFound struct {
DomainId int
RecordId int
Err error
}
func (e *ErrDomainIdOrRecordIdNotFound) Error() string {
return fmt.Sprintf("domain id %v or record id %v not found in dme", e.DomainId, e.RecordId)
}
// get all records
func (client *Client) GetAllRecords(domainId int) (RecordsList, error) {
request := Request{
http.MethodGet,
fmt.Sprintf("/dns/managed/%v/records", domainId),
map[string]string{},
[]byte(""),
}
var records RecordsList
status, body, err := client.apiRequest(request)
if err != nil {
return records, &ErrApiRequest{Err: err}
}
if status == 404 {
return records, &ErrDomainIdNotFound{Id: domainId}
}
_ = json.Unmarshal(body, &records)
return records, err
}
// add record
func (client *Client) AddRecord(domainId int,
recordName string,
recordType string,
recordValue string,
recordGtdLocation string,
recordTtl int) (Record, error) {
reqBody := NewRecord{
recordName,
recordType,
recordValue,
recordGtdLocation,
recordTtl,
}
reqBodyBytes, _ := json.Marshal(reqBody)
request := Request{
http.MethodPost,
fmt.Sprintf("/dns/managed/%v/records", domainId),
map[string]string{},
reqBodyBytes,
}
var record Record
status, body, err := client.apiRequest(request)
if err != nil {
return record, &ErrApiRequest{Err: err}
}
if status == 400 {
var eBody errBody
_ = json.Unmarshal(body, &eBody)
if strings.Contains(eBody.Error[0], "already exists") {
return record, &ErrRecordExists{
Type: recordType,
Name: recordName,
Value: recordValue,
}
} else {
return record, &ErrFormat{Err: fmt.Errorf(eBody.Error[0])}
}
}
_ = json.Unmarshal(body, &record)
return record, err
}
// update record
func (client *Client) UpdateRecord(domainId int,
recordName string,
recordType string,
recordValue string,
recordId int,
recordGtdLocation string,
recordTtl int) error {
reqBody := UpdateRecord{
recordName,
recordType,
recordValue,
recordId,
recordGtdLocation,
recordTtl,
}
reqBodyBytes, _ := json.Marshal(reqBody)
request := Request{
http.MethodPut,
fmt.Sprintf("/dns/managed/%v/records/%v", domainId, recordId),
map[string]string{},
reqBodyBytes,
}
status, _, err := client.apiRequest(request)
if err != nil {
return &ErrApiRequest{Err: err}
}
if status == 404 {
return &ErrDomainIdOrRecordIdNotFound{
DomainId: domainId,
RecordId: recordId,
}
}
if status == 400 {
return &ErrFormat{Err: err}
}
return err
}
// delete record
func (client *Client) DeleteRecord(domainId int, recordId int) error {
request := Request{
http.MethodDelete,
fmt.Sprintf("/dns/managed/%v/records/%v", domainId, recordId),
map[string]string{},
[]byte(""),
}
status, _, err := client.apiRequest(request)
if err != nil {
return &ErrApiRequest{Err: err}
}
if status == 404 {
return &ErrDomainIdOrRecordIdNotFound{
DomainId: domainId,
RecordId: recordId,
}
}
return err
}