-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
204 lines (159 loc) · 5.5 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
package namedotcom
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"github.com/libdns/libdns"
)
// nameClient extends the namedotcom api and request handler to the provider..
type nameClient struct {
client *nameDotCom
mutex sync.Mutex
}
// getClient initiates a new nameClient and assigns it to the provider..
func (p *Provider) getClient(ctx context.Context) error {
newNameclient, err := NewNameDotComClient(ctx, p.Token, p.User, p.Server)
if err != nil {
return err
}
p.client = newNameclient
return nil
}
// listZones returns all the zones (domains) for the user
func (p *Provider) listZones(ctx context.Context) ([]libdns.Zone, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
var (
zones []libdns.Zone
method = "GET"
body io.Reader
resp = &listDomainsResponse{}
reqPage = 1
err error
)
if err = p.getClient(ctx); err != nil {
return []libdns.Zone{}, err
}
for reqPage > 0 {
if reqPage != 0 {
endpoint := fmt.Sprintf("/v4/domains?page=%d", reqPage)
if body, err = p.client.doRequest(ctx, method, endpoint, nil); err != nil {
return []libdns.Zone{}, fmt.Errorf("request failed: %w", err)
}
if err = json.NewDecoder(body).Decode(resp); err != nil {
return []libdns.Zone{}, fmt.Errorf("could not decode name.com's response: %w", err)
}
for _, domain := range resp.Domains {
zones = append(zones, libdns.Zone{
Name: domain.DomainName,
})
}
reqPage = int(resp.NextPage)
}
}
return zones, nil
}
// listAllRecords returns all records for the given zone .. GET /v4/domains/{ domainName }/records
func (p *Provider) listAllRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
var (
records []libdns.Record
/*** 'zone' args that are passed in using compliant zone formats have the FQDN '.' suffix qualifier
and in order to use the zone arg as a domainName reference to name.com's api we must remove the '.' suffix.
otherwise the api will not recognize the domain.. ***/
unFQDNzone = strings.TrimSuffix(zone, ".")
method = "GET"
body io.Reader
resp = &listRecordsResponse{}
reqPage = 1
err error
)
if err = p.getClient(ctx); err != nil {
return []libdns.Record{}, err
}
// handle pagination, in case domain has more records then the default of 1000 per page
for reqPage > 0 {
if reqPage != 0 {
endpoint := fmt.Sprintf("/v4/domains/%s/records?page=%d", unFQDNzone, reqPage)
if body, err = p.client.doRequest(ctx, method, endpoint, nil); err != nil {
return []libdns.Record{}, fmt.Errorf("request failed: %w", err)
}
if err = json.NewDecoder(body).Decode(resp); err != nil {
return []libdns.Record{}, fmt.Errorf("could not decode name.com's response: %w", err)
}
for _, record := range resp.Records {
records = append(records, record.toLibDNSRecord())
}
reqPage = int(resp.NextPage)
}
}
return records, nil
}
// deleteRecord DELETE /v4/domains/{ domainName }/records/{ record.ID }
func (p *Provider) deleteRecord(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
var (
shouldDelete nameDotComRecord
unFQDNzone = strings.TrimSuffix(zone, ".")
method = "DELETE"
endpoint = fmt.Sprintf("/v4/domains/%s/records/%s", unFQDNzone, record.ID)
body io.Reader
post = &bytes.Buffer{}
err error
)
shouldDelete.fromLibDNSRecord(record, unFQDNzone)
if err = p.getClient(ctx); err != nil {
return libdns.Record{}, err
}
if err = json.NewEncoder(post).Encode(shouldDelete); err != nil {
return libdns.Record{}, fmt.Errorf("could not encode form data for request: %w", err)
}
if body, err = p.client.doRequest(ctx, method, endpoint, post); err != nil {
return libdns.Record{}, fmt.Errorf("request to delete the record was not successful: %w", err)
}
if err = json.NewDecoder(body).Decode(&shouldDelete); err != nil {
return libdns.Record{}, fmt.Errorf("could not decode the response from name.com: %w", err)
}
return shouldDelete.toLibDNSRecord(), nil
}
// upsertRecord PUT || POST /v4/domains/{ domainName }/records/{ record.ID }
func (p *Provider) upsertRecord(ctx context.Context, zone string, canidateRecord libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
var (
shouldUpsert nameDotComRecord
unFQDNzone = strings.TrimSuffix(zone, ".")
method = "PUT"
endpoint = fmt.Sprintf("/v4/domains/%s/records/%s", unFQDNzone, canidateRecord.ID)
body io.Reader
post = &bytes.Buffer{}
err error
)
if canidateRecord.ID == "" {
method = "POST"
endpoint = fmt.Sprintf("/v4/domains/%s/records", unFQDNzone)
}
shouldUpsert.fromLibDNSRecord(canidateRecord, unFQDNzone)
if err = p.getClient(ctx); err != nil {
return libdns.Record{}, err
}
if err = json.NewEncoder(post).Encode(shouldUpsert); err != nil {
return libdns.Record{}, fmt.Errorf("could not encode the form data for the request: %w", err)
}
if body, err = p.client.doRequest(ctx, method, endpoint, post); err != nil {
if strings.Contains(err.Error(), "Duplicate Record") {
err = fmt.Errorf("name.com will not allow an update to a record that has identical values to an existing record: %w", err)
}
return libdns.Record{}, fmt.Errorf("request to update the record was not successful: %w", err)
}
if err = json.NewDecoder(body).Decode(&shouldUpsert); err != nil {
return libdns.Record{}, fmt.Errorf("could not decode name.com's response: %w", err)
}
return shouldUpsert.toLibDNSRecord(), nil
}