-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider.go
117 lines (95 loc) · 2.94 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
package dreamhost
import (
"context"
"sync"
"github.com/adamantal/go-dreamhost/api"
"github.com/libdns/libdns"
)
// Provider facilitates DNS record manipulation with Dreamhost.
type Provider struct {
APIKey string `json:"api_key,omitempty"`
client api.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()
err := p.init()
if err != nil {
return nil, err
}
var records []libdns.Record
apiRecords, err := p.client.ListDNSRecords(ctx)
if err != nil {
return nil, err
}
// translate each Dreamhost Domain Record to a libdns Record
for _, rec := range apiRecords {
if rec.Zone == zone {
records = append(records, recordFromApiDnsRecord(rec))
}
}
return records, nil
}
func (p *Provider) addDNSRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
var createdRecords []libdns.Record
for _, record := range records {
apiInputRecord := apiDnsRecordInputFromRecord(record, zone)
err := p.client.AddDNSRecord(ctx, apiInputRecord)
if err == nil {
createdRecords = append(createdRecords, record)
}
}
return createdRecords, nil
}
func (p *Provider) removeDNSRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
var deletedRecords []libdns.Record
for _, record := range records {
apiInputRecord := apiDnsRecordInputFromRecord(record, zone)
err := p.client.RemoveDNSRecord(ctx, apiInputRecord)
if err == nil {
deletedRecords = append(deletedRecords, record)
}
}
return deletedRecords, 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()
err := p.init()
if err != nil {
return nil, err
}
return p.addDNSRecords(ctx, zone, records)
}
// 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()
err := p.init()
if err != nil {
return nil, err
}
return p.addDNSRecords(ctx, zone, records)
}
// 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()
err := p.init()
if err != nil {
return nil, err
}
return p.removeDNSRecords(ctx, zone, records)
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)