forked from waynz0r/go-powerdns
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpowerdns.go
273 lines (215 loc) · 5.79 KB
/
powerdns.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package powerdns
//Based off of github.com/waynz0r/powerdns
import (
"fmt"
"log"
"net/url"
"strings"
"github.com/dghubble/sling"
)
// Error strct
type Error struct {
Message string `json:"error"`
}
// Error Returns
func (e Error) Error() string {
return fmt.Sprintf("%v", e.Message)
}
// CombinedRecord strct
type CombinedRecord struct {
Name string
Type string
TTL int
Records []string
}
// Zone struct
type Zone struct {
ID string `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Type string `json:"type"`
DNSsec bool `json:"dnssec"`
Serial int `json:"serial"`
NotifiedSerial int `json:"notified_serial"`
LastCheck int `json:"last_check"`
Records []struct {
Name string `json:"name"`
Type string `json:"type"`
TTL int `json:"ttl"`
Priority int `json:"priority"`
Disabled bool `json:"disabled"`
Content string `json:"content"`
} `json:"records"`
}
// Record struct
type Record struct {
Name string `json:"name"`
Type string `json:"type"`
TTL int `json:"ttl"`
Priority int `json:"priority"`
Disabled bool `json:"disabled"`
Content string `json:"content"`
}
// RRset struct
type RRset struct {
Name string `json:"name"`
Type string `json:"type"`
ChangeType string `json:"changetype"`
Records []Record `json:"records"`
}
// RRsets struct
type RRsets struct {
Sets []RRset `json:"rrsets"`
}
// PowerDNS struct
type PowerDNS struct {
scheme string
hostname string
port string
vhost string
domain string
apikey string
}
// New returns a new PowerDNS
func New(baseURL string, vhost string, domain string, apikey string) *PowerDNS {
if vhost == "" {
vhost = "localhost"
}
u, err := url.Parse(baseURL)
if err != nil {
log.Fatalf("%s is not a valid url: %v", baseURL, err)
}
hp := strings.Split(u.Host, ":")
hostname := hp[0]
var port string
if len(hp) > 1 {
port = hp[1]
} else {
if u.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}
return &PowerDNS{
scheme: u.Scheme,
hostname: hostname,
port: port,
vhost: vhost,
domain: domain,
apikey: apikey,
}
}
// AddRecord ...
func (p *PowerDNS) AddRecord(name string, recordType string, ttl int, content []string) (*Zone, error) {
zone, err := p.ChangeRecord(name, recordType, ttl, content, "UPSERT")
return zone, err
}
// DeleteRecord ...
func (p *PowerDNS) DeleteRecord(name string, recordType string, ttl int, content []string) (*Zone, error) {
zone, err := p.ChangeRecord(name, recordType, ttl, content, "DELETE")
return zone, err
}
// ChangeRecord ...
func (p *PowerDNS) ChangeRecord(name string, recordType string, ttl int, content []string, action string) (*Zone, error) {
Record := new(CombinedRecord)
Record.Name = name
Record.Type = recordType
Record.TTL = ttl
Record.Records = content
zone, err := p.patchRRset(*Record, action)
return zone, err
}
func (p *PowerDNS) patchRRset(record CombinedRecord, action string) (*Zone, error) {
if strings.HasSuffix(record.Name, ".") {
record.Name = strings.TrimSuffix(record.Name, ".")
}
Set := RRset{Name: record.Name, Type: record.Type, ChangeType: "REPLACE"}
if action == "DELETE" {
Set.ChangeType = "DELETE"
}
var R Record
for _, rec := range record.Records {
R = Record{Name: record.Name, Type: record.Type, TTL: record.TTL, Content: rec}
Set.Records = append(Set.Records, R)
}
json := RRsets{}
json.Sets = append(json.Sets, Set)
error := new(Error)
zone := new(Zone)
resp, err := p.getSling().Patch(p.domain).BodyJSON(json).Receive(zone, error)
if err == nil && resp.StatusCode >= 400 {
error.Message = strings.Join([]string{resp.Status, error.Message}, " ")
return nil, error
}
return zone, err
}
func (p *PowerDNS) getSling() *sling.Sling {
u := new(url.URL)
u.Host = p.hostname + ":" + p.port
u.Scheme = p.scheme
u.Path = "/servers/" + p.vhost + "/zones/"
Sling := sling.New().Base(u.String())
Sling.Set("X-API-Key", p.apikey)
return Sling
}
// GetRecords ...
func (p *PowerDNS) GetRecords() ([]Record, error) {
var records []Record
zone := new(Zone)
error := new(Error)
u := new(url.URL)
u.Host = p.hostname + ":" + p.port
u.Scheme = p.scheme
u.Path = "/servers/" + p.vhost + "/zones/"
resp, err := p.getSling().Path(p.domain).Set("X-API-Key", p.apikey).Receive(zone, error)
if err != nil {
return records, fmt.Errorf("PowerDNS API call has failed: %v", err)
}
if resp.StatusCode >= 400 {
error.Message = strings.Join([]string{resp.Status, error.Message}, " ")
return records, error
}
for _, rec := range zone.Records {
record := Record{Name: rec.Name, Type: rec.Type, TTL: rec.TTL, Priority: rec.Priority, Disabled: rec.Disabled, Content: rec.Content}
records = append(records, record)
}
return records, err
}
// GetCombinedRecords ...
func (p *PowerDNS) GetCombinedRecords() ([]CombinedRecord, error) {
var records []CombinedRecord
var uniqueRecords []CombinedRecord
//- Plain records from the zone
Records, err := p.GetRecords()
if err != nil {
return records, err
}
//- Iterate through records to combine them by name and type
for _, rec := range Records {
record := CombinedRecord{Name: rec.Name, Type: rec.Type, TTL: rec.TTL}
found := false
for _, uRec := range uniqueRecords {
if uRec.Name == rec.Name && uRec.Type == rec.Type {
found = true
continue
}
}
//- append them only if missing
if found == false {
uniqueRecords = append(uniqueRecords, record)
}
}
//- Get all values from the unique records
for _, uRec := range uniqueRecords {
for _, rec := range Records {
if uRec.Name == rec.Name && uRec.Type == rec.Type {
uRec.Records = append(uRec.Records, rec.Content)
}
}
records = append(records, uRec)
}
return records, nil
}
func init() {
}