5
5
"context"
6
6
"encoding/xml"
7
7
"errors"
8
- "fmt"
9
8
"net/http"
10
9
"net/url"
11
10
"strconv"
@@ -14,14 +13,11 @@ import (
14
13
"golang.org/x/oauth2"
15
14
)
16
15
17
- const (
18
- defaultBaseURL = "https://api.nic.ru"
19
- TokenURL = defaultBaseURL + "/oauth/token"
20
- )
16
+ const defaultBaseURL = "https://api.nic.ru"
21
17
22
- const OAuth2Scope = ".+:/dns-master/.+ "
18
+ const tokenURL = defaultBaseURL + "/oauth/token "
23
19
24
- const SuccessStatus = "success"
20
+ const successStatus = "success"
25
21
26
22
// OauthConfiguration credentials.
27
23
type OauthConfiguration struct {
@@ -43,10 +39,10 @@ func NewOauthClient(config *OauthConfiguration) (*http.Client, error) {
43
39
ClientID : config .OAuth2ClientID ,
44
40
ClientSecret : config .OAuth2SecretID ,
45
41
Endpoint : oauth2.Endpoint {
46
- TokenURL : TokenURL ,
42
+ TokenURL : tokenURL ,
47
43
AuthStyle : oauth2 .AuthStyleInParams ,
48
44
},
49
- Scopes : []string {OAuth2Scope },
45
+ Scopes : []string {".+:/dns-master/.+" },
50
46
}
51
47
52
48
oauth2Token , err := oauth2Config .PasswordCredentialsToken (ctx , config .Username , config .Password )
@@ -95,22 +91,14 @@ func (c *Client) GetZones() ([]*Zone, error) {
95
91
return nil , err
96
92
}
97
93
98
- buf := bytes .NewBuffer (nil )
99
- if _ , err := buf .ReadFrom (response .Body ); err != nil {
100
- return nil , err
101
- }
102
-
103
94
apiResponse := & Response {}
104
- if err := xml .NewDecoder (buf ).Decode (& apiResponse ); err != nil {
105
- return nil , err
106
- }
107
95
108
- var zones [] * Zone
109
- for _ , zone := range apiResponse . Data . Zone {
110
- zones = append ( zones , zone )
96
+ err = xml . NewDecoder ( response . Body ). Decode ( apiResponse )
97
+ if err != nil {
98
+ return nil , err
111
99
}
112
100
113
- return zones , nil
101
+ return apiResponse . Data . Zone , nil
114
102
}
115
103
116
104
func (c * Client ) GetRecords (fqdn string ) ([]* RR , error ) {
@@ -126,13 +114,10 @@ func (c *Client) GetRecords(fqdn string) ([]*RR, error) {
126
114
return nil , err
127
115
}
128
116
129
- buf := bytes .NewBuffer (nil )
130
- if _ , err := buf .ReadFrom (response .Body ); err != nil {
131
- return nil , err
132
- }
133
-
134
117
apiResponse := & Response {}
135
- if err := xml .NewDecoder (buf ).Decode (& apiResponse ); err != nil {
118
+
119
+ err = xml .NewDecoder (response .Body ).Decode (apiResponse )
120
+ if err != nil {
136
121
return nil , err
137
122
}
138
123
@@ -150,14 +135,14 @@ func (c *Client) GetTXTRecords(fqdn string) ([]*Txt, error) {
150
135
return nil , err
151
136
}
152
137
153
- txt := make ( []* Txt , 0 )
138
+ var txtRecords []* Txt
154
139
for _ , record := range records {
155
140
if record .Txt != nil {
156
- txt = append (txt , record .Txt )
141
+ txtRecords = append (txtRecords , record .Txt )
157
142
}
158
143
}
159
144
160
- return txt , nil
145
+ return txtRecords , nil
161
146
}
162
147
163
148
func (c * Client ) AddTxtRecord (zoneName string , name string , content string , ttl int ) (* Response , error ) {
@@ -171,8 +156,8 @@ func (c *Client) AddTxtRecord(zoneName string, name string, content string, ttl
171
156
return c .add (zoneName , request )
172
157
}
173
158
174
- func (c * Client ) DeleteRecord (zoneName string , id int ) (* Response , error ) {
175
- endpoint := c .baseURL .JoinPath ("dns-master" , "services" , c .serviceName , "zones" , zoneName , strconv . Itoa ( id ) )
159
+ func (c * Client ) DeleteRecord (zoneName string , id string ) (* Response , error ) {
160
+ endpoint := c .baseURL .JoinPath ("dns-master" , "services" , c .serviceName , "zones" , zoneName , "records" , id )
176
161
177
162
req , err := http .NewRequest (http .MethodDelete , endpoint .String (), nil )
178
163
if err != nil {
@@ -184,16 +169,18 @@ func (c *Client) DeleteRecord(zoneName string, id int) (*Response, error) {
184
169
return nil , err
185
170
}
186
171
187
- apiResponse := Response {}
188
- if err := xml .NewDecoder (response .Body ).Decode (& apiResponse ); err != nil {
172
+ apiResponse := & Response {}
173
+
174
+ err = xml .NewDecoder (response .Body ).Decode (apiResponse )
175
+ if err != nil {
189
176
return nil , err
190
177
}
191
178
192
- if apiResponse .Status != SuccessStatus {
179
+ if apiResponse .Status != successStatus {
193
180
return nil , err
194
181
}
195
182
196
- return & apiResponse , nil
183
+ return apiResponse , nil
197
184
}
198
185
199
186
func (c * Client ) CommitZone (zoneName string ) (* Response , error ) {
@@ -209,27 +196,29 @@ func (c *Client) CommitZone(zoneName string) (*Response, error) {
209
196
return nil , err
210
197
}
211
198
212
- apiResponse := Response {}
213
- if err := xml .NewDecoder (response .Body ).Decode (& apiResponse ); err != nil {
199
+ apiResponse := & Response {}
200
+
201
+ err = xml .NewDecoder (response .Body ).Decode (apiResponse )
202
+ if err != nil {
214
203
return nil , err
215
204
}
216
205
217
- if apiResponse .Status != SuccessStatus {
206
+ if apiResponse .Status != successStatus {
218
207
return nil , err
219
208
}
220
209
221
- return & apiResponse , nil
210
+ return apiResponse , nil
222
211
}
223
212
224
213
func (c * Client ) add (zoneName string , request * Request ) (* Response , error ) {
225
214
endpoint := c .baseURL .JoinPath ("dns-master" , "services" , c .serviceName , "zones" , zoneName , "records" )
226
215
227
- buf := bytes .NewBuffer ( nil )
228
- if err := xml .NewEncoder (buf ).Encode (request ); err != nil {
216
+ body := & bytes.Buffer {}
217
+ if err := xml .NewEncoder (body ).Encode (request ); err != nil {
229
218
return nil , err
230
219
}
231
220
232
- req , err := http .NewRequest (http .MethodPut , endpoint .String (), buf )
221
+ req , err := http .NewRequest (http .MethodPut , endpoint .String (), body )
233
222
if err != nil {
234
223
return nil , err
235
224
}
@@ -239,46 +228,18 @@ func (c *Client) add(zoneName string, request *Request) (*Response, error) {
239
228
return nil , err
240
229
}
241
230
242
- buf = bytes .NewBuffer (nil )
243
- if _ , err := buf .ReadFrom (response .Body ); err != nil {
244
- return nil , err
245
- }
246
-
247
231
apiResponse := & Response {}
248
- if err := xml .NewDecoder (buf ).Decode (& apiResponse ); err != nil {
249
- return nil , err
250
- }
251
-
252
- if apiResponse .Status != SuccessStatus {
253
- return nil , fmt .Errorf (describeError (apiResponse .Errors .Error ))
254
- }
255
-
256
- return apiResponse , nil
257
- }
258
-
259
- func (c * Client ) deleteRecord (zoneName string , id int ) (* Response , error ) {
260
- endpoint := c .baseURL .JoinPath ("dns-master" , "services" , c .serviceName , "zones" , zoneName , "records" , strconv .Itoa (id ))
261
232
262
- req , err := http . NewRequest ( http . MethodDelete , endpoint . String (), nil )
233
+ err = xml . NewDecoder ( response . Body ). Decode ( apiResponse )
263
234
if err != nil {
264
235
return nil , err
265
236
}
266
237
267
- response , err := c .httpClient .Do (req )
268
- if err != nil {
269
- return nil , err
238
+ if apiResponse .Status != successStatus {
239
+ return nil , errors .New (describeError (apiResponse .Errors .Error ))
270
240
}
271
241
272
- apiResponse := Response {}
273
- if err := xml .NewDecoder (response .Body ).Decode (& apiResponse ); err != nil {
274
- return nil , err
275
- }
276
-
277
- if apiResponse .Status != SuccessStatus {
278
- return nil , err
279
- }
280
-
281
- return & apiResponse , nil
242
+ return apiResponse , nil
282
243
}
283
244
284
245
func validateAuthOptions (config * OauthConfiguration ) error {
0 commit comments