-
Notifications
You must be signed in to change notification settings - Fork 7
/
provider.go
154 lines (138 loc) · 3.74 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
// Package powerdns implements a powerdns
package powerdns
import (
"context"
"io"
"os"
"strings"
"sync"
"time"
"github.com/libdns/libdns"
)
// Provider facilitates DNS record manipulation with PowerDNS.
type Provider struct {
// ServerURL is the location of the pdns server.
ServerURL string `json:"server_url"`
// ServerID is the id of the server. localhost will be used
// if this is omitted.
ServerID string `json:"server_id,omitempty"`
// APIToken is the auth token.
APIToken string `json:"api_token,omitempty"`
// Debug - can set this to stdout or stderr to dump
// debugging information about the API interaction with
// powerdns. This will dump your auth token in plain text
// so be careful.
Debug string `json:"debug,omitempty"`
mu sync.Mutex
c *client
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
c, err := p.client()
if err != nil {
return nil, err
}
prec, err := c.fullZone(ctx, zone)
if err != nil {
return nil, err
}
recs := make([]libdns.Record, 0, len(prec.ResourceRecordSets))
for _, rec := range prec.ResourceRecordSets {
for _, v := range rec.Records {
recs = append(recs, libdns.Record{
ID: prec.ID,
Type: rec.Type,
Name: libdns.RelativeName(rec.Name, zone),
Value: v.Content,
TTL: time.Second * time.Duration(rec.TTL),
Priority: 0,
})
}
}
return recs, 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) {
c, err := p.client()
if err != nil {
return nil, err
}
fullZone, err := c.fullZone(ctx, zone)
if err != nil {
return nil, err
}
rrecs, err := mergeRRecs(fullZone, convertNamesToAbsolute(zone, records))
if err != nil {
return nil, err
}
err = c.updateRRs(ctx, fullZone.ID, rrecs)
if 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) {
c, err := p.client()
if err != nil {
return nil, err
}
zID, err := c.zoneID(ctx, zone)
if err != nil {
return nil, err
}
inHash := makeLDRecHash(convertNamesToAbsolute(zone, records))
rRecs := convertLDHash(inHash)
err = c.updateRRs(ctx, zID, rRecs)
if err != nil {
return nil, err
}
return records, 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) {
c, err := p.client()
if err != nil {
return nil, err
}
fullZone, err := c.fullZone(ctx, zone)
if err != nil {
return nil, err
}
rRSets := cullRRecs(fullZone, convertNamesToAbsolute(zone, records))
err = c.updateRRs(ctx, fullZone.ID, rRSets)
if err != nil {
return nil, err
}
return records, nil
}
func (p *Provider) client() (*client, error) {
p.mu.Lock()
defer p.mu.Unlock()
if p.c == nil {
var err error
if p.ServerID == "" {
p.ServerID = "localhost"
}
var debug io.Writer
switch strings.ToLower(p.Debug) {
case "stdout", "yes", "true", "1":
debug = os.Stdout
case "stderr":
debug = os.Stderr
}
p.c, err = newClient(p.ServerID, p.ServerURL, p.APIToken, debug)
if err != nil {
return nil, err
}
}
return p.c, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)