-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
188 lines (171 loc) · 5.06 KB
/
client.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
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
package alidns
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
)
// mClient is an abstration of AliClient
type mClient struct {
aClient *aliClient
mutex sync.Mutex
}
func (p *Provider) getClient() error {
return p.getClientWithZone("")
}
func (p *Provider) getClientWithZone(zone string) error {
cred := newCredInfo(p.AccKeyID, p.AccKeySecret, p.RegionID)
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)
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) (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 len(recType) > 0 {
p.client.aClient.addReqBody("TypeKeyWord", recType[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)
}
// TODO:Will complete,If we need to get Domain Info for something.
func (c *mClient) getDomainInfo(ctx context.Context, zone string) error {
return nil
}
func (c *mClient) doAPIRequest(ctx context.Context, method string, result interface{}) error {
req, err := c.applyReq(ctx, method, nil)
if err != nil {
return err
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
var buf []byte
buf, err = ioutil.ReadAll(rsp.Body)
strBody := string(buf)
if err != nil {
return err
}
err = json.Unmarshal([]byte(strBody), result)
if err != nil {
return err
}
if rsp.StatusCode != 200 {
return fmt.Errorf("get error status: HTTP %d: %+v", rsp.StatusCode, result.(*aliResult).Msg)
}
c.aClient = nil
return err
}