forked from joeig/go-powerdns
/
records.go
83 lines (67 loc) · 2.34 KB
/
records.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
package powerdns
import (
"fmt"
)
// RecordsService handles communication with the records related methods of the Client API
type RecordsService service
// RRset structure with JSON API metadata
type RRset struct {
Name *string `json:"name,omitempty"`
Type *string `json:"type,omitempty"`
TTL *uint32 `json:"ttl,omitempty"`
ChangeType *string `json:"changetype,omitempty"`
Records []Record `json:"records,omitempty"`
Comments []Comment `json:"comments,omitempty"`
}
// Record structure with JSON API metadata
type Record struct {
Content *string `json:"content,omitempty"`
Disabled *bool `json:"disabled,omitempty"`
SetPTR *bool `json:"set-ptr,omitempty"`
}
// Comment structure with JSON API metadata
type Comment struct {
Content *string `json:"content,omitempty"`
Account *string `json:"account,omitempty"`
ModifiedAt *uint64 `json:"modified_at,omitempty"`
}
// RRsets structure with JSON API metadata
type RRsets struct {
Sets []RRset `json:"rrsets,omitempty"`
}
// Add creates a new resource record
func (r *RecordsService) Add(domain string, name string, recordType string, ttl uint32, content []string) error {
return r.Change(domain, name, recordType, ttl, content)
}
// Change replaces an existing resource record
func (r *RecordsService) Change(domain string, name string, recordType string, ttl uint32, content []string) error {
rrset := new(RRset)
rrset.Name = &name
rrset.Type = &recordType
rrset.TTL = &ttl
rrset.ChangeType = String("REPLACE")
for _, c := range content {
r := Record{Content: String(c), Disabled: Bool(false), SetPTR: Bool(false)}
rrset.Records = append(rrset.Records, r)
}
return r.patchRRset(domain, *rrset)
}
// Delete removes an existing resource record
func (r *RecordsService) Delete(domain string, name string, recordType string) error {
rrset := new(RRset)
rrset.Name = &name
rrset.Type = &recordType
rrset.ChangeType = String("DELETE")
return r.patchRRset(domain, *rrset)
}
func (r *RecordsService) patchRRset(domain string, rrset RRset) error {
rrset.Name = String(fixDomainSuffix(*rrset.Name))
payload := RRsets{}
payload.Sets = append(payload.Sets, rrset)
req, err := r.client.newRequest("PATCH", fmt.Sprintf("servers/%s/zones/%s", r.client.VHost, trimDomain(domain)), payload)
if err != nil {
return err
}
_, err = r.client.do(req, nil)
return err
}