-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
227 lines (188 loc) · 5.3 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
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
package hetzner
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/libdns/libdns"
)
type getAllRecordsResponse struct {
Records []record `json:"records"`
}
type getAllZonesResponse struct {
Zones []zone `json:"zones"`
}
type createRecordResponse struct {
Record record `json:"record"`
}
type updateRecordResponse struct {
Record record `json:"record"`
}
type zone struct {
ID string `json:"id"`
}
type record struct {
ID string `json:"id,omitempty"`
ZoneID string `json:"zone_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) {
request.Header.Add("Auth-API-Token", token)
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
return nil, 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 getZoneID(ctx context.Context, token string, zone string) (string, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/zones?name=%s", url.QueryEscape(zone)), nil)
data, err := doRequest(token, req)
if err != nil {
return "", err
}
result := getAllZonesResponse{}
if err := json.Unmarshal(data, &result); err != nil {
return "", err
}
if len(result.Zones) > 1 {
return "", errors.New("zone is ambiguous")
}
return result.Zones[0].ID, nil
}
func getAllRecords(ctx context.Context, token string, zone string) ([]libdns.Record, error) {
zoneID, err := getZoneID(ctx, token, zone)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("https://dns.hetzner.com/api/v1/records?zone_id=%s", zoneID), nil)
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) {
zoneID, err := getZoneID(ctx, token, zone)
if err != nil {
return libdns.Record{}, err
}
reqData := record{
ZoneID: zoneID,
Type: r.Type,
Name: normalizeRecordName(r.Name, zone),
Value: r.Value,
TTL: int(r.TTL.Seconds()),
}
reqBuffer, err := json.Marshal(reqData)
if err != nil {
return libdns.Record{}, err
}
req, err := http.NewRequestWithContext(ctx, "POST", "https://dns.hetzner.com/api/v1/records", bytes.NewBuffer(reqBuffer))
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.Record.ID,
Type: result.Record.Type,
Name: result.Record.Name,
Value: result.Record.Value,
TTL: time.Duration(result.Record.TTL) * time.Second,
}, nil
}
func deleteRecord(ctx context.Context, token string, record libdns.Record) error {
req, err := http.NewRequestWithContext(ctx, "DELETE", fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%s", record.ID), nil)
_, 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) {
zoneID, err := getZoneID(ctx, token, zone)
if err != nil {
return libdns.Record{}, err
}
reqData := record{
ZoneID: zoneID,
Type: r.Type,
Name: normalizeRecordName(r.Name, zone),
Value: r.Value,
TTL: int(r.TTL.Seconds()),
}
reqBuffer, err := json.Marshal(reqData)
if err != nil {
return libdns.Record{}, err
}
req, err := http.NewRequestWithContext(ctx, "PUT", fmt.Sprintf("https://dns.hetzner.com/api/v1/records/%s", r.ID), bytes.NewBuffer(reqBuffer))
if err != nil {
return libdns.Record{}, err
}
data, err := doRequest(token, req)
if err != nil {
return libdns.Record{}, err
}
result := updateRecordResponse{}
if err := json.Unmarshal(data, &result); err != nil {
return libdns.Record{}, err
}
return libdns.Record{
ID: result.Record.ID,
Type: result.Record.Type,
Name: result.Record.Name,
Value: result.Record.Value,
TTL: time.Duration(result.Record.TTL) * time.Second,
}, 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))
if normalized == "" {
normalized = "@"
}
return unFQDN(normalized)
}