/
client.go
197 lines (158 loc) · 4.62 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
package internal
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
)
// defaultBaseURL represents the API endpoint to call.
const defaultBaseURL = "https://dns.hetzner.com"
const authHeader = "Auth-API-Token"
// Client the Hetzner client.
type Client struct {
HTTPClient *http.Client
BaseURL string
apiKey string
}
// NewClient Creates a new Hetzner client.
func NewClient(apiKey string) *Client {
return &Client{
HTTPClient: http.DefaultClient,
BaseURL: defaultBaseURL,
apiKey: apiKey,
}
}
// GetTxtRecord gets a TXT record.
func (c *Client) GetTxtRecord(name, value, zoneID string) (*DNSRecord, error) {
records, err := c.getRecords(zoneID)
if err != nil {
return nil, err
}
for _, record := range records.Records {
if record.Type == "TXT" && record.Name == name && record.Value == value {
return &record, nil
}
}
return nil, fmt.Errorf("could not find record: zone ID: %s; Record: %s", zoneID, name)
}
// https://dns.hetzner.com/api-docs#operation/GetRecords
func (c *Client) getRecords(zoneID string) (*DNSRecords, error) {
endpoint, err := c.createEndpoint("api", "v1", "records")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}
query := endpoint.Query()
query.Set("zone_id", zoneID)
endpoint.RawQuery = query.Encode()
resp, err := c.do(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("could not get records: zone ID: %s; Status: %s; Body: %s",
zoneID, resp.Status, string(bodyBytes))
}
records := &DNSRecords{}
err = json.NewDecoder(resp.Body).Decode(records)
if err != nil {
return nil, fmt.Errorf("failed to decode response body: %w", err)
}
return records, nil
}
// CreateRecord creates a DNS record.
// https://dns.hetzner.com/api-docs#operation/CreateRecord
func (c *Client) CreateRecord(record DNSRecord) error {
body, err := json.Marshal(record)
if err != nil {
return err
}
endpoint, err := c.createEndpoint("api", "v1", "records")
if err != nil {
return fmt.Errorf("failed to create endpoint: %w", err)
}
resp, err := c.do(http.MethodPost, endpoint, bytes.NewReader(body))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("could not create record %s; Status: %s; Body: %s", string(body), resp.Status, string(bodyBytes))
}
return nil
}
// DeleteRecord deletes a DNS record.
// https://dns.hetzner.com/api-docs#operation/DeleteRecord
func (c *Client) DeleteRecord(recordID string) error {
endpoint, err := c.createEndpoint("api", "v1", "records", recordID)
if err != nil {
return fmt.Errorf("failed to create endpoint: %w", err)
}
resp, err := c.do(http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("could not delete record: %s; Status: %s", resp.Status, recordID)
}
return nil
}
// GetZoneID gets the zone ID for a domain.
func (c *Client) GetZoneID(domain string) (string, error) {
zones, err := c.getZones()
if err != nil {
return "", err
}
for _, zone := range zones.Zones {
if zone.Name == domain {
return zone.ID, nil
}
}
return "", fmt.Errorf("could not get zone for domain %s not found", domain)
}
// https://dns.hetzner.com/api-docs#operation/GetZones
func (c *Client) getZones() (*Zones, error) {
endpoint, err := c.createEndpoint("api", "v1", "zones")
if err != nil {
return nil, fmt.Errorf("failed to create endpoint: %w", err)
}
resp, err := c.do(http.MethodGet, endpoint, nil)
if err != nil {
return nil, fmt.Errorf("could not get zones: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not get zones: %s", resp.Status)
}
zones := &Zones{}
err = json.NewDecoder(resp.Body).Decode(zones)
if err != nil {
return nil, fmt.Errorf("failed to decode response body: %w", err)
}
return zones, nil
}
func (c *Client) do(method string, endpoint fmt.Stringer, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(method, endpoint.String(), body)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set(authHeader, c.apiKey)
return c.HTTPClient.Do(req)
}
func (c *Client) createEndpoint(parts ...string) (*url.URL, error) {
baseURL, err := url.Parse(c.BaseURL)
if err != nil {
return nil, err
}
endpoint, err := baseURL.Parse(path.Join(parts...))
if err != nil {
return nil, err
}
return endpoint, nil
}