/
client.go
192 lines (150 loc) · 4.78 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
package internal
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/go-acme/lego/v4/providers/dns/internal/errutils"
)
const defaultBaseURL string = "https://api.domeneshop.no/v0"
// Client implements a very simple wrapper around the Domeneshop API.
// For now, it will only deal with adding and removing TXT records, as required by ACME providers.
// https://api.domeneshop.no/docs/
type Client struct {
apiToken string
apiSecret string
baseURL *url.URL
HTTPClient *http.Client
}
// NewClient returns an instance of the Domeneshop API wrapper.
func NewClient(apiToken, apiSecret string) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
return &Client{
apiToken: apiToken,
apiSecret: apiSecret,
baseURL: baseURL,
HTTPClient: &http.Client{Timeout: 5 * time.Second},
}
}
// GetDomainByName fetches the domain list and returns the Domain object for the matching domain.
// https://api.domeneshop.no/docs/#operation/getDomains
func (c *Client) GetDomainByName(ctx context.Context, domain string) (*Domain, error) {
endpoint := c.baseURL.JoinPath("domains")
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var domains []Domain
err = c.do(req, &domains)
if err != nil {
return nil, err
}
for _, d := range domains {
if !d.Services.DNS {
// Domains without DNS service cannot have DNS record added.
continue
}
if d.Name == domain {
return &d, nil
}
}
return nil, fmt.Errorf("failed to find matching domain name: %s", domain)
}
// CreateTXTRecord creates a TXT record with the provided host (subdomain) and data.
// https://api.domeneshop.no/docs/#tag/dns/paths/~1domains~1{domainId}~1dns/post
func (c *Client) CreateTXTRecord(ctx context.Context, domain *Domain, host string, data string) error {
endpoint := c.baseURL.JoinPath("domains", strconv.Itoa(domain.ID), "dns")
record := DNSRecord{
Data: data,
Host: host,
TTL: 300,
Type: "TXT",
}
req, err := newJSONRequest(ctx, http.MethodPost, endpoint, record)
if err != nil {
return err
}
return c.do(req, nil)
}
// DeleteTXTRecord deletes the DNS record matching the provided host and data.
// https://api.domeneshop.no/docs/#tag/dns/paths/~1domains~1{domainId}~1dns~1{recordId}/delete
func (c *Client) DeleteTXTRecord(ctx context.Context, domain *Domain, host string, data string) error {
record, err := c.getDNSRecordByHostData(ctx, *domain, host, data)
if err != nil {
return err
}
endpoint := c.baseURL.JoinPath("domains", strconv.Itoa(domain.ID), "dns", strconv.Itoa(record.ID))
req, err := newJSONRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return err
}
return c.do(req, nil)
}
// getDNSRecordByHostData finds the first matching DNS record with the provided host and data.
// https://api.domeneshop.no/docs/#operation/getDnsRecords
func (c *Client) getDNSRecordByHostData(ctx context.Context, domain Domain, host string, data string) (*DNSRecord, error) {
endpoint := c.baseURL.JoinPath("domains", strconv.Itoa(domain.ID), "dns")
req, err := newJSONRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
var records []DNSRecord
err = c.do(req, &records)
if err != nil {
return nil, err
}
for _, r := range records {
if r.Host == host && r.Data == data {
return &r, nil
}
}
return nil, fmt.Errorf("failed to find record with host %s for domain %s", host, domain.Name)
}
// do a request against the API,
// and makes sure that the required Authorization header is set using `setBasicAuth`.
func (c *Client) do(req *http.Request, result any) error {
req.SetBasicAuth(c.apiToken, c.apiSecret)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return errutils.NewHTTPDoError(req, err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode >= http.StatusBadRequest {
return errutils.NewUnexpectedResponseStatusCodeError(req, resp)
}
if result == nil {
return nil
}
raw, err := io.ReadAll(resp.Body)
if err != nil {
return errutils.NewReadResponseError(req, resp.StatusCode, err)
}
err = json.Unmarshal(raw, result)
if err != nil {
return errutils.NewUnmarshalError(req, resp.StatusCode, raw, err)
}
return nil
}
func newJSONRequest(ctx context.Context, method string, endpoint *url.URL, payload any) (*http.Request, error) {
buf := new(bytes.Buffer)
if payload != nil {
err := json.NewEncoder(buf).Encode(payload)
if err != nil {
return nil, fmt.Errorf("failed to create request JSON body: %w", err)
}
}
req, err := http.NewRequestWithContext(ctx, method, endpoint.String(), buf)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Accept", "application/json")
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
return req, nil
}