-
Notifications
You must be signed in to change notification settings - Fork 2
/
provider.go
220 lines (187 loc) · 6.3 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
package dnsmadeeasy
import (
"context"
"fmt"
"slices"
"strconv"
"strings"
"sync"
dme "github.com/john-k/dnsmadeeasy"
"github.com/libdns/libdns"
)
// Provider facilitates DNS record manipulation with DNSMadeEasy
type Provider struct {
APIKey string `json:"api_key,omitempty"`
SecretKey string `json:"secret_key,omitempty"`
APIEndpoint dme.BaseURL `json:"api_endpoint,omitempty"`
client dme.Client
once sync.Once
mutex sync.Mutex
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
var records []libdns.Record
// first, get the ID for our zone name -- dnsmadeeasy doesn't use the trailing dot
zoneId, err := p.client.IdForDomain(strings.TrimRight(zone, "."))
if err != nil {
return nil, err
}
// get an array of DNSMadeEasy Records for our zone
dmeRecords, err := p.client.EnumerateRecords(zoneId)
if err != nil {
return nil, err
}
// translate each DNSMadeEasy Domain Record to a libdns Record
for _, rec := range dmeRecords {
records = append(records, recordFromDmeRecord(rec))
}
return records, nil
}
func createRecords(client dme.Client, zone string, records []libdns.Record) ([]libdns.Record, error) {
var dmeRecords []dme.Record
// first, get the ID for our zone name -- dnsmadeeasy doesn't use the trailing dot
zoneId, err := client.IdForDomain(strings.TrimRight(zone, "."))
if err != nil {
return nil, err
}
for _, record := range records {
dmeRecord, err := dmeRecordFromRecord(record)
if err != nil {
return []libdns.Record{}, err
}
dmeRecords = append(dmeRecords, dmeRecord)
}
newDmeRecords, err := client.CreateRecords(zoneId, dmeRecords)
if err != nil {
return nil, err
}
var newRecords []libdns.Record
for _, dmeRec := range newDmeRecords {
// The client.CreateRecords call wraps the value in spurious quotes
dmeRec.Value = strings.Trim(dmeRec.Value, "\"")
newRec := recordFromDmeRecord(dmeRec)
newRecords = append(newRecords, newRec)
}
return newRecords, 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) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
return createRecords(p.client, zone, records)
}
// 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) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
// first, get the ID for our zone name -- dnsmadeeasy doesn't use the trailing dot
zoneId, err := p.client.IdForDomain(strings.TrimRight(zone, "."))
if err != nil {
return nil, err
}
// get an array of DNSMadeEasy Records for our zone
dmeRecords, err := p.client.EnumerateRecords(zoneId)
if err != nil {
return nil, err
}
// split our input records into those that need updating and those that need creating.
// if an ID is not provided in the record, try to match based on Type and Name
var existingRecords []libdns.Record
var newRecords []libdns.Record
for _, record := range records {
foundIdx := slices.IndexFunc(dmeRecords, func(dmeRecord dme.Record) bool {
if record.ID != "0" && record.ID != "" {
return fmt.Sprint(dmeRecord.ID) == record.ID
} else {
return record.Type == dmeRecord.Type && record.Name == dmeRecord.Name
}
})
if foundIdx == -1 {
newRecords = append(newRecords, record)
} else {
record.ID = fmt.Sprint(dmeRecords[foundIdx].ID)
existingRecords = append(existingRecords, record)
}
}
var dmeRecordsToUpdate []dme.Record
for _, record := range existingRecords {
newRecord, err := dmeRecordFromRecord(record)
if err != nil {
fmt.Printf("Could not convert %s record for %s: %s", record.Type, record.Name, err)
continue
}
dmeRecordsToUpdate = append(dmeRecordsToUpdate, newRecord)
}
// update existing records
// Note: this is performed first so that we don't leave our request
// in a half-applied state
updatedDmeRecords, err := p.client.UpdateRecords(zoneId, dmeRecordsToUpdate)
if err != nil {
return nil, err
}
// convert the DME Records to libdns records
var updatedRecords []libdns.Record
for _, record := range updatedDmeRecords {
updatedRecords = append(updatedRecords, recordFromDmeRecord(record))
}
// create new records
createdRecords, err := createRecords(p.client, zone, newRecords)
if err != nil {
return nil, err
}
// TODO: hopefully record ordering in the array isn't important
return append(updatedRecords, createdRecords...), nil
}
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.init(ctx)
// first, get the ID for our zone name -- dnsmadeeasy doesn't use the trailing dot
zoneId, err := p.client.IdForDomain(strings.TrimRight(zone, "."))
if err != nil {
return nil, err
}
// convert an array of records into an array of integer
// record ID to pass to the DME library
var recordsToDelete []int
for _, record := range records {
id, err := strconv.Atoi(record.ID)
if err != nil {
fmt.Printf("Could not convert id '%s' to integer", record.ID)
continue
}
recordsToDelete = append(recordsToDelete, id)
}
deletedRecords, err := p.client.DeleteRecords(zoneId, recordsToDelete)
if err != nil {
return nil, err
}
var returnRecords []libdns.Record
for _, id := range deletedRecords {
// find our deleted records ID in the original array argument
recordId := slices.IndexFunc(records, func(rec libdns.Record) bool {
return rec.ID == fmt.Sprint(id)
})
if recordId == -1 {
fmt.Printf("Could not find record id %d in supplied list of libdns.Record\n", id)
continue
}
// add the full record to the array to be returned
returnRecords = append(returnRecords, records[recordId])
}
return returnRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)