-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
329 lines (298 loc) · 9.97 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
// Package nd is a Cisco Nexus Dashboard REST client library for Go.
package nd
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"log"
"math"
"math/rand"
"net/http"
"net/http/cookiejar"
"strings"
"sync"
"time"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
const DefaultMaxRetries int = 3
const DefaultBackoffMinDelay int = 2
const DefaultBackoffMaxDelay int = 60
const DefaultBackoffDelayFactor float64 = 3
// Client is an HTTP Nexus Dashboard client.
// Use nd.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 Nexus Dashboard IP or hostname, e.g. https://10.0.0.1:443 (port is optional).
Url string
// BasePath is the Nexus Dashboard URL prefix to use, e.g. '/appcenter/cisco/ndfc/api/v1'.
BasePath string
// Token is the current authentication token
Token string
// Usr is the Nexus Dashboard username.
Usr string
// Pwd is the Nexus Dashboard password.
Pwd string
// Domain is the Nexus Dashboard domain.
Domain 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
}
// NewClient creates a new Nexus Dashboard HTTP client.
// Pass modifiers in to modify the behavior of the client, e.g.
//
// client, _ := NewClient("https://10.1.1.1", "/appcenter/cisco/ndfc/api/v1", "user", "password", "", true, RequestTimeout(120))
func NewClient(url, basePath, usr, pwd, domain 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,
}
if domain == "" {
domain = "DefaultAuth"
}
client := Client{
HttpClient: &httpClient,
Url: url,
BasePath: basePath,
Usr: usr,
Pwd: pwd,
Domain: domain,
Insecure: insecure,
MaxRetries: DefaultMaxRetries,
BackoffMinDelay: DefaultBackoffMinDelay,
BackoffMaxDelay: DefaultBackoffMaxDelay,
BackoffDelayFactor: DefaultBackoffDelayFactor,
AuthenticationMutex: &sync.Mutex{},
}
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 2.
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, body)
httpReq.Header.Add("Content-Type", "application/json")
req := Req{
HttpReq: httpReq,
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", "/appcenter/cisco/ndfc/api/v1/lan-fabric/rest/control/fabrics", nil)
// res, _ := client.Do(req)
func (client *Client) Do(req Req) (Res, error) {
// add token
req.HttpReq.Header.Add("Authorization", "Bearer "+client.Token)
// 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 {
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 := io.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 >= 200 && httpRes.StatusCode <= 299 {
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 if httpRes.StatusCode == 408 || (httpRes.StatusCode >= 501 && httpRes.StatusCode <= 599) {
log.Printf("[ERROR] HTTP Request failed: StatusCode %v, Retries: %v", httpRes.StatusCode, attempts)
continue
} else {
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)
}
}
}
return res, nil
}
// Get makes a GET request and returns a GJSON result.
// Results will be the raw data structure as returned by Nexus Dashboard
func (client *Client) Get(path string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("GET", client.BasePath+path, nil, mods...)
err := client.Authenticate()
if err != nil {
return Res{}, err
}
return client.Do(req)
}
// Delete makes a DELETE request and returns a GJSON result.
// Hint: Use the Body struct to easily create DELETE body data.
func (client *Client) Delete(path string, data string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("DELETE", client.BasePath+path, strings.NewReader(data), mods...)
err := client.Authenticate()
if err != nil {
return Res{}, err
}
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(path, data string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("POST", client.BasePath+path, strings.NewReader(data), mods...)
err := client.Authenticate()
if err != nil {
return Res{}, err
}
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(path, data string, mods ...func(*Req)) (Res, error) {
req := client.NewReq("PUT", client.BasePath+path, strings.NewReader(data), mods...)
err := client.Authenticate()
if err != nil {
return Res{}, err
}
return client.Do(req)
}
// Login authenticates to the Nexus Dashboard instance.
func (client *Client) Login() error {
body := ""
body, _ = sjson.Set(body, "userName", client.Usr)
body, _ = sjson.Set(body, "userPasswd", client.Pwd)
body, _ = sjson.Set(body, "domain", client.Domain)
req := client.NewReq("POST", "/login", strings.NewReader(body), NoLogPayload)
httpRes, err := client.HttpClient.Do(req.HttpReq)
if err != nil {
return err
}
defer httpRes.Body.Close()
if httpRes.StatusCode != 200 {
log.Printf("[ERROR] Authentication failed: StatusCode %v", httpRes.StatusCode)
return fmt.Errorf("Authentication failed")
}
bodyBytes, _ := io.ReadAll(httpRes.Body)
res := Res(gjson.ParseBytes(bodyBytes))
token := res.Get("token").String()
if token == "" {
log.Printf("[ERROR] Token retrieval failed: no token in payload")
return fmt.Errorf("Authentication failed")
}
client.Token = token
log.Printf("[DEBUG] Authentication successful")
return nil
}
// Login if no token available.
func (client *Client) Authenticate() error {
var err error
client.AuthenticationMutex.Lock()
if client.Token == "" {
err = client.Login()
}
client.AuthenticationMutex.Unlock()
return err
}
// 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
}