-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
223 lines (169 loc) · 5.79 KB
/
Copy pathprovider.go
File metadata and controls
223 lines (169 loc) · 5.79 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Package liara implements a DNS record management client compatible
// with the libdns interfaces for Liara.
package liara
import (
"context"
"fmt"
"strings"
"github.com/libdns/libdns"
)
// Provider facilitates DNS record manipulation with Liara
type Provider struct {
APIToken string `json:"api_token,omitempty"`
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
client := newClient(p.APIToken)
// Liara doesn't expect a trailing dot in the zone name
zone = strings.TrimSuffix(zone, ".")
records, err := client.APIGetRecords(ctx, zone)
if err != nil {
return nil, err
}
libdns_records := make([]libdns.Record, 0, len(records))
for _, record := range records {
libdns_record, err := record.ToLibdnsRRType(zone)
if err != nil {
return nil, err
}
libdns_records = append(libdns_records, libdns_record...)
}
return libdns_records, nil
}
// AppendRecords adds records to the zone. It returns the records that were
// added in a form of libdns concrete type, and not a opaque RR type.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
client := newClient(p.APIToken)
// Liara doesn't expect a trailing dot in the zone name
zone = strings.TrimSuffix(zone, ".")
added_records := make([]libdns.Record, 0, len(records))
liaraRecords, err := ToLiaraAPIRecords(records, zone)
if err != nil {
return nil, err
}
for _, record := range liaraRecords {
added, err := client.APIPostRecord(ctx, zone, record)
if err != nil {
return nil, fmt.Errorf("Posting the record to Liara failed: %s", err)
}
addedAsLibdns, err := added.ToLibdnsRRType(zone)
if err != nil {
return nil, fmt.Errorf("Conversion to libdns type failed: %s", err)
}
added_records = append(added_records, addedAsLibdns...)
}
return added_records, nil
}
// Here, the "SetRecords" method doesn't provide atomicity. Meaning a non nil value for the returned
// "error" can indicate that the zone is in a invalid state, and there is
// no rollback operation happened to rollback the previous changes.
//
// SetRecords sets the records in the zone, either by updating existing records or creating new ones.
// It returns the updated records, And not the skipped ones.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
// Liara doesn't expect a trailing dot in the zone name
zone = strings.TrimSuffix(zone, ".")
existingRecords, err := p.GetRecords(ctx, zone)
if err != nil {
return nil, fmt.Errorf("Failure when fetching the record from Liara: %s", err)
}
toDelete := make([]libdns.Record, 0)
for _, new := range records {
exists := libDNSContains(existingRecords, new)
if exists != nil {
toDelete = append(toDelete, exists...)
}
}
_, err = p.DeleteRecords(ctx, zone, toDelete)
if err != nil {
return nil, fmt.Errorf("Deleting records from Liara has failed: %s", err)
}
return p.AppendRecords(ctx, zone, records)
}
// DeleteRecords deletes the specified 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) {
client := newClient(p.APIToken)
deleted := make([]libdns.Record, 0, len(records))
// Liara doesn't expect a trailing dot in the zone name
zone = strings.TrimSuffix(zone, ".")
existingRecords, err := client.APIGetRecords(ctx, zone)
if err != nil {
return nil, fmt.Errorf("Failed to fetch the dns records form Liara: %s", err)
}
for _, record := range records {
toRemoveLiara, err := ToLiaraAPIRecord(record, zone)
if err != nil {
return nil, fmt.Errorf("Failed to convert libdns type to APIRecord: %s", err)
}
existingLiara, yes := APIRecordContains(existingRecords, toRemoveLiara)
if !yes {
// Silently skipping the delete request, for a non-existent record.
continue
}
// Check to see how much of the content slice remains after deletion.
remaining := RemainingContent(
existingLiara.Contents,
toRemoveLiara.Contents,
)
if len(remaining) == 0 {
// remaning 0 means the entire record should be deleted.
err = client.APIDeleteRecord(ctx, zone, existingLiara.ID)
if err != nil {
return nil, fmt.Errorf("Error occured when deleting a record from Liara: %s", err.Error())
}
} else {
// remaning != 0 means the record has to be updated with new
// remaining values.
// E.x. example.com | 1.1.1.1, 1.0.0.1 => example.com | 1.1.1.1
existingLiara.Contents = remaining
updatedRecord := *existingLiara
updatedRecord.ID = ""
_, err = client.APIUpdateRecord(
ctx,
zone,
existingLiara.ID,
updatedRecord,
)
if err != nil {
return nil, fmt.Errorf(
"error occurred when updating a record through Liara: %w",
err,
)
}
}
converted, err := toRemoveLiara.ToLibdnsRRType(zone)
if err != nil {
return nil, err
}
deleted = append(deleted, converted...)
}
return deleted, nil
}
// Differentiates Remaining content in "APIRecord.Content".
// Takes the existing APIRecord.Contents and removes the contents that the user asked to delete.
func RemainingContent(
existing []APIRecordContent,
recordsToRemove []APIRecordContent,
) []APIRecordContent {
remaining := make([]APIRecordContent, 0, len(existing))
for _, record := range existing {
found := false
for _, recordToRemove := range recordsToRemove {
if record == recordToRemove {
found = true
break
}
}
if !found {
remaining = append(remaining, record)
}
}
return remaining
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)