forked from libdns/hetzner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
167 lines (137 loc) · 3.92 KB
/
client.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
package vercel
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
"strings"
"time"
"github.com/libdns/libdns"
)
type getAllRecordsResponse struct {
Records []VercelRecord `json:"records"`
}
type createRecordResponse struct {
Uid string `json:"uid"`
}
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
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
defer response.Body.Close()
data, _ := ioutil.ReadAll(response.Body)
return data, fmt.Errorf("%s (%d)", http.StatusText(response.StatusCode), response.StatusCode)
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return data, nil
}
func getAllRecords(ctx context.Context, token string, zone string) ([]libdns.Record, 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
}
records := []libdns.Record{}
for _, r := range result.Records {
records = append(records, libdns.Record{
ID: r.Id,
Type: r.Type,
Name: r.Name,
Value: r.Value,
TTL: time.Duration(r.TTL) * time.Second,
})
}
return records, nil
}
func createRecord(ctx context.Context, token string, zone string, r libdns.Record) (libdns.Record, error) {
reqData := VercelRecord{
Type: r.Type,
Name: normalizeRecordName(r.Name, zone),
Value: r.Value,
TTL: int(math.Max((r.TTL.Seconds()), 60)),
}
reqBuffer, err := json.Marshal(reqData)
if err != nil {
return libdns.Record{}, 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 libdns.Record{}, err
}
data, err := doRequest(token, req)
if err != nil {
return libdns.Record{}, err
}
result := createRecordResponse{}
if err := json.Unmarshal(data, &result); err != nil {
return libdns.Record{}, err
}
return libdns.Record{
ID: result.Uid,
Type: r.Type,
Name: normalizeRecordName(r.Name, zone),
Value: r.Value,
}, nil
}
func deleteRecord(ctx context.Context, zone string, token string, record libdns.Record) error {
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("https://api.vercel.com/v2/domains/%s/records/%s", zone, record.ID), nil)
if err != nil {
return err
}
_, err = doRequest(token, req)
if err != nil {
return err
}
return nil
}
func updateRecord(ctx context.Context, token string, zone string, r libdns.Record) (libdns.Record, error) {
err := deleteRecord(ctx, zone, token, r)
if err != nil {
return libdns.Record{}, err
}
newRecord, err := createRecord(ctx, token, zone, r)
if err != nil {
return libdns.Record{}, err
}
return newRecord, nil
}
func createOrUpdateRecord(ctx context.Context, token string, zone string, r libdns.Record) (libdns.Record, error) {
if len(r.ID) == 0 {
return createRecord(ctx, token, zone, r)
}
return updateRecord(ctx, token, zone, r)
}
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)
}