-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
214 lines (195 loc) · 5.33 KB
/
client.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
213
214
package powerdns
import (
"context"
"fmt"
"io"
"strings"
"github.com/libdns/libdns"
"github.com/libdns/powerdns/txtsanitize"
pdns "github.com/mittwald/go-powerdns"
"github.com/mittwald/go-powerdns/apis/zones"
)
type client struct {
sID string
pdns.Client
}
func newClient(ServerID, ServerURL, APIToken string, debug io.Writer) (*client, error) {
if debug == nil {
debug = io.Discard
}
c, err := pdns.New(
pdns.WithBaseURL(ServerURL),
pdns.WithAPIKeyAuthentication(APIToken),
pdns.WithDebuggingOutput(debug),
)
if err != nil {
return nil, err
}
return &client{
sID: ServerID,
Client: c,
}, nil
}
func (c *client) updateRRs(ctx context.Context, zoneID string, recs []zones.ResourceRecordSet) error {
for _, rec := range recs {
err := c.Zones().AddRecordSetToZone(ctx, c.sID, zoneID, rec)
if err != nil {
return err
}
}
return nil
}
func mergeRRecs(fullZone *zones.Zone, records []libdns.Record) ([]zones.ResourceRecordSet, error) {
// pdns doesn't really have an append functionality, so we have to fake it by
// fetching existing rrsets for the zone and see if any already exist. If so,
// merge those with the existing data. Otherwise just add the record.
inHash := makeLDRecHash(records)
var rrsets []zones.ResourceRecordSet
// Merge existing resource record sets with any that were passed in to modify.
for _, t := range fullZone.ResourceRecordSets {
k := key(t.Name, t.Type)
if recs, ok := inHash[k]; ok && len(recs) > 0 {
rr := zones.ResourceRecordSet{
Name: t.Name,
Type: t.Type,
TTL: int(recs[0].TTL.Seconds()),
ChangeType: zones.ChangeTypeReplace,
Comments: t.Comments,
Records: make([]zones.Record, len(t.Records)),
}
copy(rr.Records, t.Records)
// squash duplicate values
dupes := make(map[string]bool)
for _, prec := range t.Records {
dupes[prec.Content] = true
}
// now for our additions
for _, rec := range recs {
if !dupes[rec.Value] {
rr.Records = append(rr.Records, zones.Record{
Content: rec.Value,
})
dupes[rec.Value] = true
}
}
rrsets = append(rrsets, rr)
delete(inHash, k)
}
}
// Any remaining in our input hash need to be straight adds / creates.
rrsets = append(rrsets, convertLDHash(inHash)...)
return rrsets, nil
}
// generate RessourceRecordSets that will delete records from zone
func cullRRecs(fullZone *zones.Zone, records []libdns.Record) []zones.ResourceRecordSet {
inHash := makeLDRecHash(records)
var rRSets []zones.ResourceRecordSet
for _, t := range fullZone.ResourceRecordSets {
k := key(t.Name, t.Type)
if recs, ok := inHash[k]; ok && len(recs) > 0 {
rRec := removeRecords(t, recs)
if len(rRec.Records) == 0 {
rRec.ChangeType = zones.ChangeTypeDelete
} else {
rRec.ChangeType = zones.ChangeTypeReplace
}
rRSets = append(rRSets, rRec)
}
}
return rRSets
}
// remove culls from rRSet record values
func removeRecords(rRSet zones.ResourceRecordSet, culls []libdns.Record) zones.ResourceRecordSet {
deleteItem := func(item string) []zones.Record {
recs := rRSet.Records
for i := len(recs) - 1; i >= 0; i-- {
if recs[i].Content == item {
copy(recs[i:], recs[:i+1])
recs = recs[:len(recs)-1]
}
}
return recs
}
for _, c := range culls {
rRSet.Records = deleteItem(c.Value)
}
return rRSet
}
func convertLDHash(inHash map[string][]libdns.Record) []zones.ResourceRecordSet {
var rrsets []zones.ResourceRecordSet
for _, recs := range inHash {
if len(recs) == 0 {
continue
}
rr := zones.ResourceRecordSet{
Name: recs[0].Name,
Type: recs[0].Type,
TTL: int(recs[0].TTL.Seconds()),
ChangeType: zones.ChangeTypeReplace,
}
for _, rec := range recs {
rr.Records = append(rr.Records, zones.Record{
Content: rec.Value,
})
}
rrsets = append(rrsets, rr)
}
return rrsets
}
func key(Name, Type string) string {
return Name + ":" + Type
}
func makeLDRecHash(records []libdns.Record) map[string][]libdns.Record {
// Keep track of records grouped by name + type
inHash := make(map[string][]libdns.Record)
for _, r := range records {
k := key(r.Name, r.Type)
inHash[k] = append(inHash[k], r)
}
return inHash
}
func (c *client) fullZone(ctx context.Context, zoneName string) (*zones.Zone, error) {
zc := c.Zones()
shortZone, err := c.shortZone(ctx, zoneName)
if err != nil {
return nil, err
}
fullZone, err := zc.GetZone(ctx, c.sID, shortZone.ID)
if err != nil {
return nil, err
}
return fullZone, nil
}
func (c *client) shortZone(ctx context.Context, zoneName string) (*zones.Zone, error) {
zc := c.Zones()
shortZones, err := zc.ListZone(ctx, c.sID, zoneName)
if err != nil {
return nil, err
}
if len(shortZones) != 1 {
return nil, fmt.Errorf("zone not found")
}
return &shortZones[0], nil
}
func (c *client) zoneID(ctx context.Context, zoneName string) (string, error) {
shortZone, err := c.shortZone(ctx, zoneName)
if err != nil {
return "", err
}
return shortZone.ID, nil
}
func convertNamesToAbsolute(zone string, records []libdns.Record) []libdns.Record {
out := make([]libdns.Record, len(records))
copy(out, records)
for i := range out {
name := libdns.AbsoluteName(out[i].Name, zone)
if !strings.HasSuffix(name, ".") {
name = name + "."
}
out[i].Name = name
if out[i].Type == "TXT" {
out[i].Value = txtsanitize.TXTSanitize(out[i].Value)
}
}
return out
}