-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
212 lines (175 loc) · 6.02 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
// Package gcore implements the libdns interfaces for GCore DNS
package gcore
import (
"context"
"fmt"
"strings"
"time"
gcoreSDK "github.com/G-Core/gcore-dns-sdk-go"
"github.com/libdns/libdns"
)
// qualityRecordNames takes a libdns.Record and a zone, and returns a new record with a name that is fully qualified
// (i.e. it includes the zone name). If the record name does not end with the zone name and a '.', the zone name and '.'
// are appended to the record name. Otherwise the record name is left unchanged.
func qualityRecordNames(record libdns.Record, zone string) libdns.Record {
return libdns.Record{
Name: libdns.AbsoluteName(record.Name, zone),
Type: record.Type,
TTL: record.TTL,
Value: record.Value,
}
}
// unqualifyRecordNames takes a libdns.Record and a zone, and returns a new record with a name that is unqualified
// (i.e. it does not include the zone name). If the record name ends with the zone name and a '.', it is replaced with
// an empty string. Otherwise the record name is left unchanged.
func unqualifyRecordNames(record libdns.Record, zone string) libdns.Record {
return libdns.Record{
Name: libdns.RelativeName(record.Name, zone),
Type: record.Type,
TTL: record.TTL,
Value: record.Value,
}
}
// Provider facilitates DNS record manipulation with GCore DNS.
type Provider struct {
APIKey string `json:"api_key,omitempty"`
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
cli := gcoreSDK.NewClient(gcoreSDK.PermanentAPIKeyAuth(p.APIKey))
// Get records for zone and convert to libdns records
gcoreZone, err := cli.Zone(ctx, zone)
if err != nil {
return nil, err
}
records := make([]libdns.Record, len(gcoreZone.Records))
for i, gcoreRecord := range gcoreZone.Records {
rrSets, err := cli.RRSet(ctx, zone, gcoreRecord.Name, gcoreRecord.Type)
if err != nil {
return nil, err
}
for _, rrSet := range rrSets.Records {
records[i] = libdns.Record{
Name: gcoreRecord.Name,
Type: gcoreRecord.Type,
TTL: time.Duration(gcoreRecord.TTL) * time.Second,
Value: rrSet.ContentToString(),
}
}
}
for i, record := range records {
records[i] = unqualifyRecordNames(record, zone)
}
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) {
cli := gcoreSDK.NewClient(gcoreSDK.PermanentAPIKeyAuth(p.APIKey))
for i, record := range records {
records[i] = qualityRecordNames(record, zone)
}
recordsByType := make(map[string][]libdns.Record)
for _, record := range records {
recordsByType[record.Type] = append(recordsByType[record.Type], record)
}
var addedRecords []libdns.Record
for recordType, records := range recordsByType {
for _, record := range records {
rrSet, err := cli.RRSet(ctx, zone, record.Name, recordType)
if err != nil {
if strings.Contains(err.Error(), "404: record is not found") {
rrSet = gcoreSDK.RRSet{
Type: recordType,
TTL: int(record.TTL.Seconds()),
Records: []gcoreSDK.ResourceRecord{
{
Content: []any{record.Value},
Enabled: true,
},
},
}
if err := cli.UpdateRRSet(ctx, zone, record.Name, recordType, rrSet); err != nil {
return nil, err
}
addedRecords = append(addedRecords, record)
continue
}
return nil, err
}
for _, rr := range rrSet.Records {
if rr.ContentToString() == record.Value {
continue
}
rrSet.Records = append(rrSet.Records, gcoreSDK.ResourceRecord{
Content: []any{record.Value},
Enabled: true,
})
}
if err := cli.UpdateRRSet(ctx, zone, record.Name, recordType, rrSet); err != nil {
return nil, err
}
addedRecords = append(addedRecords, record)
}
}
for i, record := range addedRecords {
addedRecords[i] = unqualifyRecordNames(record, zone)
}
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) {
cli := gcoreSDK.NewClient(gcoreSDK.PermanentAPIKeyAuth(p.APIKey))
for i, record := range records {
records[i] = qualityRecordNames(record, zone)
}
var updatedRecords []libdns.Record
for _, record := range records {
rrSet, err := cli.RRSet(ctx, zone, record.Name, record.Type)
if err != nil {
return nil, err
}
for _, rr := range rrSet.Records {
if rr.ContentToString() == record.Value {
continue
}
rrSet.Records = append(rrSet.Records, gcoreSDK.ResourceRecord{
Content: []any{record.Value},
Enabled: true,
})
}
if err := cli.UpdateRRSet(ctx, zone, record.Name, record.Type, rrSet); err != nil {
return nil, err
}
updatedRecords = append(updatedRecords, record)
}
for i, record := range updatedRecords {
updatedRecords[i] = unqualifyRecordNames(record, zone)
}
return updatedRecords, 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) {
cli := gcoreSDK.NewClient(gcoreSDK.PermanentAPIKeyAuth(p.APIKey))
for i, record := range records {
records[i] = qualityRecordNames(record, zone)
}
var deletedRecords []libdns.Record
for _, record := range records {
if cli.DeleteRRSetRecord(ctx, zone, record.Name, record.Type, record.Value) != nil {
return nil, fmt.Errorf("failed to delete record %v", record)
}
deletedRecords = append(deletedRecords, record)
}
for i, record := range deletedRecords {
deletedRecords[i] = unqualifyRecordNames(record, zone)
}
return deletedRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)