-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
194 lines (164 loc) · 5.16 KB
/
provider.go
File metadata and controls
194 lines (164 loc) · 5.16 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
// Package libdnsregery implements a DNS record management client compatible
// with the libdns interfaces for Regery.
package libdnsregery
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/libdns/libdns"
)
type RegeryDNSRecord struct {
Address string `json:"address"`
Value string `json:"value"`
Type string `json:"type"`
TTL int `json:"ttl,omitempty"`
Name string `json:"name"`
}
type RegeryDNSRecords struct {
Records []RegeryDNSRecord `json:"records"`
}
// Provider facilitates DNS record manipulation with Regery.
type Provider struct {
APIToken string `json:"api_token,omitempty"`
Secret string `json:"secret"`
}
const baseUrl = "https://api.regery.com/v1/domains"
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
url := fmt.Sprintf("%s/%s/records", baseUrl, zone)
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", fmt.Sprintf("%s:%s", p.APIToken, p.Secret))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
contents, _ := io.ReadAll(resp.Body)
log.Fatalf("Received non-200 response: %d %s", resp.StatusCode, contents)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response body: %v", err)
return nil, err
}
var result RegeryDNSRecords
if err := json.Unmarshal(body, &result); err != nil {
log.Fatalf("Failed to parse JSON: %v", err)
return nil, err
}
var records []libdns.Record
for _, record := range result.Records {
var value string
if record.Value == "" {
value = record.Address
} else {
value = record.Value
}
records = append(records, libdns.RR{
Name: record.Name,
TTL: time.Duration(record.TTL) * time.Second,
Type: record.Type,
Data: value,
})
}
return records, nil
}
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
url := fmt.Sprintf("%s/%s/records", baseUrl, zone)
var regeryRecords []RegeryDNSRecord
for _, r := range records {
regeryRecord := toRegeryDNSRecord(r)
regeryRecords = append(regeryRecords, regeryRecord)
}
request, err := json.Marshal(RegeryDNSRecords{regeryRecords})
req, err := http.NewRequest("POST", url, bytes.NewBuffer(request))
req.Header.Add("Authorization", fmt.Sprintf("%s:%s", p.APIToken, p.Secret))
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
contents, _ := io.ReadAll(resp.Body)
log.Fatalf("Received non-200 response: %d\n%s\n%s\n%+v", resp.StatusCode, contents, request, records)
}
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) {
var err error
existingRecords, err := p.GetRecords(ctx, zone)
if err != nil {
return nil, err
}
var toDelete []libdns.Record
for _, r := range existingRecords {
for _, newRecord := range records {
if newRecord.RR().Name == r.RR().Name {
toDelete = append(toDelete, r)
}
}
}
appendedRecords, err := p.AppendRecords(ctx, zone, records)
if err != nil {
return nil, err
}
_, err = p.DeleteRecords(ctx, zone, toDelete)
if err != nil {
log.Printf("Failed to delete records that were overwritten, %s", err)
}
return appendedRecords, 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) {
url := fmt.Sprintf("%s/%s/records", baseUrl, zone)
var regeryRecords []RegeryDNSRecord
for _, r := range records {
regeryRecord := toRegeryDNSRecord(r)
regeryRecords = append(regeryRecords, regeryRecord)
}
request, err := json.Marshal(RegeryDNSRecords{regeryRecords})
req, err := http.NewRequest("DELETE", url, bytes.NewBuffer(request))
req.Header.Add("Authorization", fmt.Sprintf("%s:%s", p.APIToken, p.Secret))
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
contents, _ := io.ReadAll(resp.Body)
log.Fatalf("Received non-200 response: %d\n%s", resp.StatusCode, contents)
}
return records, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)
func toRegeryDNSRecord(r libdns.Record) RegeryDNSRecord {
rr := r.RR()
var ttlSeconds int
ttlSeconds = int(rr.TTL.Seconds())
if ttlSeconds == 0 {
ttlSeconds = 3600
}
return RegeryDNSRecord{
Address: rr.Data,
Value: rr.Data,
Type: rr.Type,
TTL: ttlSeconds,
Name: rr.Name,
}
}