forked from libdns/digitalocean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
134 lines (105 loc) · 2.67 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
package digitalocean
import (
"context"
"strconv"
"strings"
"sync"
"time"
"github.com/digitalocean/godo"
"github.com/libdns/libdns"
)
type Client struct {
client *godo.Client
mutex sync.Mutex
}
func (p *Provider) getClient() error {
if p.client == nil {
p.client = godo.NewFromToken(p.APIToken)
}
return nil
}
func (p *Provider) getDNSEntries(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.getClient()
opt := &godo.ListOptions{}
var records []libdns.Record
for {
domains, resp, err := p.client.Domains.Records(ctx, zone, opt)
if err != nil {
return records, err
}
for _, entry := range domains {
record := libdns.Record{
Name: entry.Name + "." + strings.Trim(zone, ".") + ".",
Value: entry.Data,
Type: entry.Type,
TTL: time.Duration(entry.TTL) * time.Second,
ID: strconv.Itoa(entry.ID),
}
records = append(records, record)
}
// if we are at the last page, break out the for loop
if resp.Links == nil || resp.Links.IsLastPage() {
break
}
page, err := resp.Links.CurrentPage()
if err != nil {
return records, err
}
// set the page we want for the next request
opt.Page = page + 1
}
return records, nil
}
func (p *Provider) addDNSEntry(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.getClient()
entry := godo.DomainRecordEditRequest{
Name: strings.Trim(strings.ReplaceAll(record.Name, zone, ""), "."),
Data: record.Value,
Type: record.Type,
TTL: int(record.TTL.Seconds()),
}
rec, _, err := p.client.Domains.CreateRecord(ctx, zone, &entry)
if err != nil {
return record, err
}
record.ID = strconv.Itoa(rec.ID)
return record, nil
}
func (p *Provider) removeDNSEntry(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.getClient()
id, err := strconv.Atoi(record.ID)
if err != nil {
return record, err
}
_, err = p.client.Domains.DeleteRecord(ctx, zone, id)
if err != nil {
return record, err
}
return record, nil
}
func (p *Provider) updateDNSEntry(ctx context.Context, zone string, record libdns.Record) (libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.getClient()
id, err := strconv.Atoi(record.ID)
if err != nil {
return record, err
}
entry := godo.DomainRecordEditRequest{
Name: strings.Trim(strings.ReplaceAll(record.Name, zone, ""), "."),
Data: record.Value,
Type: record.Type,
TTL: int(record.TTL.Seconds()),
}
_, _, err = p.client.Domains.EditRecord(ctx, zone, id, &entry)
if err != nil {
return record, err
}
return record, nil
}