-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
105 lines (92 loc) · 2.49 KB
/
Copy pathprovider.go
File metadata and controls
105 lines (92 loc) · 2.49 KB
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
package edgeone
import (
"context"
"github.com/libdns/libdns"
)
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
zoneId, err := p.getZoneId(ctx, zone)
if err != nil {
return nil, err
}
return p.listRecords(ctx, zoneId, zone)
}
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zoneId, err := p.getZoneId(ctx, zone)
if err != nil {
return nil, err
}
for _, record := range records {
r := edgeOneRecord(zone, record)
if err := p.createDnsRecord(ctx, zoneId, r); err != nil {
return nil, err
}
}
return records, nil
}
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zoneId, err := p.getZoneId(ctx, zone)
if err != nil {
return nil, err
}
recordMap := make(map[string]libdns.Record)
p_matches := make(map[string]int)
matches := make(map[string][]string)
for _, record := range records {
r := edgeOneRecord(zone, record)
if _, ok := matches[r.Name]; !ok {
matches[r.Name], err = p.findRecord(ctx, zoneId, r, false)
p_matches[r.Name] = len(matches[r.Name])
}
if err != nil {
return nil, err
}
if p_matches[r.Name] > 0 {
p_matches[r.Name] = p_matches[r.Name] - 1
recordMap[matches[r.Name][p_matches[r.Name]]] = record
} else if err = p.createDnsRecord(ctx, zoneId, r); err != nil {
return nil, err
}
}
if err = p.modifyDnsRecords(ctx, zoneId, zone, recordMap); err != nil {
return nil, err
}
return records, nil
}
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zoneId, err := p.getZoneId(ctx, zone)
if err != nil {
return nil, err
}
var ids []string
matches := make(map[string][]string)
for _, record := range records {
r := edgeOneRecord(zone, record)
item := r.Name
matchContent := record.RR().Data != ""
if matchContent {
item = r.Name + r.Content
}
if _, ok := matches[item]; !ok {
matches[item], err = p.findRecord(ctx, zoneId, r, matchContent)
}
if err != nil {
return nil, err
}
}
for s := range matches {
ids = append(ids, matches[s]...)
}
if len(ids) > 0 {
if err := p.deleteDnsRecords(ctx, zoneId, ids); err != nil {
return nil, err
}
}
return records, nil
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)