-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
188 lines (156 loc) · 4.39 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
package autodns
import (
"context"
"fmt"
"net/http"
"slices"
"github.com/libdns/libdns"
)
const (
autoDNSendpoint string = "https://api.autodns.com/v1"
autoDNScontext string = "4"
)
// Provider facilitates DNS record manipulation with Autodns.
type Provider struct {
Username string `json:"username"`
Password string `json:"password"`
Endpoint string `json:"Endpoint"`
Context string `json:"context"`
Primary string `json:"primary"`
HttpClient *http.Client `json:"-"`
}
// NewWithDefaults is a convenience method to create the provider with sensible defaults.
func NewWithDefaults(username, password string) *Provider {
return &Provider{
Username: username,
Password: password,
Endpoint: autoDNSendpoint,
Context: autoDNScontext,
HttpClient: &http.Client{},
}
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
var records []libdns.Record
zoneInfo, err := p.checkZone(ctx, zone)
if err != nil {
return nil, err
}
result, err := p.getZone(ctx, zoneInfo.Origin, zoneInfo.Nameserver, zone)
if err != nil {
return nil, err
}
for _, r := range result.Data[0].Records {
records = append(records, libdns.Record{
Type: r.Type,
Name: r.Name,
Value: r.Value,
})
}
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) {
zoneInfo, err := p.checkZone(ctx, zone)
if err != nil {
return nil, err
}
result, err := p.getZone(ctx, zoneInfo.Origin, zoneInfo.Nameserver, zone)
if err != nil {
return nil, err
}
for _, r := range records {
result.Data[0].Records = append(result.Data[0].Records, ZoneRecord{
Name: r.Name,
Value: r.Value,
Type: r.Type,
})
}
if err := p.updateZone(ctx, zoneInfo.Origin, zoneInfo.Nameserver, result.Data[0]); err != nil {
return nil, err
}
return records, 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) {
zoneInfo, err := p.checkZone(ctx, zone)
if err != nil {
return nil, err
}
result, err := p.getZone(ctx, zoneInfo.Origin, zoneInfo.Nameserver, zone)
if err != nil {
return nil, err
}
var set []libdns.Record
for _, r := range records {
// find record
idx := slices.IndexFunc(
result.Data[0].Records,
func(zr ZoneRecord) bool {
return zr.Name == r.Name && zr.Type == r.Type
})
if idx == -1 {
result.Data[0].Records = append(result.Data[0].Records,
ZoneRecord{
Name: r.Name,
Type: r.Type,
Value: r.Value,
},
)
set = append(set, r)
continue
}
// update existing record
result.Data[0].Records[idx] = ZoneRecord{
Name: r.Name,
Type: r.Type,
Value: r.Value,
}
set = append(set, r)
}
if err := p.updateZone(ctx, zoneInfo.Origin, zoneInfo.Nameserver, result.Data[0]); err != nil {
return nil, err
}
return set, 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) {
zoneInfo, err := p.checkZone(ctx, zone)
if err != nil {
return nil, err
}
result, err := p.getZone(ctx, zoneInfo.Origin, zoneInfo.Nameserver, zone)
if err != nil {
return nil, err
}
var deleted []libdns.Record
for _, r := range records {
// find record
idx := slices.IndexFunc(
result.Data[0].Records,
func(zr ZoneRecord) bool {
return zr.Name == r.Name && zr.Type == r.Type
})
if idx == -1 {
continue
}
// remove
result.Data[0].Records = append(result.Data[0].Records[:idx], result.Data[0].Records[idx+1:]...)
deleted = append(deleted, r)
}
if err := p.updateZone(ctx, zoneInfo.Origin, zoneInfo.Nameserver, result.Data[0]); err != nil {
if _, ok := err.(*AutoDNSError); ok {
return nil, err
}
return nil, fmt.Errorf("DeleteRecords: %v", err)
}
return deleted, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)