-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
254 lines (206 loc) · 7.4 KB
/
client.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
package ovh
import (
"context"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/libdns/libdns"
"github.com/ovh/go-ovh/ovh"
)
// Client is an abstraction of OvhClient
type Client struct {
ovhClient *ovh.Client
mutex sync.Mutex
}
// Ovh zone record implementation
type OvhDomainZoneRecord struct {
ID int64 `json:"id,omitempty"`
Zone string `json:"zone,omitempty"`
SubDomain string `json:"subDomain"`
FieldType string `json:"fieldType,omitempty"`
Target string `json:"target"`
TTL int64 `json:"ttl"`
}
// Ovh zone soa implementation
type OvhDomainZoneSOA struct {
Server string `json:"server"`
Email string `json:"email"`
Serial int64 `json:"serial"`
Refresh int64 `json:"refresh"`
NxDomainTTL int64 `json:"nxDomainTtl"`
Expire int64 `json:"expire"`
TTL int64 `json:"ttl"`
}
// setupClient invokes authentication and store client to the provider instance.
func (p *Provider) setupClient() error {
if p.client.ovhClient == nil {
client, err := ovh.NewClient(p.Endpoint, p.ApplicationKey, p.ApplicationSecret, p.ConsumerKey)
if err != nil {
return err
}
p.client.ovhClient = client
}
return nil
}
// getRecords gets all records in specified zone on Ovh DNS.
// TTL as 0 for any record correspond to the default TTL for the zone
func (p *Provider) getRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
if err := p.setupClient(); err != nil {
return nil, err
}
var dzSOA OvhDomainZoneSOA
if err := p.client.ovhClient.GetWithContext(ctx, fmt.Sprintf("/domain/zone/%s/soa", zone), &dzSOA); err != nil {
return nil, err
}
var idRecords []int64
if err := p.client.ovhClient.GetWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record", zone), &idRecords); err != nil {
return nil, err
}
var records []libdns.Record
for _, idr := range idRecords {
var dzr OvhDomainZoneRecord
if err := p.client.ovhClient.GetWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record/%d", zone, idr), &dzr); err != nil {
return nil, err
}
if dzr.TTL == 0 {
dzr.TTL = dzSOA.TTL
}
record := libdns.Record{
ID: strconv.FormatInt(dzr.ID, 10),
Type: dzr.FieldType,
Name: dzr.SubDomain,
Value: strings.TrimRight(strings.TrimLeft(dzr.Target, "\""), "\""),
TTL: time.Duration(dzr.TTL) * time.Second,
}
records = append(records, record)
}
return records, nil
}
// createRecord creates a new record in the specified zone.
func (p *Provider) createRecord(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
if err := p.setupClient(); err != nil {
return libdns.Record{}, err
}
var nzr OvhDomainZoneRecord
if err := p.client.ovhClient.PostWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record", zone), &OvhDomainZoneRecord{FieldType: record.Type, SubDomain: normalizeRecordName(record.Name, zone), Target: record.Value, TTL: int64(record.TTL.Seconds())}, &nzr); err != nil {
return libdns.Record{}, err
}
createdRecord := libdns.Record{
ID: strconv.FormatInt(nzr.ID, 10),
Type: nzr.FieldType,
Name: nzr.SubDomain,
Value: strings.TrimRight(strings.TrimLeft(nzr.Target, "\""), "\""),
TTL: time.Duration(nzr.TTL) * time.Second,
}
return createdRecord, nil
}
// createOrUpdateRecord creates or updates a record, either by updating existing record or creating new one.
func (p *Provider) createOrUpdateRecord(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
if len(record.ID) == 0 {
// lookup for existing records
// if we find one, update it
// if we find multiple, delete them and recreate the final one
existingIDs, err := p.lookupRecordIDs(ctx, zone, record.Type, normalizeRecordName(record.Name, zone))
if err != nil {
return libdns.Record{}, err
}
if len(existingIDs) == 1 {
record.ID = strconv.FormatInt(existingIDs[0], 10)
return p.updateRecord(ctx, zone, record)
} else if len(existingIDs) > 1 {
for _, eid := range existingIDs {
if err := p.deleteRecordID(ctx, zone, strconv.FormatInt(eid, 10)); err != nil {
return libdns.Record{}, err
}
}
}
return p.createRecord(ctx, zone, record)
}
return p.updateRecord(ctx, zone, record)
}
func (p *Provider) lookupRecordIDs(ctx context.Context, zone string, recordType string, recordName string) ([]int64, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
if err := p.setupClient(); err != nil {
return nil, err
}
var res []int64
if err := p.client.ovhClient.GetWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record?fieldType=%s&subDomain=%s", zone, recordType, recordName), &res); err != nil {
return nil, err
}
return res, nil
}
func (p *Provider) deleteRecordID(ctx context.Context, zone string, recordID string) error {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
if err := p.setupClient(); err != nil {
return err
}
return p.client.ovhClient.DeleteWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record/%s", zone, recordID), nil)
}
// updateRecord updates a record
func (p *Provider) updateRecord(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
if err := p.setupClient(); err != nil {
return libdns.Record{}, err
}
var nzr OvhDomainZoneRecord
if err := p.client.ovhClient.PutWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record/%s", zone, record.ID), &OvhDomainZoneRecord{SubDomain: record.Name, Target: record.Value, TTL: int64(record.TTL.Seconds())}, &nzr); err != nil {
return libdns.Record{}, err
}
var uzr OvhDomainZoneRecord
if err := p.client.ovhClient.GetWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record/%s", zone, record.ID), &uzr); err != nil {
return libdns.Record{}, err
}
updatedRecord := libdns.Record{
ID: strconv.FormatInt(uzr.ID, 10),
Type: uzr.FieldType,
Name: uzr.SubDomain,
Value: strings.TrimRight(strings.TrimLeft(uzr.Target, "\""), "\""),
TTL: time.Duration(uzr.TTL) * time.Second,
}
return updatedRecord, nil
}
// deleteRecord deletes an existing record.
// Regardless of the value of the record, if the name and type match, the record will be deleted.
func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
if err := p.setupClient(); err != nil {
return libdns.Record{}, err
}
if err := p.client.ovhClient.DeleteWithContext(ctx, fmt.Sprintf("/domain/zone/%s/record/%s", zone, record.ID), nil); err != nil {
return libdns.Record{}, err
}
return record, nil
}
// refresh trigger a reload of the DNS zone.
// It must be called after appending, setting or deleting any record
func (p *Provider) refresh(ctx context.Context, zone string) error {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
if err := p.setupClient(); err != nil {
return err
}
if err := p.client.ovhClient.PostWithContext(ctx, fmt.Sprintf("/domain/zone/%s/refresh", zone), nil, nil); err != nil {
return err
}
return nil
}
// unFQDN trims any trailing "." from fqdn. OVH's API does not use FQDNs.
func unFQDN(fqdn string) string {
return strings.TrimSuffix(fqdn, ".")
}
// normalizeRecordName remove absolute record name
func normalizeRecordName(recordName string, zone string) string {
normalized := unFQDN(recordName)
normalized = strings.TrimSuffix(normalized, unFQDN(zone))
return unFQDN(normalized)
}