-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient.go
More file actions
197 lines (165 loc) · 4.9 KB
/
Copy pathclient.go
File metadata and controls
197 lines (165 loc) · 4.9 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
195
196
197
package vercel
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strings"
"time"
"github.com/libdns/libdns"
)
type getAllRecordsResponse struct {
Records []VercelRecord `json:"records"`
}
// VercelRecord is Vercel's own wire format for a DNS record. Unlike
// [libdns.RR], it carries the provider-assigned ID needed to update or
// delete a record.
type VercelRecord struct {
Id string `json:"id,omitempty"`
Type string `json:"type"`
Name string `json:"name"`
Value string `json:"value"`
TTL int `json:"ttl"`
}
func doRequest(token string, request *http.Request) ([]byte, error) {
// Bearer Token for Vercel Authorization: Bearer <TOKEN>
request.Header.Add("Authorization", "Bearer "+token)
request.Header.Add("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
return data, fmt.Errorf("%s (%d)", http.StatusText(response.StatusCode), response.StatusCode)
}
return data, nil
}
// getAllRawRecords fetches the zone's records in Vercel's own format, which
// retains the record ID that [libdns.RR] no longer carries.
func getAllRawRecords(ctx context.Context, token string, zone string) ([]VercelRecord, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://api.vercel.com/v4/domains/%s/records", zone), nil)
if err != nil {
return nil, err
}
data, err := doRequest(token, req)
if err != nil {
return nil, err
}
result := getAllRecordsResponse{}
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}
return result.Records, nil
}
func getAllRecords(ctx context.Context, token string, zone string) ([]libdns.Record, error) {
raw, err := getAllRawRecords(ctx, token, zone)
if err != nil {
return nil, err
}
records := make([]libdns.Record, 0, len(raw))
for _, r := range raw {
rec, err := vercelToRR(r).Parse()
if err != nil {
return nil, fmt.Errorf("parsing Vercel DNS record %+v: %v", r, err)
}
records = append(records, rec)
}
return records, nil
}
func vercelToRR(r VercelRecord) libdns.RR {
return libdns.RR{
Name: r.Name,
Type: r.Type,
Data: r.Value,
TTL: time.Duration(r.TTL) * time.Second,
}
}
func createRecord(ctx context.Context, token string, zone string, record libdns.Record) (libdns.Record, error) {
rr := record.RR()
name := normalizeRecordName(rr.Name, zone)
reqData := VercelRecord{
Type: rr.Type,
Name: name,
Value: rr.Data,
TTL: int(math.Max(rr.TTL.Seconds(), 60)),
}
reqBuffer, err := json.Marshal(reqData)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("https://api.vercel.com/v2/domains/%s/records", zone), bytes.NewBuffer(reqBuffer))
if err != nil {
return nil, err
}
if _, err := doRequest(token, req); err != nil {
return nil, err
}
return (libdns.RR{
Name: name,
Type: rr.Type,
Data: rr.Data,
TTL: time.Duration(reqData.TTL) * time.Second,
}).Parse()
}
func deleteRecordByID(ctx context.Context, token string, zone string, id string) error {
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("https://api.vercel.com/v2/domains/%s/records/%s", zone, id), nil)
if err != nil {
return err
}
_, err = doRequest(token, req)
return err
}
// deleteRecords implements the [libdns.RecordDeleter] contract: it only
// deletes records that exactly match an input record's name, type, TTL, and
// value, except that an empty type, TTL, or value on the input acts as a
// wildcard for that field (name is always required).
func deleteRecords(ctx context.Context, token string, zone string, records []libdns.Record) ([]libdns.Record, error) {
raw, err := getAllRawRecords(ctx, token, zone)
if err != nil {
return nil, err
}
var deleted []libdns.Record
for _, record := range records {
rr := record.RR()
name := normalizeRecordName(rr.Name, zone)
for _, r := range raw {
if r.Name != name {
continue
}
if rr.Type != "" && r.Type != rr.Type {
continue
}
if rr.Data != "" && r.Value != rr.Data {
continue
}
if rr.TTL != 0 && time.Duration(r.TTL)*time.Second != rr.TTL {
continue
}
if err := deleteRecordByID(ctx, token, zone, r.Id); err != nil {
return deleted, err
}
parsed, err := vercelToRR(r).Parse()
if err != nil {
return deleted, fmt.Errorf("parsing Vercel DNS record %+v: %v", r, err)
}
deleted = append(deleted, parsed)
}
}
return deleted, nil
}
func normalizeRecordName(recordName string, zone string) string {
// Workaround for https://github.com/caddy-dns/hetzner/issues/3
// Can be removed after https://github.com/libdns/libdns/issues/12
normalized := unFQDN(recordName)
normalized = strings.TrimSuffix(normalized, unFQDN(zone))
return unFQDN(normalized)
}