-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprovider.go
More file actions
121 lines (102 loc) · 2.77 KB
/
provider.go
File metadata and controls
121 lines (102 loc) · 2.77 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package timeweb
import (
"context"
"net/http"
"github.com/libdns/libdns"
)
type Provider struct {
ApiURL string
ApiToken string
}
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
zone = normalizeZone(zone)
reqURL := p.v1("domains/%s/user-records", zone)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return nil, err
}
var result RecordsResponse
err = p.doAPIRequest(req, &result)
recs := make([]libdns.Record, 0, len(result.DNSRecords))
for _, r := range result.DNSRecords {
recs = append(recs, r.libDNSRecord())
}
return recs, err
}
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = normalizeZone(zone)
var created []libdns.Record
for _, record := range records {
result, err := p.createRecord(ctx, zone, record)
if err != nil {
return nil, err
}
created = append(created, result)
}
return created, nil
}
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = normalizeZone(zone)
var deleted []libdns.Record
for _, record := range records {
err := p.deleteRecord(ctx, zone, record)
if err != nil {
return nil, err
}
deleted = append(deleted, record)
}
return deleted, nil
}
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = normalizeZone(zone)
zoneRecords, err := p.GetRecords(ctx, zone)
if err != nil {
return nil, err
}
var results []libdns.Record
var resultErr error
for _, libRecord := range records {
exists := isRecordExists(zoneRecords, libRecord)
if exists {
record, err := p.updateRecord(ctx, zone, libRecord)
if err != nil {
resultErr = err
}
results = append(results, record)
} else {
record, err := p.createRecord(ctx, zone, libRecord)
if err != nil {
resultErr = err
}
results = append(results, record)
}
}
return results, resultErr
}
func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error) {
reqURL := p.v1("domains")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, reqURL, nil)
if err != nil {
return nil, err
}
var result DomainsResponse
err = p.doAPIRequest(req, &result)
if err != nil {
return nil, err
}
zones := make([]libdns.Zone, 0, len(result.Domains))
for _, domain := range result.Domains {
if domain.FQDN == "" {
continue
}
zones = append(zones, libdns.Zone{Name: domain.FQDN})
}
return zones, nil
}
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
_ libdns.ZoneLister = (*Provider)(nil)
)