-
Notifications
You must be signed in to change notification settings - Fork 1
/
provider.go
81 lines (71 loc) · 2.02 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
package acme
import (
"context"
"fmt"
"sync"
"github.com/libdns/libdns"
)
type RecordStore struct {
entries []libdns.Record
}
type Provider struct {
sync.Mutex
recordMap map[string]*RecordStore
}
func (p *Provider) getZoneRecords(ctx context.Context, zoneName string) *RecordStore {
records, found := p.recordMap[zoneName]
if !found {
return nil
}
return records
}
func compareRecords(a, b libdns.Record) bool {
return a.Type == b.Type && a.Name == b.Name && a.Value == b.Value && a.TTL == b.TTL
}
func (r *RecordStore) deleteRecords(recs []libdns.Record) []libdns.Record {
deletedRecords := []libdns.Record{}
for i, entry := range r.entries {
for _, record := range recs {
if compareRecords(entry, record) {
deletedRecords = append(deletedRecords, record)
r.entries = append(r.entries[:i], r.entries[i+1:]...)
}
}
}
return deletedRecords
}
func (p *Provider) AppendRecords(ctx context.Context, zoneName string, recs []libdns.Record) ([]libdns.Record, error) {
p.Lock()
defer p.Unlock()
zoneRecordStore := p.getZoneRecords(ctx, zoneName)
if zoneRecordStore == nil {
zoneRecordStore = new(RecordStore)
p.recordMap[zoneName] = zoneRecordStore
}
zoneRecordStore.entries = append(zoneRecordStore.entries, recs...)
return zoneRecordStore.entries, nil
}
func (p *Provider) DeleteRecords(ctx context.Context, zoneName string, recs []libdns.Record) ([]libdns.Record, error) {
p.Lock()
defer p.Unlock()
zoneRecordStore := p.getZoneRecords(ctx, zoneName)
if zoneRecordStore == nil {
return nil, nil
}
deletedRecords := zoneRecordStore.deleteRecords(recs)
return deletedRecords, nil
}
func (p *Provider) GetRecords(ctx context.Context, zoneName string) ([]libdns.Record, error) {
p.Lock()
defer p.Unlock()
records := p.getZoneRecords(ctx, zoneName)
if records == nil {
return nil, fmt.Errorf("no records were found for %v", zoneName)
}
return records.entries, nil
}
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)