-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
161 lines (133 loc) · 4.49 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
// Package libdnstemplate implements a DNS record management client compatible
// with the libdns interfaces for regfish.
package regfish
import (
"context"
"fmt"
"sync"
"time"
"github.com/libdns/libdns"
rfns "github.com/regfish/regfish-dnsapi-go"
)
// Provider facilitates DNS record manipulation with regfish.
type Provider struct {
APIToken string
client rfns.Client
once sync.Once
mutex sync.Mutex
}
// 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()
p.init(ctx)
records, err := p.client.GetRecordsByDomain(zone)
if err != nil {
return nil, fmt.Errorf("failed to get records for zone %s: %w", zone, err)
}
var libdnsRecords []libdns.Record
for _, rec := range records {
libdnsRecords = append(libdnsRecords, libdns.Record{
ID: fmt.Sprintf("%d", rec.ID),
Type: rec.Type,
Name: libdns.RelativeName(rec.Name[:len(rec.Name)-1], zone),
Value: rec.Data,
TTL: time.Duration(rec.TTL) * time.Second,
Priority: getPriority(rec.Priority),
})
}
return libdnsRecords, 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()
p.init(ctx)
var createdRecords []libdns.Record
for _, record := range records {
rec := rfns.Record{
Name: p.fqdn(record.Name, zone),
Type: record.Type,
Data: record.Value,
TTL: int(record.TTL.Seconds()),
Priority: &record.Priority,
}
createdRec, err := p.client.CreateRecord(rec)
if err != nil {
return nil, fmt.Errorf("failed to create record %s: %w", record.Name, err)
}
createdRecords = append(createdRecords, libdns.Record{
ID: fmt.Sprintf("%d", createdRec.ID),
Type: createdRec.Type,
Name: libdns.RelativeName(createdRec.Name, zone),
Value: createdRec.Data,
TTL: time.Duration(createdRec.TTL) * time.Second,
Priority: getPriority(createdRec.Priority),
})
}
return createdRecords, 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()
p.init(ctx)
var updatedRecords []libdns.Record
for _, record := range records {
// Map libdns.Record to rfns.Record
// Attempt to update the record using the client
updateRec, err := p.upsertRecord(record, zone)
if err != nil {
return nil, fmt.Errorf("failed to update record %s: %w", record.Name, err)
}
// Map updated rfns.Record to libdns.Record and append to the result slice
updatedRecords = append(updatedRecords, libdns.Record{
ID: fmt.Sprintf("%d", updateRec.ID),
Type: updateRec.Type,
Name: libdns.RelativeName(updateRec.Name, zone),
Value: updateRec.Data,
TTL: time.Duration(updateRec.TTL) * time.Second,
Priority: getPriority(updateRec.Priority),
})
}
return updatedRecords, 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()
p.init(ctx)
all_records, err := p.client.GetRecordsByDomain(zone)
if err != nil {
return nil, fmt.Errorf("failed to get records for zone %s: %w", zone, err)
}
var rrid int
var deletedRecords []libdns.Record
for _, record := range records {
// Find the record ID
rrid = 0
for _, rec := range all_records {
if fmt.Sprintf("%d", rec.ID) == record.ID || (p.fqdn(rec.Name, zone) == p.fqdn(record.Name, zone) && rec.Type == record.Type && rec.Data == record.Value) {
rrid = rec.ID
break
}
}
if rrid == 0 {
return nil, fmt.Errorf("record %s of type %s with data %s not found", record.Name, record.Type, record.Value)
}
err := p.client.DeleteRecord(rrid)
if err != nil {
return nil, fmt.Errorf("failed to delete record ID %d: %w", rrid, err)
}
deletedRecords = append(deletedRecords, record)
}
return deletedRecords, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)