-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
194 lines (178 loc) · 4.58 KB
/
provider.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
package glesys
import (
"context"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/glesys/glesys-go/v3"
"github.com/libdns/libdns"
)
var debug bool
const DEBUG_KEY = "LIBDNS_GLESYS_DEBUG"
func init() {
if b, err := strconv.ParseBool(os.Getenv(DEBUG_KEY)); err == nil && b {
debug = true
}
}
type Provider struct {
mutex sync.Mutex
clientCache *glesys.Client
Project string `json:"project,omitempty"`
ApiKey string `json:"api_key,omitempty"`
}
func (p *Provider) client() *glesys.Client {
if p.clientCache == nil {
p.clientCache = glesys.NewClient(p.Project, p.ApiKey, "libdns-glesys/0.0.1")
}
return p.clientCache
}
func cleanZ(z string) string {
return strings.TrimRight(z, ". ")
}
func gle2lib(dr *glesys.DNSDomainRecord) libdns.Record {
r := libdns.Record{
ID: strconv.Itoa(dr.RecordID),
Type: dr.Type,
Name: dr.Host,
Value: dr.Data,
TTL: time.Duration(dr.TTL) * time.Second,
}
switch dr.Type {
// extract priority
case "MX", "SRV", "URI":
parts := strings.Split(dr.Data, " ")
if p, err := strconv.Atoi(parts[0]); err == nil {
r.Priority = p
r.Value = parts[1]
}
}
return r
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
zone = cleanZ(zone)
if debug {
log.Printf("GetRecords zone=%s", zone)
}
drs, err := p.client().DNSDomains.ListRecords(ctx, zone)
if err != nil {
return nil, err
}
records := make([]libdns.Record, len(*drs))
for i, dr := range *drs {
if zone != dr.DomainName {
return records, fmt.Errorf("unexpected domainname in respose: %v", dr.DomainName)
}
r := gle2lib(&dr)
records[i] = r
}
if debug {
log.Printf("GetRecords result: %+v", records)
}
return records, nil
}
// AppendRecords adds records to the zone. It returns the records that were added.
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
zone = cleanZ(zone)
if debug {
log.Printf("AppendRecords zone=%s", zone)
}
results := []libdns.Record{}
for _, r := range records {
param := glesys.AddRecordParams{
DomainName: zone,
Host: r.Name,
Data: r.Value,
TTL: int(r.TTL / time.Second),
Type: strings.ToUpper(r.Type),
}
if r.Priority > 0 {
param.Data = fmt.Sprintf("%d %s", r.Priority, param.Data)
}
dr, err := p.client().DNSDomains.AddRecord(ctx, param)
if err != nil {
return results, err
}
results = append(results, gle2lib(dr))
}
if debug {
log.Printf("AppendRecords result: %+v", results)
}
return results, nil
}
// SetRecords sets the records in the zone, either by updating existing records or creating new ones.
// It returns the updated records.
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
zone = cleanZ(zone)
if debug {
log.Printf("SetRecords zone=%s", zone)
}
results := []libdns.Record{}
for _, r := range records {
id, err := strconv.Atoi(r.ID)
if err != nil && r.ID != "" {
return results, err
}
param := glesys.UpdateRecordParams{
RecordID: id,
Host: r.Name,
Data: r.Value,
TTL: int(r.TTL / time.Second),
Type: strings.ToUpper(r.Type),
}
if r.Priority > 0 {
param.Data = fmt.Sprintf("%d %s", r.Priority, param.Data)
}
dr, err := p.client().DNSDomains.UpdateRecord(ctx, param)
if err != nil {
return results, err
}
results = append(results, gle2lib(dr))
}
return results, nil
}
// DeleteRecords deletes the records from the zone. It returns the records that were deleted.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
zone = cleanZ(zone)
if debug {
log.Printf("DeleteRecords zone=%s", zone)
}
results := []libdns.Record{}
for _, r := range records {
if r.ID == "" {
return results, fmt.Errorf("record must have ID to delete: %+v", r)
}
id, err := strconv.Atoi(r.ID)
if err != nil {
return results, err
}
err = p.client().DNSDomains.DeleteRecord(ctx, id)
if err != nil {
return results, err
}
results = append(results, r)
}
if debug {
log.Printf("DeleteRecords results: %+v", results)
}
return results, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)