forked from libdns/libdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
229 lines (187 loc) · 5.57 KB
/
provider.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
package godaddy
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/libdns/libdns"
)
// Provider godaddy dns provider
type Provider struct {
APIToken string
}
func getDomain(zone string) string {
return strings.TrimSuffix(zone, ".")
}
func getRecordName(zone, name string) string {
return strings.TrimSuffix(strings.TrimSuffix(name, zone), ".")
}
func (p *Provider) getApiHost() string {
return "https://api.godaddy.com"
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
log.Println("GetRecords", zone)
client := http.Client{}
domain := getDomain(zone)
req, err := http.NewRequest("GET", p.getApiHost()+"/v1/domains/"+domain+"/records", nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "sso-key "+p.APIToken)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("could not get records: Domain: %s; Status: %v; Body: %s",
domain, resp.StatusCode, string(bodyBytes))
}
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resultObj := []struct {
Type string `json:"type"`
Name string `json:"name"`
Value string `json:"data"`
TTL int `json:"ttl"`
}{}
err = json.Unmarshal(result, &resultObj)
if err != nil {
return nil, err
}
var records []libdns.Record
for _, record := range resultObj {
records = append(records, libdns.Record{
Type: record.Type,
Name: record.Name,
Value: record.Value,
TTL: time.Duration(record.TTL) * time.Second,
})
}
return records, nil
}
// AppendRecords adds records to the zone. It returns the records that were added.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
log.Println("AppendRecords", zone, records)
var appendedRecords []libdns.Record
for _, record := range records {
client := http.Client{}
type PostRecord struct {
Data string `json:"data"`
}
if record.TTL < time.Duration(600)*time.Second {
record.TTL = time.Duration(600) * time.Second
}
data, err := json.Marshal([]PostRecord{
{
Data: record.Value,
},
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("PUT", p.getApiHost()+"/v1/domains/"+getDomain(zone)+"/records/"+record.Type+"/"+getRecordName(zone, record.Name), bytes.NewBuffer(data))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "sso-key "+p.APIToken)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("could not append records: Domain: %s; Record: %s, Status: %v; Body: %s; PUT: %s",
getDomain(zone), getRecordName(zone, record.Name), resp.StatusCode, string(bodyBytes), data)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
appendedRecords = append(appendedRecords, record)
}
return appendedRecords, nil
}
// SetRecords sets the records in the zone, either by updating existing records
// or creating new ones. It returns the updated records.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
log.Println("SetRecords", zone, records)
return p.AppendRecords(ctx, zone, records)
}
// DeleteRecords deletes the records from the zone.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
log.Println("DeleteRecords", zone, records)
currentRecords, err := p.GetRecords(ctx, zone)
if err != nil {
return nil, err
}
var deletedRecords []libdns.Record
for _, record := range records {
for i, currentRecord := range currentRecords {
if currentRecord.Type == record.Type && currentRecord.Name == getRecordName(zone, record.Name) {
currentRecords = append(currentRecords[:i], currentRecords[i+1:]...)
deletedRecords = append(deletedRecords, currentRecord)
break
}
}
}
type PostRecord struct {
Data string `json:"data"`
Name string `json:"name"`
TTL int `json:"ttl"`
Type string `json:"type"`
}
var data []PostRecord
for _, record := range currentRecords {
data = append(data, PostRecord{
Data: record.Value,
Name: record.Name,
TTL: int(record.TTL / time.Second),
Type: record.Type,
})
}
dataByte, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("PUT", p.getApiHost()+"/v1/domains/"+getDomain(zone)+"/records", bytes.NewBuffer(dataByte))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "sso-key "+p.APIToken)
req.Header.Add("Content-Type", "application/json")
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("could not delete records: Domain: %s; Records: %v, Status: %v; Body: %s",
zone, currentRecords, resp.StatusCode, string(bodyBytes))
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return deletedRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)