-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
166 lines (135 loc) · 4.42 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
// Package leaseweb implements a DNS record management client compatible
// with the libdns interfaces for Leaseweb.
// Upstream documentation found at:
// https://developer.leaseweb.com/api-docs/domains_v2.html
package leaseweb
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"github.com/libdns/libdns"
)
const (
LeasewebApiKeyHeader = "X-LSW-Auth"
)
// Provider facilitates DNS record manipulation with Leaseweb.
type Provider struct {
// Leasewebs API key. Generate one in the Leaseweb customer portal -> Administration -> API Key
APIKey string `json:"api_token,omitempty"`
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()
domainName := strings.TrimSuffix(zone, ".")
recordSets, err := p.listRecordSets(domainName)
if err != nil {
return nil, err
}
records := fromLeaseweb(recordSets)
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) {
p.mutex.Lock()
defer p.mutex.Unlock()
recordSets, err := fromLibdns(zone, records)
if err != nil {
return nil, err
}
for _, recordSet := range recordSets {
_, err := p.createRecordSet(zone, recordSet)
if err != nil {
return nil, err
}
}
// TODO: Ideally should check which records are actually POSTed.
// For now we can assume all if we reach this point with no errors.
var addedRecords = records
return addedRecords, 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) {
p.mutex.Lock()
defer p.mutex.Unlock()
domainName := strings.TrimSuffix(zone, ".")
existingRecordSets, err := p.listRecordSets(domainName)
if err != nil {
return nil, err
}
recordSets, err := fromLibdns(zone, records)
if err != nil {
return nil, err
}
existingRecords := fromLeaseweb(existingRecordSets)
var updatedRecords []libdns.Record
for _, recordSet := range recordSets {
var hasExisting = false
for _, existingRecord := range existingRecords {
if existingRecord.Name == recordSet.Name && existingRecord.Type == recordSet.Type {
hasExisting = true
}
}
if hasExisting {
updatedRecordResponse, err := p.updateRecordSet(zone, recordSet)
if err != nil {
return nil, err
}
for _, updatedRecord := range fromLeaseweb(updatedRecordResponse) {
updatedRecords = append(updatedRecords, updatedRecord)
}
} else {
_, err := p.createRecordSet(zone, recordSet)
if err != nil {
return nil, err
}
}
}
return updatedRecords, nil
}
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
// Leaseweb specifics:
// - Well-formatted DELETE requests will always succeed, even for non-existing records.
// TODO: Extract HTTP logic into function in client.go.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
client := &http.Client{}
var domainName = strings.TrimSuffix(zone, ".")
recordSets, err := fromLibdns(zone, records)
for _, recordSet := range recordSets {
if err != nil {
return nil, err
}
// https://developer.leaseweb.com/api-docs/domains_v2.html#operation/delete/domains/{domainName}/resourceRecordSets/{name}/{type}
req, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("https://api.leaseweb.com/hosting/v2/domains/%s/resourceRecordSets/%s/%s", domainName, recordSet.Name, recordSet.Type), nil)
if err != nil {
return nil, err
}
req.Header.Add(LeasewebApiKeyHeader, p.APIKey)
res, err := client.Do(req)
defer res.Body.Close()
if err != nil {
return nil, err
}
err = handleLeasewebHttpError(res, "delete")
if err != nil {
return nil, err
}
}
// TODO: Ideally should check which records are actually POSTed.
// For now we can assume all if we reach this point with no errors.
var deletedRecords = records
return deletedRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)