-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
360 lines (327 loc) · 10.3 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Package nxos is a a Cisco NXOS NX-API REST client library for Go.
package nxos
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"net/http"
"net/http/cookiejar"
"strings"
"time"
"github.com/tidwall/gjson"
)
const DefaultMaxRetries int = 3
const DefaultBackoffMinDelay int = 4
const DefaultBackoffMaxDelay int = 60
const DefaultBackoffDelayFactor float64 = 3
// Client is an HTTP NXOS NX-API client.
// Use nxos.NewClient to initiate a client.
// This will ensure proper cookie handling and processing of modifiers.
type Client struct {
// HttpClient is the *http.Client used for API requests.
HttpClient *http.Client
// List of URLs.
Url string
// LastRefresh is the timestamp of the last token refresh interval.
LastRefresh time.Time
// Token is the current authentication token
Token string
// Usr is the NXOS device username.
Usr string
// Pwd is the NXOS device password.
Pwd string
// Insecure determines if insecure https connections are allowed.
Insecure bool
// Maximum number of retries
MaxRetries int
// Minimum delay between two retries
BackoffMinDelay int
// Maximum delay between two retries
BackoffMaxDelay int
// Backoff delay factor
BackoffDelayFactor float64
}
// NewClient creates a new NXOS HTTP client.
// Pass modifiers in to modify the behavior of the client, e.g.
//
// client, _ := NewClient("apic", "user", "password", true, RequestTimeout(120))
func NewClient(url, usr, pwd string, insecure bool, mods ...func(*Client)) (Client, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
cookieJar, _ := cookiejar.New(nil)
httpClient := http.Client{
Timeout: 60 * time.Second,
Transport: tr,
Jar: cookieJar,
}
client := Client{
HttpClient: &httpClient,
Url: url,
Usr: usr,
Pwd: pwd,
Insecure: insecure,
MaxRetries: DefaultMaxRetries,
BackoffMinDelay: DefaultBackoffMinDelay,
BackoffMaxDelay: DefaultBackoffMaxDelay,
BackoffDelayFactor: DefaultBackoffDelayFactor,
}
for _, mod := range mods {
mod(&client)
}
return client, nil
}
// RequestTimeout modifies the HTTP request timeout from the default of 60 seconds.
func RequestTimeout(x time.Duration) func(*Client) {
return func(client *Client) {
client.HttpClient.Timeout = x * time.Second
}
}
// MaxRetries modifies the maximum number of retries from the default of 3.
func MaxRetries(x int) func(*Client) {
return func(client *Client) {
client.MaxRetries = x
}
}
// BackoffMinDelay modifies the minimum delay between two retries from the default of 4.
func BackoffMinDelay(x int) func(*Client) {
return func(client *Client) {
client.BackoffMinDelay = x
}
}
// BackoffMaxDelay modifies the maximum delay between two retries from the default of 60.
func BackoffMaxDelay(x int) func(*Client) {
return func(client *Client) {
client.BackoffMaxDelay = x
}
}
// BackoffDelayFactor modifies the backoff delay factor from the default of 3.
func BackoffDelayFactor(x float64) func(*Client) {
return func(client *Client) {
client.BackoffDelayFactor = x
}
}
// NewReq creates a new Req request for this client.
func (client Client) NewReq(method, uri string, body io.Reader, mods ...func(*Req)) Req {
httpReq, _ := http.NewRequest(method, client.Url+uri+".json", body)
req := Req{
HttpReq: httpReq,
Refresh: true,
LogPayload: true,
}
for _, mod := range mods {
mod(&req)
}
return req
}
// Do makes a request.
// Requests for Do are built ouside of the client, e.g.
//
// req := client.NewReq("GET", "/api/mo/sys/bgp", nil)
// res, _ := client.Do(req)
func (client *Client) Do(req Req) (Res, error) {
// retain the request body across multiple attempts
var body []byte
if req.HttpReq.Body != nil {
body, _ = ioutil.ReadAll(req.HttpReq.Body)
}
var res Res
for attempts := 0; ; attempts++ {
req.HttpReq.Body = ioutil.NopCloser(bytes.NewBuffer(body))
if req.LogPayload {
log.Printf("[DEBUG] HTTP Request: %s, %s, %s", req.HttpReq.Method, req.HttpReq.URL, req.HttpReq.Body)
} else {
log.Printf("[DEBUG] HTTP Request: %s, %s", req.HttpReq.Method, req.HttpReq.URL)
}
httpRes, err := client.HttpClient.Do(req.HttpReq)
if err != nil {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] HTTP Connection error occured: %+v", err)
log.Printf("[DEBUG] Exit from Do method")
return Res{}, err
} else {
log.Printf("[ERROR] HTTP Connection failed: %s, retries: %v", err, attempts)
continue
}
}
defer httpRes.Body.Close()
bodyBytes, err := ioutil.ReadAll(httpRes.Body)
if err != nil {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] Cannot decode response body: %+v", err)
log.Printf("[DEBUG] Exit from Do method")
return Res{}, err
} else {
log.Printf("[ERROR] Cannot decode response body: %s, retries: %v", err, attempts)
continue
}
}
res = Res(gjson.ParseBytes(bodyBytes))
if req.LogPayload {
log.Printf("[DEBUG] HTTP Response: %s", res.Raw)
}
if (httpRes.StatusCode < 500 || httpRes.StatusCode > 504) && httpRes.StatusCode != 405 {
log.Printf("[DEBUG] Exit from Do method")
break
} else {
if ok := client.Backoff(attempts); !ok {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v", httpRes.StatusCode)
log.Printf("[DEBUG] Exit from Do method")
return Res{}, fmt.Errorf("HTTP Request failed: StatusCode %v", httpRes.StatusCode)
} else {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
}
}
}
errCode := res.Get("imdata.0.error.attributes.code").Str
if errCode != "" {
log.Printf("[ERROR] JSON error: %s", res.Raw)
return res, fmt.Errorf("JSON error: %s", res.Raw)
}
return res, nil
}
// Get makes a GET request and returns a GJSON result.
// Results will be the raw data structure as returned by the NXOS device, wrapped in imdata, e.g.
//
// {
// "totalCount": "1",
// "imdata": [
// {
// "bgpEntity": {
// "attributes": {
// "adminSt": "enabled",
// "dn": "sys/bgp",
// "name": "bgp"
// }
// }
// }
// ]
// }
func (client *Client) Get(path string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("GET", path, nil, mods...)
client.Authenticate()
return client.Do(req)
}
// GetClass makes a GET request by class and unwraps the results.
// Result is removed from imdata, but still wrapped in Class.attributes, e.g.
//
// [
// {
// "bgpEntity": {
// "attributes": {
// "dn": "sys/bgp",
// "name": "bgp",
// }
// }
// }
// ]
func (client *Client) GetClass(class string, mods ...func(*Req)) (Res, error) {
res, err := client.Get(fmt.Sprintf("/api/class/%s", class), mods...)
if err != nil {
return res, err
}
return res.Get("imdata"), nil
}
// GetDn makes a GET request by DN.
// Result is removed from imdata and first result is removed from the list, e.g.
//
// {
// "bgpEntity": {
// "attributes": {
// "dn": "sys/bgp",
// "name": "bgp",
// }
// }
// }
func (client *Client) GetDn(dn string, mods ...func(*Req)) (Res, error) {
res, err := client.Get(fmt.Sprintf("/api/mo/%s", dn), mods...)
if err != nil {
return res, err
}
return res.Get("imdata.0"), nil
}
// DeleteDn makes a DELETE request by DN.
func (client *Client) DeleteDn(dn string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("DELETE", fmt.Sprintf("/api/mo/%s", dn), nil, mods...)
client.Authenticate()
return client.Do(req)
}
// Post makes a POST request and returns a GJSON result.
// Hint: Use the Body struct to easily create POST body data.
func (client *Client) Post(dn, data string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("POST", fmt.Sprintf("/api/mo/%s", dn), strings.NewReader(data), mods...)
client.Authenticate()
return client.Do(req)
}
// Put makes a PUT request and returns a GJSON result.
// Hint: Use the Body struct to easily create PUT body data.
func (client *Client) Put(dn, data string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("PUT", fmt.Sprintf("/api/mo/%s", dn), strings.NewReader(data), mods...)
client.Authenticate()
return client.Do(req)
}
// Login authenticates to the NXOS device.
func (client *Client) Login() error {
data := fmt.Sprintf(`{"aaaUser":{"attributes":{"name":"%s","pwd":"%s"}}}`,
client.Usr,
client.Pwd,
)
req := client.NewReq("POST", "/api/aaaLogin", strings.NewReader(data), NoRefresh, NoLogPayload)
res, err := client.Do(req)
if err != nil {
return err
}
client.Token = res.Get("imdata.0.aaaLogin.attributes.token").Str
client.LastRefresh = time.Now()
return nil
}
// Refresh refreshes the authentication token.
// Note that this will be handled automatically be default.
// Refresh will be checked every request and the token will be refreshed after 8 minutes.
// Pass nxos.NoRefresh to prevent automatic refresh handling and handle it directly instead.
func (client *Client) Refresh() error {
res, err := client.Get("/api/aaaRefresh", NoRefresh, NoLogPayload)
if err != nil {
return err
}
client.Token = res.Get("imdata.0.aaaRefresh.attributes.token").Str
client.LastRefresh = time.Now()
return nil
}
// Login if no token available or refresh the token if older than 480 seconds.
func (client *Client) Authenticate() error {
if client.Token == "" {
return client.Login()
} else if time.Since(client.LastRefresh) > 480*time.Second {
return client.Refresh()
} else {
return nil
}
}
// Backoff waits following an exponential backoff algorithm
func (client *Client) Backoff(attempts int) bool {
log.Printf("[DEBUG] Begining backoff method: attempts %v on %v", attempts, client.MaxRetries)
if attempts >= client.MaxRetries {
log.Printf("[DEBUG] Exit from backoff method with return value false")
return false
}
minDelay := time.Duration(client.BackoffMinDelay) * time.Second
maxDelay := time.Duration(client.BackoffMaxDelay) * time.Second
min := float64(minDelay)
backoff := min * math.Pow(client.BackoffDelayFactor, float64(attempts))
if backoff > float64(maxDelay) {
backoff = float64(maxDelay)
}
backoff = (rand.Float64()/2+0.5)*(backoff-min) + min
backoffDuration := time.Duration(backoff)
log.Printf("[TRACE] Starting sleeping for %v", backoffDuration.Round(time.Second))
time.Sleep(backoffDuration)
log.Printf("[DEBUG] Exit from backoff method with return value true")
return true
}