-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
208 lines (182 loc) · 5.63 KB
/
provider.go
File metadata and controls
208 lines (182 loc) · 5.63 KB
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
// Package libdnstemplate implements a DNS record management client compatible
// with the libdns interfaces for Dynv6 REST API.
package libdynv6
import (
"context"
"sync"
"github.com/ZxwyProject/dynv6"
"github.com/libdns/libdns"
)
// TODO: Providers must not require additional provisioning steps by the callers; it
// should work simply by populating a struct and calling methods on it. If your DNS
// service requires long-lived state or some extra provisioning step, do it implicitly
// when methods are called; sync.Once can help with this, and/or you can use a
// sync.(RW)Mutex in your Provider struct to synchronize implicit provisioning.
// Provider facilitates DNS record manipulation with Dynv6 REST API.
type Provider struct {
o sync.Once // for init
Dynv6 *dynv6.Client `json:"-"` // internal client
//# HTTP Token
//
// You can get it at https://dynv6.com/keys
Token string `json:"token,omitempty"`
// TODO: Put config fields here (with snake_case json struct tags on exported fields), for example:
// Exported config fields should be JSON-serializable or omitted (`json:"-"`)
}
func (p *Provider) init() {
// You must ensure that the token is filled in before the first call!
if p.Token == `` {
panic(`libdynv6: No token provided!`)
}
p.Dynv6 = dynv6.NewClient(p.Token)
}
// GetRecords returns all the records in the DNS zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.o.Do(p.init)
z, err := p.Dynv6.ZoneNameCtx(ctx, zone)
if err != nil {
return nil, err
}
r, err := p.Dynv6.RecordsCtx(ctx, string(z.ID))
if err != nil {
return nil, err
}
l := len(r)
o := make([]libdns.Record, l)
for i := 0; i < l; i++ {
o[i] = recordToLibdns(&r[i])
}
// Make sure to return RR-type-specific structs, not libdns.RR structs.
return o, nil
}
// AppendRecords creates the inputted records in the given zone and returns the populated records that were created.
// It never changes existing records.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.o.Do(p.init)
z, err := p.Dynv6.ZoneNameCtx(ctx, zone)
if err != nil {
return nil, err
}
r, err := p.Dynv6.RecordsCtx(ctx, string(z.ID))
if err != nil {
return nil, err
}
l, m, n := len(records), len(r), 0
o := make([]libdns.Record, l)
for i := 0; i < l; i++ {
li := records[i]
lr := li.RR()
if recordFind(r, &lr, m) != nil {
if dynv6.Debug {
dynv6.DbgLog.Println(`[Dynv6-debug/libdns] AppendRecords:`, libdns.AbsoluteName(lr.Name, zone), `already exists!`)
}
continue
}
dr, err := recordFromLibdns(&lr)
if err != nil {
return nil, err
}
_, err = p.Dynv6.RecordAddCtx(ctx, string(z.ID), dr)
if err != nil {
return nil, err
}
o[n] = lr
n++
}
// Make sure to return RR-type-specific structs, not libdns.RR structs.
return o[:n], nil
}
// SetRecords updates the zone so that the records described in the input are reflected in the output.
// It may create or update records or—depending on the record type—delete records to maintain parity with the input.
// No other records are affected. It returns the records which were set.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.o.Do(p.init)
z, err := p.Dynv6.ZoneNameCtx(ctx, zone)
if err != nil {
return nil, err
}
r, err := p.Dynv6.RecordsCtx(ctx, string(z.ID))
if err != nil {
return nil, err
}
l, m := len(records), len(r)
o := make([]libdns.Record, l)
for i := 0; i < l; i++ {
li := records[i]
lr := li.RR()
dr, err := recordFromLibdns(&lr)
if err != nil {
return nil, err
}
fr := recordFind(r, &lr, m)
if fr == nil {
// new
_, err = p.Dynv6.RecordAddCtx(ctx, string(z.ID), dr)
} else {
// upd
_, err = p.Dynv6.RecordUpdCtx(ctx, string(z.ID), string(fr.ID), dr)
}
if err != nil {
return nil, err
}
o[i] = lr
}
// Make sure to return RR-type-specific structs, not libdns.RR structs.
return o, nil
}
// DeleteRecords deletes the given records from the zone if they exist in the zone and exactly match the input.
// If the input records do not exist in the zone, they are silently ignored.
// DeleteRecords returns only the the records that were deleted, and does not return any records that were provided in the input but did not exist in the zone.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.o.Do(p.init)
z, err := p.Dynv6.ZoneNameCtx(ctx, zone)
if err != nil {
return nil, err
}
r, err := p.Dynv6.RecordsCtx(ctx, string(z.ID))
if err != nil {
return nil, err
}
l, m, n := len(records), len(r), 0
o := make([]libdns.Record, l)
for i := 0; i < l; i++ {
li := records[i]
lr := li.RR()
fr := recordFind(r, &lr, m)
if fr == nil {
continue
}
err = p.Dynv6.RecordDelCtx(ctx, string(z.ID), string(fr.ID))
if err != nil {
return nil, err
}
o[n] = lr
n++
}
// Make sure to return RR-type-specific structs, not libdns.RR structs.
return o[:n], nil
}
// ListZones returns the list of available DNS zones for use by other [libdns] methods.
func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error) {
p.o.Do(p.init)
z, err := p.Dynv6.ZonesCtx(ctx)
if err != nil {
return nil, err
}
l := len(z)
o := make([]libdns.Zone, l)
for i := 0; i < l; i++ {
o[i] = libdns.Zone{
Name: z[i].Name,
}
}
return o, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
_ libdns.ZoneLister = (*Provider)(nil)
)