forked from libdns/alidns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
252 lines (234 loc) · 7.38 KB
/
Copy pathprovider.go
File metadata and controls
252 lines (234 loc) · 7.38 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package alidns
import (
"context"
"errors"
"fmt"
"strings"
"github.com/libdns/libdns"
)
// Provider implements the libdns interfaces for Alicloud.
type Provider struct {
client mClient
// The API Key ID Required by Aliyun's for accessing the Aliyun's API
AccKeyID string `json:"access_key_id"`
// The API Key Secret Required by Aliyun's for accessing the Aliyun's API
AccKeySecret string `json:"access_key_secret"`
// Optional for identifing the region of the Aliyun's Service,The default is zh-hangzhou
RegionID string `json:"region_id,omitempty"`
// Your instance RAM role (https://www.alibabacloud.com/help/doc-detail/54579.htm)
RAMRole string `json:"ram_role,omitempty"`
// STS Security Token (optional)
SecurityToken string `json:"security_token,omitempty"`
}
// 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
for _, rec := range recs {
ar := alidnsRecord(rec, zone)
rid, err := p.addDomainRecord(ctx, ar)
if err != nil {
return nil, err
}
ar.RecID = rid
rls = append(rls, ar.LibdnsRecord())
}
return rls, nil
}
// 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
for _, rec := range recs {
ar := alidnsRecord(rec, zone)
if ar.RecID == "" {
r0, err := p.queryDomainRecord(ctx, ar.Rr, ar.DName, ar.DTyp, ar.DVal)
if err != nil {
return nil, err
}
ar.RecID = r0.RecID
}
_, err := p.delDomainRecord(ctx, ar)
if err != nil {
return nil, err
}
rls = append(rls, ar.LibdnsRecord())
}
return rls, nil
}
// 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, err
}
for _, rec := range recs {
rls = append(rls, rec.LibdnsRecord())
}
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 err error
for _, rec := range recs {
ar := alidnsRecord(rec, zone)
if ar.RecID == "" {
r0, err := p.queryDomainRecord(ctx, ar.Rr, ar.DName, ar.DTyp, ar.DVal)
if err != nil {
ar.RecID, err = p.addDomainRecord(ctx, ar)
if err != nil {
return nil, err
}
} else {
ar.RecID = r0.RecID
}
} else {
_, err = p.setDomainRecord(ctx, ar)
if err != nil {
return nil, err
}
}
rls = append(rls, ar.LibdnsRecord())
}
return rls, nil
}
func (p *Provider) getClient() error {
return p.getClientWithZone("")
}
func (p *Provider) getClientWithZone(zone string) error {
cred := newCredInfo(p.AccKeyID, p.AccKeySecret, p.RegionID, p.RAMRole, p.SecurityToken)
return p.client.getAliClient(cred, zone)
}
func (p *Provider) addDomainRecord(ctx context.Context, rc aliDomaRecord) (recID string, err error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
p.getClientWithZone(rc.DName)
if rc.TTL <= 0 {
rc.TTL = 600
}
p.client.aClient.addReqBody("Action", "AddDomainRecord")
p.client.aClient.addReqBody("DomainName", rc.DName)
p.client.aClient.addReqBody("RR", rc.Rr)
p.client.aClient.addReqBody("Type", rc.DTyp)
p.client.aClient.addReqBody("Value", rc.DVal)
p.client.aClient.addReqBody("TTL", fmt.Sprintf("%d", rc.TTL))
rs := aliResult{}
err = p.doAPIRequest(ctx, &rs)
recID = rs.RecID
if err != nil {
return "", err
}
return recID, err
}
func (p *Provider) delDomainRecord(ctx context.Context, rc aliDomaRecord) (recID string, err error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
p.getClient()
p.client.aClient.addReqBody("Action", "DeleteDomainRecord")
p.client.aClient.addReqBody("RecordId", rc.RecID)
rs := aliResult{}
err = p.doAPIRequest(ctx, &rs)
recID = rs.RecID
if err != nil {
return "", err
}
return recID, err
}
func (p *Provider) setDomainRecord(ctx context.Context, rc aliDomaRecord) (recID string, err error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
p.getClientWithZone(rc.DName)
if rc.TTL <= 0 {
rc.TTL = 600
}
p.client.aClient.addReqBody("Action", "UpdateDomainRecord")
p.client.aClient.addReqBody("RecordId", rc.RecID)
p.client.aClient.addReqBody("RR", rc.Rr)
p.client.aClient.addReqBody("Type", rc.DTyp)
p.client.aClient.addReqBody("Value", rc.DVal)
p.client.aClient.addReqBody("TTL", fmt.Sprintf("%d", rc.TTL))
rs := aliResult{}
err = p.doAPIRequest(ctx, &rs)
recID = rs.RecID
if err != nil {
return "", err
}
return recID, err
}
func (p *Provider) getDomainRecord(ctx context.Context, recID string) (aliDomaRecord, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
p.getClient()
p.client.aClient.addReqBody("Action", "DescribeDomainRecordInfo")
p.client.aClient.addReqBody("RecordId", recID)
rs := aliResult{}
err := p.doAPIRequest(ctx, &rs)
rec := rs.ToDomaRecord()
if err != nil {
return aliDomaRecord{}, err
}
return rec, err
}
func (p *Provider) queryDomainRecords(ctx context.Context, name string) ([]aliDomaRecord, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
p.getClient()
p.client.aClient.addReqBody("Action", "DescribeDomainRecords")
p.client.aClient.addReqBody("DomainName", strings.Trim(name, "."))
rs := aliResult{}
err := p.doAPIRequest(ctx, &rs)
if err != nil {
return []aliDomaRecord{}, err
}
return rs.DRecords.Record, err
}
func (p *Provider) queryDomainRecord(ctx context.Context, rr, name string, recType string, recVal ...string) (aliDomaRecord, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
p.getClient()
p.client.aClient.addReqBody("Action", "DescribeDomainRecords")
p.client.aClient.addReqBody("DomainName", strings.Trim(name, "."))
p.client.aClient.addReqBody("RRKeyWord", rr)
if recType != "" {
p.client.aClient.addReqBody("TypeKeyWord", recType)
}
if len(recVal) > 0 && recVal[0] != "" {
p.client.aClient.addReqBody("ValueKeyWord", recVal[0])
}
p.client.aClient.addReqBody("SearchMode", "ADVANCED")
rs := aliResult{}
err := p.doAPIRequest(ctx, &rs)
if err != nil {
return aliDomaRecord{}, err
}
if len(rs.DRecords.Record) == 0 {
return aliDomaRecord{}, errors.New("the Record Name of the domain not found")
}
return rs.DRecords.Record[0], err
}
// REVERSED:queryMainDomain rseserved for absolute names to name,zone
func (p *Provider) queryMainDomain(ctx context.Context, name string) (string, string, error) {
p.client.mutex.Lock()
defer p.client.mutex.Unlock()
p.getClient()
p.client.aClient.addReqBody("Action", "GetMainDomainName")
p.client.aClient.addReqBody("InputString", strings.Trim(name, "."))
rs := aliResult{}
err := p.doAPIRequest(ctx, &rs)
if err != nil {
return "", "", err
}
return rs.Rr, rs.DName, err
}
func (p *Provider) doAPIRequest(ctx context.Context, result interface{}) error {
return p.client.doAPIRequest(ctx, "GET", result)
}
// Interface guards
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)