-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathprovider.go
More file actions
181 lines (165 loc) · 4.83 KB
/
provider.go
File metadata and controls
181 lines (165 loc) · 4.83 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package alidns
import (
"context"
"github.com/libdns/libdns"
)
// Provider implements the libdns interfaces for Alicloud.
type Provider struct {
client *aliClient
CredentialInfo
}
// AppendRecords adds records to the zone. It returns the records that were added.
func (p *Provider) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
var rls []libdns.Record
var errs = OpErrors("AppendRecords")
for _, rec := range recs {
ar := alidnsRecord(rec, zone)
rid, err := p.addDomainRecord(ctx, ar)
if err != nil {
errs.JoinRecord(rec, err)
continue
}
ar.RecordID = rid
rls = append(rls, ar.DomainRecord())
}
return rls, errs.Error()
}
// DeleteRecords deletes the records from the zone. If a record does not have an ID,
// it will be looked up. It returns the records that were deleted.
func (p *Provider) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
var rls []libdns.Record
var errs = OpErrors("DeleteRecords")
for _, rec := range recs {
ar := alidnsRecord(rec, zone)
if ar.RecordID == "" {
r0, err := p.queryDomainRecord(ctx, ar.Rr, ar.DomainName, ar.DomainType, ar.DomainValue)
if err != nil {
errs.JoinRecord(rec, err)
continue
}
ar.RecordID = r0.RecordID
}
_, err := p.delDomainRecord(ctx, ar)
if err != nil {
errs.JoinRecord(rec, err)
continue
}
rls = append(rls, ar.DomainRecord())
}
return rls, errs.Error()
}
// GetRecords lists all the records in the zone.
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
var rls []libdns.Record
recs, err := p.queryDomainRecords(ctx, zone)
if err != nil {
return nil, OpError("GetRecords", err)
}
for _, rec := range recs {
rls = append(rls, rec.DomainRecord())
}
return rls, 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, recs []libdns.Record) ([]libdns.Record, error) {
var rls []libdns.Record
var errs = OpErrors("SetRecords")
for _, rec := range recs {
ar := alidnsRecord(rec, zone)
if ar.RecordID == "" {
r0, err := p.queryDomainRecord(ctx, ar.Rr, ar.DomainName, ar.DomainType, ar.DomainValue)
if err == nil {
ar.RecordID = r0.RecordID
}
if ar.Rr == r0.Rr && len(ar.RecordID) > 0 {
_, err := p.delDomainRecord(ctx, ar)
if err != nil {
errs.JoinRecord(rec, err)
continue
}
ar.RecordID = ""
}
if len(ar.RecordID) == 0 {
ar.RecordID, err = p.addDomainRecord(ctx, ar)
if err != nil {
errs.JoinRecord(rec, err)
} else {
rls = append(rls, ar.DomainRecord())
}
continue
}
}
_, err := p.setDomainRecord(ctx, ar)
if err != nil {
errs.JoinRecord(rec, err)
continue
}
rls = append(rls, ar.DomainRecord())
}
return rls, errs.Error()
}
func (p *Provider) getClient() error {
return p.getClientWithZone("")
}
func (p *Provider) getClientWithZone(zone string) error {
var err error
if len(zone) == 0 {
p.client, err = getClient(&p.CredentialInfo)
} else {
p.client, err = getClient(&p.CredentialInfo, zone)
}
if err != nil {
return err
}
return nil
}
func (p *Provider) addDomainRecord(ctx context.Context, rc aliDomainRecord) (recID string, err error) {
err = p.getClientWithZone(rc.DomainName)
if err != nil {
return "", err
}
if !p.client.IsEntprienseEdition() && rc.TTL < 600 {
rc.TTL = 600
}
return p.client.addDomainRecord(ctx, rc)
}
func (p *Provider) delDomainRecord(ctx context.Context, rc aliDomainRecord) (recID string, err error) {
err = p.getClientWithZone(rc.DomainName)
if err != nil {
return "", err
}
if !p.client.IsEntprienseEdition() && rc.TTL < 600 {
rc.TTL = 600
}
return p.client.delDomainRecord(ctx, rc)
}
func (p *Provider) setDomainRecord(ctx context.Context, rc aliDomainRecord) (recID string, err error) {
err = p.getClientWithZone(rc.DomainName)
if err != nil {
return "", err
}
if !p.client.IsEntprienseEdition() && rc.TTL < 600 {
rc.TTL = 600
}
return p.client.setDomainRecord(ctx, rc)
}
func (p *Provider) getDomainRecord(ctx context.Context, recID string) (aliDomainRecord, error) {
p.getClient()
return p.client.getDomainRecord(ctx, recID)
}
func (p *Provider) queryDomainRecords(ctx context.Context, name string) ([]aliDomainRecord, error) {
p.getClient()
return p.client.queryDomainRecords(ctx, name)
}
func (p *Provider) queryDomainRecord(ctx context.Context, rr, name string, recType string, recVal ...string) (aliDomainRecord, error) {
p.getClient()
return p.client.queryDomainRecord(ctx, rr, name, recType, recVal...)
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)