-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
79 lines (69 loc) · 2.48 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
package dnsimple
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/dnsimple/dnsimple-go/dnsimple"
)
// dnsimpleClient is a wrapper for `dnsimple.Client`.
type dnsimpleClient struct {
*dnsimple.Client
}
// dnsimpleService is an interface for dnsimpleClient.
type dnsimpleService interface {
getZone(ctx context.Context, accountID string, zoneName string) (*dnsimple.Zone, error)
listZoneRecords(ctx context.Context, accountID string, zoneName string, maxRetries int) ([]dnsimple.ZoneRecord, error)
updateZoneStatus(accountID string, apiCaller DNSimpleApiCaller, maxRetries int, status updateZoneStatusRequest) (err error)
}
// getZone is a wrapper method around `dnsimple.Client.Zones.GetZone`
// it checks if a zone exists in DNSimple.
func (c dnsimpleClient) getZone(ctx context.Context, accountID string, zoneName string) (*dnsimple.Zone, error) {
response, err := c.Zones.GetZone(ctx, accountID, zoneName)
if err != nil {
return nil, err
}
return response.Data, nil
}
// listZoneRecords is a wrapper for `dnsimple.Client.Zones.ListRecords`.
// It fetches and returns all record sets for a zone handling pagination.
func (c dnsimpleClient) listZoneRecords(ctx context.Context, accountID string, zoneName string, maxRetries int) ([]dnsimple.ZoneRecord, error) {
var rs []dnsimple.ZoneRecord
listOptions := &dnsimple.ZoneRecordListOptions{}
listOptions.PerPage = dnsimple.Int(100)
// Fetch all records for the zone.
for {
var response *dnsimple.ZoneRecordsResponse
listErr := retryable(maxRetries, func() (listErr error) {
// Our API does not expect the zone name to end with a dot.
response, listErr = c.Zones.ListRecords(ctx, accountID, strings.TrimSuffix(zoneName, "."), listOptions)
return
})
if listErr != nil {
return nil, listErr
}
rs = append(rs, response.Data...)
if response.Pagination.CurrentPage >= response.Pagination.TotalPages {
break
}
listOptions.Page = dnsimple.Int(response.Pagination.CurrentPage + 1)
}
return rs, nil
}
func (c dnsimpleClient) updateZoneStatus(accountID string, apiCaller DNSimpleApiCaller, maxRetries int, status updateZoneStatusRequest) (err error) {
statusJson, err := json.Marshal(status)
if err != nil {
panic(fmt.Errorf("failed to serialise status request: %v", err))
}
sendStatusError := retryable(maxRetries, func() (err error) {
err = apiCaller(
fmt.Sprintf("/v2/%s/platform/statuses", accountID),
statusJson,
)
return
})
if sendStatusError != nil {
return sendStatusError
}
return nil
}