-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
396 lines (362 loc) · 11 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Package aci is a Cisco ACI REST client library for Go.
package aci
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"math"
"math/rand"
"net/http"
"net/http/cookiejar"
"strings"
"sync"
"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 ACI client.
// Use aci.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
// Url is the APIC IP or hostname, e.g. https://10.0.0.1:80 (port is optional).
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 APIC username.
Usr string
// Pwd is the APIC 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
// Authentication mutex
AuthenticationMutex *sync.Mutex
// Enable debug logging
Logging bool
}
// NewClient creates a new ACI HTTP client.
// Pass modifiers in to modify the behavior of the client, e.g.
//
// client, _ := NewClient("https://1.1.1.1", "user", "password", RequestTimeout(120))
func NewClient(url, usr, pwd string, mods ...func(*Client)) (Client, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
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,
MaxRetries: DefaultMaxRetries,
BackoffMinDelay: DefaultBackoffMinDelay,
BackoffMaxDelay: DefaultBackoffMaxDelay,
BackoffDelayFactor: DefaultBackoffDelayFactor,
AuthenticationMutex: &sync.Mutex{},
Logging: false,
}
for _, mod := range mods {
mod(&client)
}
return client, nil
}
// Insecure determines if insecure https connections are allowed. Default value is true.
func Insecure(x bool) func(*Client) {
return func(client *Client) {
client.HttpClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = x
}
}
// 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
}
}
// Logging enables debug logging. Default value is false.
func Logging(x bool) func(*Client) {
return func(client *Client) {
client.Logging = 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/class/fvBD", 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, _ = io.ReadAll(req.HttpReq.Body)
}
var res Res
for attempts := 0; ; attempts++ {
req.HttpReq.Body = io.NopCloser(bytes.NewBuffer(body))
if req.LogPayload && client.Logging {
log.Printf("[DEBUG] HTTP Request: %s, %s, %s", req.HttpReq.Method, req.HttpReq.URL, req.HttpReq.Body)
} else if client.Logging {
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 {
if client.Logging {
log.Printf("[ERROR] HTTP Connection error occured: %+v", err)
log.Printf("[DEBUG] Exit from Do method")
}
return Res{}, err
} else {
if client.Logging {
log.Printf("[ERROR] HTTP Connection failed: %s, retries: %v", err, attempts)
}
continue
}
}
defer httpRes.Body.Close()
bodyBytes, err := io.ReadAll(httpRes.Body)
if err != nil {
if ok := client.Backoff(attempts); !ok {
if client.Logging {
log.Printf("[ERROR] Cannot decode response body: %+v", err)
log.Printf("[DEBUG] Exit from Do method")
}
return Res{}, err
} else {
if client.Logging {
log.Printf("[ERROR] Cannot decode response body: %s, retries: %v", err, attempts)
}
continue
}
}
res = Res(gjson.ParseBytes(bodyBytes))
if req.LogPayload && client.Logging {
log.Printf("[DEBUG] HTTP Response: %s", res.Raw)
}
if (httpRes.StatusCode < 500 || httpRes.StatusCode > 504) && httpRes.StatusCode != 405 {
if client.Logging {
log.Printf("[DEBUG] Exit from Do method")
}
break
} else {
if ok := client.Backoff(attempts); !ok {
if client.Logging {
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 {
if client.Logging {
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 != "" {
if client.Logging {
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 APIC, wrapped in imdata, e.g.
//
// {
// "imdata": [
// {
// "fvTenant": {
// "attributes": {
// "dn": "uni/tn-mytenant",
// "name": "mytenant",
// }
// }
// }
// ],
// "totalCount": "1"
// }
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.
//
// [
// {
// "fvTenant": {
// "attributes": {
// "dn": "uni/tn-mytenant",
// "name": "mytenant",
// }
// }
// }
// ]
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.
//
// {
// "fvTenant": {
// "attributes": {
// "dn": "uni/tn-mytenant",
// "name": "mytenant",
// }
// }
// }
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)
}
// Login authenticates to the APIC.
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 aci.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 {
var err error
client.AuthenticationMutex.Lock()
if client.Token == "" {
err = client.Login()
} else if time.Since(client.LastRefresh) > 480*time.Second {
err = client.Refresh()
}
client.AuthenticationMutex.Unlock()
return err
}
// Backoff waits following an exponential backoff algorithm
func (client *Client) Backoff(attempts int) bool {
if client.Logging {
log.Printf("[DEBUG] Begining backoff method: attempts %v on %v", attempts, client.MaxRetries)
}
if attempts >= client.MaxRetries {
if client.Logging {
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)
if client.Logging {
log.Printf("[TRACE] Starting sleeping for %v", backoffDuration.Round(time.Second))
}
time.Sleep(backoffDuration)
if client.Logging {
log.Printf("[DEBUG] Exit from backoff method with return value true")
}
return true
}