forked from kubernetes-sigs/external-dns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsimple.go
329 lines (288 loc) · 10.2 KB
/
dnsimple.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/dnsimple/dnsimple-go/dnsimple"
"github.com/kubernetes-incubator/external-dns/endpoint"
"github.com/kubernetes-incubator/external-dns/plan"
log "github.com/sirupsen/logrus"
)
const dnsimpleRecordTTL = 3600 // Default TTL of 1 hour if not set (DNSimple's default)
type identityService struct {
service *dnsimple.IdentityService
}
func (i identityService) Whoami() (*dnsimple.WhoamiResponse, error) {
return i.service.Whoami()
}
// Returns the account ID given dnsimple credentials
func (p *dnsimpleProvider) GetAccountID(credentials dnsimple.Credentials, client dnsimple.Client) (accountID string, err error) {
// get DNSimple client accountID
whoamiResponse, err := client.Identity.Whoami()
if err != nil {
return "", err
}
return strconv.Itoa(whoamiResponse.Data.Account.ID), nil
}
// dnsimpleZoneServiceInterface is an interface that contains all necessary zone services from dnsimple
type dnsimpleZoneServiceInterface interface {
ListZones(accountID string, options *dnsimple.ZoneListOptions) (*dnsimple.ZonesResponse, error)
ListRecords(accountID string, zoneID string, options *dnsimple.ZoneRecordListOptions) (*dnsimple.ZoneRecordsResponse, error)
CreateRecord(accountID string, zoneID string, recordAttributes dnsimple.ZoneRecord) (*dnsimple.ZoneRecordResponse, error)
DeleteRecord(accountID string, zoneID string, recordID int) (*dnsimple.ZoneRecordResponse, error)
UpdateRecord(accountID string, zoneID string, recordID int, recordAttributes dnsimple.ZoneRecord) (*dnsimple.ZoneRecordResponse, error)
}
type dnsimpleZoneService struct {
service *dnsimple.ZonesService
}
func (z dnsimpleZoneService) ListZones(accountID string, options *dnsimple.ZoneListOptions) (*dnsimple.ZonesResponse, error) {
return z.service.ListZones(accountID, options)
}
func (z dnsimpleZoneService) ListRecords(accountID string, zoneID string, options *dnsimple.ZoneRecordListOptions) (*dnsimple.ZoneRecordsResponse, error) {
return z.service.ListRecords(accountID, zoneID, options)
}
func (z dnsimpleZoneService) CreateRecord(accountID string, zoneID string, recordAttributes dnsimple.ZoneRecord) (*dnsimple.ZoneRecordResponse, error) {
return z.service.CreateRecord(accountID, zoneID, recordAttributes)
}
func (z dnsimpleZoneService) DeleteRecord(accountID string, zoneID string, recordID int) (*dnsimple.ZoneRecordResponse, error) {
return z.service.DeleteRecord(accountID, zoneID, recordID)
}
func (z dnsimpleZoneService) UpdateRecord(accountID string, zoneID string, recordID int, recordAttributes dnsimple.ZoneRecord) (*dnsimple.ZoneRecordResponse, error) {
return z.service.UpdateRecord(accountID, zoneID, recordID, recordAttributes)
}
type dnsimpleProvider struct {
client dnsimpleZoneServiceInterface
identity identityService
accountID string
domainFilter DomainFilter
zoneIDFilter ZoneIDFilter
dryRun bool
}
type dnsimpleChange struct {
Action string
ResourceRecordSet dnsimple.ZoneRecord
}
const (
dnsimpleCreate = "CREATE"
dnsimpleDelete = "DELETE"
dnsimpleUpdate = "UPDATE"
)
// NewDnsimpleProvider initializes a new Dnsimple based provider
func NewDnsimpleProvider(domainFilter DomainFilter, zoneIDFilter ZoneIDFilter, dryRun bool) (Provider, error) {
oauthToken := os.Getenv("DNSIMPLE_OAUTH")
if len(oauthToken) == 0 {
return nil, fmt.Errorf("No dnsimple oauth token provided")
}
client := dnsimple.NewClient(dnsimple.NewOauthTokenCredentials(oauthToken))
provider := &dnsimpleProvider{
client: dnsimpleZoneService{service: client.Zones},
identity: identityService{service: client.Identity},
domainFilter: domainFilter,
zoneIDFilter: zoneIDFilter,
dryRun: dryRun,
}
whoamiResponse, err := provider.identity.service.Whoami()
if err != nil {
return nil, err
}
provider.accountID = strconv.Itoa(whoamiResponse.Data.Account.ID)
return provider, nil
}
// Returns a list of filtered Zones
func (p *dnsimpleProvider) Zones() (map[string]dnsimple.Zone, error) {
zones := make(map[string]dnsimple.Zone)
page := 1
listOptions := &dnsimple.ZoneListOptions{}
for {
listOptions.Page = page
zonesResponse, err := p.client.ListZones(p.accountID, listOptions)
if err != nil {
return nil, err
}
for _, zone := range zonesResponse.Data {
if !p.domainFilter.Match(zone.Name) {
continue
}
if !p.zoneIDFilter.Match(strconv.Itoa(zone.ID)) {
continue
}
zones[strconv.Itoa(zone.ID)] = zone
}
page++
if page > zonesResponse.Pagination.TotalPages {
break
}
}
return zones, nil
}
// Records retuns a list of endpoints in a given zone
func (p *dnsimpleProvider) Records() (endpoints []*endpoint.Endpoint, _ error) {
zones, err := p.Zones()
if err != nil {
return nil, err
}
for _, zone := range zones {
page := 1
listOptions := &dnsimple.ZoneRecordListOptions{}
for {
listOptions.Page = page
records, err := p.client.ListRecords(p.accountID, zone.Name, listOptions)
if err != nil {
return nil, err
}
for _, record := range records.Data {
switch record.Type {
case "A", "CNAME", "TXT":
break
default:
continue
}
endpoints = append(endpoints, endpoint.NewEndpointWithTTL(record.Name+"."+record.ZoneID, record.Content, record.Type, endpoint.TTL(record.TTL)))
}
page++
if page > records.Pagination.TotalPages {
break
}
}
}
return endpoints, nil
}
// newDnsimpleChange initializes a new change to dns records
func newDnsimpleChange(action string, e *endpoint.Endpoint) *dnsimpleChange {
ttl := dnsimpleRecordTTL
if e.RecordTTL.IsConfigured() {
ttl = int(e.RecordTTL)
}
change := &dnsimpleChange{
Action: action,
ResourceRecordSet: dnsimple.ZoneRecord{
Name: e.DNSName,
Type: e.RecordType,
Content: e.Targets[0],
TTL: ttl,
},
}
return change
}
// newDnsimpleChanges returns a slice of changes based on given action and record
func newDnsimpleChanges(action string, endpoints []*endpoint.Endpoint) []*dnsimpleChange {
changes := make([]*dnsimpleChange, 0, len(endpoints))
for _, e := range endpoints {
changes = append(changes, newDnsimpleChange(action, e))
}
return changes
}
// submitChanges takes a zone and a collection of changes and makes all changes from the collection
func (p *dnsimpleProvider) submitChanges(changes []*dnsimpleChange) error {
if len(changes) == 0 {
log.Infof("All records are already up to date")
return nil
}
zones, err := p.Zones()
if err != nil {
return err
}
for _, change := range changes {
zone := dnsimpleSuitableZone(change.ResourceRecordSet.Name, zones)
if zone.ID == 0 {
return fmt.Errorf("No suitable zone name found")
}
log.Infof("Changing records: %s %v in zone: %s", change.Action, change.ResourceRecordSet, zone.Name)
change.ResourceRecordSet.Name = strings.TrimSuffix(change.ResourceRecordSet.Name, "."+zone.Name)
if !p.dryRun {
switch change.Action {
case dnsimpleCreate:
_, err := p.client.CreateRecord(p.accountID, zone.Name, change.ResourceRecordSet)
if err != nil {
return err
}
case dnsimpleDelete:
recordID, err := p.GetRecordID(zone.Name, change.ResourceRecordSet.Name)
if err != nil {
return err
}
_, err = p.client.DeleteRecord(p.accountID, zone.Name, recordID)
if err != nil {
return err
}
case dnsimpleUpdate:
recordID, err := p.GetRecordID(zone.Name, change.ResourceRecordSet.Name)
if err != nil {
return err
}
_, err = p.client.UpdateRecord(p.accountID, zone.Name, recordID, change.ResourceRecordSet)
if err != nil {
return err
}
}
}
}
return nil
}
// Returns the record ID for a given record name and zone
func (p *dnsimpleProvider) GetRecordID(zone string, recordName string) (recordID int, err error) {
page := 1
listOptions := &dnsimple.ZoneRecordListOptions{Name: recordName}
for {
listOptions.Page = page
records, err := p.client.ListRecords(p.accountID, zone, listOptions)
if err != nil {
return 0, err
}
for _, record := range records.Data {
if record.Name == recordName {
return record.ID, nil
}
}
page++
if page > records.Pagination.TotalPages {
break
}
}
return 0, fmt.Errorf("No record id found")
}
// dnsimpleSuitableZone returns the most suitable zone for a given hostname and a set of zones.
func dnsimpleSuitableZone(hostname string, zones map[string]dnsimple.Zone) dnsimple.Zone {
var zone dnsimple.Zone
for _, z := range zones {
if strings.HasSuffix(hostname, z.Name) {
if zone.ID == 0 || len(z.Name) > len(zone.Name) {
zone = z
}
}
}
return zone
}
// CreateRecords creates records for a given slice of endpoints
func (p *dnsimpleProvider) CreateRecords(endpoints []*endpoint.Endpoint) error {
return p.submitChanges(newDnsimpleChanges(dnsimpleCreate, endpoints))
}
// DeleteRecords deletes records for a given slice of endpoints
func (p *dnsimpleProvider) DeleteRecords(endpoints []*endpoint.Endpoint) error {
return p.submitChanges(newDnsimpleChanges(dnsimpleDelete, endpoints))
}
// UpdateRecords updates records for a given slice of endpoints
func (p *dnsimpleProvider) UpdateRecords(endpoints []*endpoint.Endpoint) error {
return p.submitChanges(newDnsimpleChanges(dnsimpleUpdate, endpoints))
}
// ApplyChanges applies a given set of changes
func (p *dnsimpleProvider) ApplyChanges(changes *plan.Changes) error {
combinedChanges := make([]*dnsimpleChange, 0, len(changes.Create)+len(changes.UpdateNew)+len(changes.Delete))
combinedChanges = append(combinedChanges, newDnsimpleChanges(dnsimpleCreate, changes.Create)...)
combinedChanges = append(combinedChanges, newDnsimpleChanges(dnsimpleUpdate, changes.UpdateNew)...)
combinedChanges = append(combinedChanges, newDnsimpleChanges(dnsimpleDelete, changes.Delete)...)
return p.submitChanges(combinedChanges)
}