/
client.go
652 lines (573 loc) · 20.1 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
package goaviatrix
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/ajg/form"
log "github.com/sirupsen/logrus"
)
// LoginResp represents the response object from the `login` action
type LoginResp struct {
Return bool `json:"return"`
Results string `json:"results"`
Reason string `json:"reason"`
CID string `json:"CID"`
}
// APIResp represents the basic response from any action
type APIResp struct {
Return bool `json:"return"`
Reason string `json:"reason"`
}
// APIRequest represents the basic fields for any request
type APIRequest struct {
CID string `form:"CID,omitempty" json:"CID" url:"CID"`
Action string `form:"action,omitempty" json:"action" url:"action"`
}
// Client for accessing the Aviatrix Controller
type Client struct {
HTTPClient *http.Client
Username string
Password string
CID string
ControllerIP string
baseURL string
IgnoreTagsConfig *IgnoreTagsConfig
}
// Login to the Aviatrix controller with the username/password provided in
// the client structure.
// Arguments:
// None
// Returns:
// error - if any
func (c *Client) Login() error {
account := make(map[string]interface{})
account["action"] = "login"
account["username"] = c.Username
account["password"] = c.Password
log.Infof("Parsed Aviatrix login: %s", account["username"])
resp, err := c.Post(c.baseURL, account)
if err != nil {
return err
}
var data LoginResp
if err = json.NewDecoder(resp.Body).Decode(&data); err != nil {
return err
}
if !data.Return {
return errors.New(data.Reason)
}
log.Tracef("CID is '%s'.", data.CID)
c.CID = data.CID
return nil
}
// NewClient creates a Client object using the arguments provided.
// Arguments:
// username - the controller username
// password - the controller password
// controllerIP - the controller IP/host
// HTTPClient - the http client object
// Returns:
// Client - the newly created client
// error - if any
// See Also:
// init()
func NewClient(username string, password string, controllerIP string, HTTPClient *http.Client, ignoreTagsConfig *IgnoreTagsConfig) (*Client, error) {
client := &Client{Username: username, Password: password, HTTPClient: HTTPClient, ControllerIP: controllerIP, IgnoreTagsConfig: ignoreTagsConfig}
return client.init(controllerIP)
}
// init initializes the new client with the given controller IP/host. Logs
// in to the controller and sets up the http client.
// Arguments:
// controllerIP - the controller host/IP
// Returns:
// Client - the updated client object
// error - if any
func (c *Client) init(controllerIP string) (*Client, error) {
if len(controllerIP) == 0 {
return nil, fmt.Errorf("Aviatrix: Client: Controller IP is not set")
}
c.baseURL = "https://" + controllerIP + "/v1/api"
if c.HTTPClient == nil {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
c.HTTPClient = &http.Client{Transport: tr}
}
if err := c.Login(); err != nil {
return nil, err
}
return c, nil
}
func (c *Client) Get(path string, i interface{}) (*http.Response, error) {
return c.Request("GET", path, i)
}
// Post issues an HTTP POST request with the given interface form-encoded.
func (c *Client) Post(path string, i interface{}) (*http.Response, error) {
return c.Request("POST", path, i)
}
func (c *Client) GetContext(ctx context.Context, path string, i interface{}) (*http.Response, error) {
return c.RequestContext(ctx, "GET", path, i)
}
// PostContext issues an HTTP POST request with the given interface form-encoded.
func (c *Client) PostContext(ctx context.Context, path string, i interface{}) (*http.Response, error) {
return c.RequestContext(ctx, "POST", path, i)
}
// CheckAPIResponseFunc looks at the Reason and Return fields from an API response
// and returns an error
type CheckAPIResponseFunc func(action, method, reason string, ret bool) error
// BasicCheck will only verify that the Return field was set to true
var BasicCheck CheckAPIResponseFunc = func(action, method, reason string, ret bool) error {
if !ret {
return fmt.Errorf("rest API %s %s failed: %s", action, method, reason)
}
return nil
}
// DuplicateBasicCheck will verify that the Return field was set to true
// If the Return is false and Reason contains "already exists", it will return a DuplicateError
var DuplicateBasicCheck CheckAPIResponseFunc = func(action, method, reason string, ret bool) error {
if !ret {
err := fmt.Errorf("rest API %s %s failed: %s", action, method, reason)
if strings.Contains(strings.ToLower(reason), "already exists") {
return DuplicateError{
Err: err,
}
}
return err
}
return nil
}
// PostAPI makes a post request to the Aviatrix API, decodes the response and checks for any errors
func (c *Client) PostAPI(action string, d interface{}, checkFunc CheckAPIResponseFunc) error {
return c.PostAPIContext(context.Background(), action, d, checkFunc)
}
// PostAPIContext makes a post request to the Aviatrix API, decodes the response and checks for any errors
func (c *Client) PostAPIContext(ctx context.Context, action string, d interface{}, checkFunc CheckAPIResponseFunc) error {
resp, err := c.PostContext(ctx, c.baseURL, d)
if err != nil {
return fmt.Errorf("HTTP POST %q failed: %v", action, err)
}
return checkAPIResp(resp, action, checkFunc)
}
// PostAPIDownloadContext makes a post request to the Aviatrix API, checks for errors and returns the response body
func (c *Client) PostAPIDownloadContext(ctx context.Context, action string, d interface{}, checkFunc CheckAPIResponseFunc) (io.ReadCloser, error) {
resp, err := c.PostContext(ctx, c.baseURL, d)
if err != nil {
return nil, fmt.Errorf("HTTP POST %q failed: %v", action, err)
}
if strings.Contains(resp.Header.Get("Content-Type"), "json") {
return nil, checkAPIResp(resp, action, checkFunc)
}
return resp.Body, nil
}
// PostAPIWithResponse makes a post request to the Aviatrix API, decodes the response, checks for any errors
// and decodes the response into the return value v.
func (c *Client) PostAPIWithResponse(v interface{}, action string, d interface{}, checkFunc CheckAPIResponseFunc) error {
return c.PostAPIContextWithResponse(context.Background(), v, action, d, checkFunc)
}
// PostAPIContextWithResponse makes a post request to the Aviatrix API, decodes the response, checks for any errors
// and decodes the response into the return value v.
func (c *Client) PostAPIContextWithResponse(ctx context.Context, v interface{}, action string, d interface{}, checkFunc CheckAPIResponseFunc) error {
resp, err := c.PostContext(ctx, c.baseURL, d)
if err != nil {
return fmt.Errorf("HTTP POST %q failed: %v", action, err)
}
return checkAndReturnAPIResp(resp, v, "POST", action, checkFunc)
}
// PostFileAPI will encode the files and parameters with multipart form encoding and POST to the API.
// The API response is decoded and checked with the provided checkFunc
func (c *Client) PostFileAPI(params map[string]string, files []File, checkFunc CheckAPIResponseFunc) error {
if params["action"] == "" {
return fmt.Errorf("cannot PostFileAPI without an 'action' in params map")
}
resp, err := c.PostFile(c.baseURL, params, files)
if err != nil {
return fmt.Errorf("HTTP POST %q failed: %v", params["action"], err)
}
return checkAPIResp(resp, params["action"], checkFunc)
}
// PostFileAPIContext will encode the files and parameters with multipart form encoding and POST to the API.
// The API response is decoded and checked with the provided checkFunc
func (c *Client) PostFileAPIContext(ctx context.Context, params map[string]string, files []File, checkFunc CheckAPIResponseFunc) error {
if params["action"] == "" {
return fmt.Errorf("cannot PostFileAPIContext without an 'action' in params map")
}
resp, err := c.PostFileContext(ctx, c.baseURL, params, files)
if err != nil {
return fmt.Errorf("HTTP POST %q failed: %v", params["action"], err)
}
return checkAPIResp(resp, params["action"], checkFunc)
}
func (c *Client) PostAsyncAPI(action string, i interface{}, checkFunc CheckAPIResponseFunc) error {
return c.PostAsyncAPIContext(context.Background(), action, i, checkFunc)
}
func (c *Client) PostAsyncAPIContext(ctx context.Context, action string, i interface{}, checkFunc CheckAPIResponseFunc) error {
log.Printf("[DEBUG] Post AsyncAPI %s: %v", action, i)
resp, err := c.PostContext(ctx, c.baseURL, i)
if err != nil {
return fmt.Errorf("HTTP POST %s failed: %v", action, err)
}
var data struct {
Return bool `json:"return"`
Result int `json:"results"`
Reason string `json:"reason"`
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
resp.Body.Close()
bodyString := buf.String()
bodyIoCopy := strings.NewReader(bodyString)
if err = json.NewDecoder(bodyIoCopy).Decode(&data); err != nil {
return fmt.Errorf("Json Decode %s failed %v\n Body: %s", action, err, bodyString)
}
if !data.Return || data.Result == 0 {
return fmt.Errorf("rest API %s POST failed to initiate async action: %s", action, data.Reason)
}
requestID := data.Result
form := map[string]string{
"action": "check_task_status",
"CID": c.CID,
"id": strconv.Itoa(requestID),
"pos": "0",
}
backendURL := fmt.Sprintf("https://%s/v1/backend1", c.ControllerIP)
const maxPoll = 360
sleepDuration := time.Second * 10
var j int
for ; j < maxPoll; j++ {
resp, err = c.PostContext(ctx, backendURL, form)
if err != nil {
// Could be transient HTTP error, e.g. EOF error
time.Sleep(sleepDuration)
continue
}
buf = new(bytes.Buffer)
buf.ReadFrom(resp.Body)
var data struct {
Pos int `json:"pos"`
Done bool `json:"done"`
Status bool `json:"status"`
Result string `json:"result"`
}
err = json.Unmarshal(buf.Bytes(), &data)
if err != nil {
return fmt.Errorf("decode check_task_status failed: %v\n Body: %s", err, buf.String())
}
if !data.Done {
// Not done yet
time.Sleep(sleepDuration)
continue
}
// Async API is done, return result of checkFunc
return checkFunc(action, "Post", data.Result, data.Status)
}
// Waited for too long and async API never finished
return fmt.Errorf("waited %s but upgrade never finished. Please manually verify the upgrade status", maxPoll*sleepDuration)
}
// checkAPIResp will decode the response and check for any errors with the provided checkFunc
func checkAPIResp(resp *http.Response, action string, checkFunc CheckAPIResponseFunc) error {
var data APIResp
var b bytes.Buffer
_, err := b.ReadFrom(resp.Body)
if err != nil {
return fmt.Errorf("reading response body %q failed: %v", action, err)
}
if err = json.NewDecoder(&b).Decode(&data); err != nil {
return fmt.Errorf("json Decode %q failed: %v\n Body: %s", action, err, b.String())
}
return checkFunc(action, "Post", data.Reason, data.Return)
}
// checkAndReturnAPIResp will decode the response and check for any errors with the provided checkFunc.
// If there are no errors, the response will be put into the return value v.
func checkAndReturnAPIResp(resp *http.Response, v interface{}, method, action string, checkFunc CheckAPIResponseFunc) error {
var data APIResp
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(resp.Body)
if err != nil {
return fmt.Errorf("reading response body %q failed: %v", action, err)
}
bodyString := buf.String()
if err := json.NewDecoder(strings.NewReader(bodyString)).Decode(&data); err != nil {
return fmt.Errorf("Json Decode into standard format failed: %v\n Body: %s", err, bodyString)
}
if err := checkFunc(action, method, data.Reason, data.Return); err != nil {
return err
}
if err := json.NewDecoder(strings.NewReader(bodyString)).Decode(&v); err != nil {
return fmt.Errorf("Json Decode failed: %v\n Body: %s", err, bodyString)
}
return nil
}
// GetAPI makes a GET request to the Aviatrix API
// First, we decode into the generic APIResp struct, then check for errors
// If no errors, we will decode into the user defined structure that is passed in
func (c *Client) GetAPI(v interface{}, action string, d map[string]string, checkFunc CheckAPIResponseFunc) error {
return c.GetAPIContext(context.Background(), v, action, d, checkFunc)
}
// GetAPIContext makes a GET request to the Aviatrix API
// If the GET request fails we will retry
// First, we decode into the generic APIResp struct, then check for errors
// If no errors, we will decode into the user defined structure that is passed in
func (c *Client) GetAPIContext(ctx context.Context, v interface{}, action string, d map[string]string, checkFunc CheckAPIResponseFunc) error {
Url, err := c.urlEncode(d)
if err != nil {
return fmt.Errorf("could not url encode values for action %q: %v", action, err)
}
try, maxTries, backoff := 0, 5, 500*time.Millisecond
var resp *http.Response
for {
try++
resp, err = c.GetContext(ctx, Url, nil)
if err == nil {
break
}
log.WithFields(log.Fields{
"try": try,
"action": action,
"err": err.Error(),
}).Warnf("HTTP GET request failed")
if try == maxTries {
return fmt.Errorf("HTTP Get %s failed: %v", action, err)
}
time.Sleep(backoff)
// Double the backoff time after each failed try
backoff *= 2
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
bodyString := buf.String()
var data APIResp
if err := json.NewDecoder(strings.NewReader(bodyString)).Decode(&data); err != nil {
return fmt.Errorf("Json Decode into standard format failed: %v\n Body: %s", err, bodyString)
}
if err := checkFunc(action, "Get", data.Reason, data.Return); err != nil {
return err
}
if err := json.NewDecoder(strings.NewReader(bodyString)).Decode(&v); err != nil {
return fmt.Errorf("Json Decode failed: %v\n Body: %s", err, bodyString)
}
return nil
}
func (c *Client) urlEncode(d map[string]string) (string, error) {
Url, err := url.Parse(c.baseURL)
if err != nil {
return "", fmt.Errorf("parsing url: %v", err)
}
v := url.Values{}
for key, val := range d {
v.Add(key, val)
}
Url.RawQuery = v.Encode()
return Url.String(), nil
}
// Put issues an HTTP PUT request with the given interface form-encoded.
func (c *Client) Put(path string, i interface{}) (*http.Response, error) {
return c.Request("PUT", path, i)
}
// Delete issues an HTTP DELETE request.
func (c *Client) Delete(path string, i interface{}) (*http.Response, error) {
return c.Request("GET", path, i)
}
type File struct {
Path string
ParamName string
UseFileContent bool // set to true when using the file content instead of file path
FileName string // use when UseFileContent is true
FileContent string // use when UseFileContent is true
}
// PostFile will encode the files and parameters with multipart form encoding.
func (c *Client) PostFile(path string, params map[string]string, files []File) (*http.Response, error) {
body, contentType, err := encodeMultipartFormData(params, files)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", path, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.HTTPClient.Do(req)
}
// PostFileContext will encode the files and parameters with multipart form encoding.
func (c *Client) PostFileContext(ctx context.Context, path string, params map[string]string, files []File) (*http.Response, error) {
body, contentType, err := encodeMultipartFormData(params, files)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "POST", path, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.HTTPClient.Do(req)
}
func encodeMultipartFormData(params map[string]string, files []File) (*bytes.Buffer, string, error) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
// Encode the files
for _, f := range files {
if !f.UseFileContent {
if f.Path == "" {
continue
}
file, err := os.Open(f.Path)
if err != nil {
return nil, "", err
}
fileContents, err := ioutil.ReadAll(file)
if err != nil {
return nil, "", err
}
fi, err := file.Stat()
if err != nil {
return nil, "", err
}
_ = file.Close()
part, err := createFormFile(f.ParamName, fi.Name(), http.DetectContentType(fileContents), writer)
if err != nil {
return nil, "", err
}
_, _ = part.Write(fileContents)
} else {
fileContents := []byte(f.FileContent)
part, err := createFormFile(f.ParamName, f.FileName, http.DetectContentType(fileContents), writer)
if err != nil {
return nil, "", err
}
_, _ = part.Write(fileContents)
}
}
// Encode the other params
for key, val := range params {
_ = writer.WriteField(key, val)
}
err := writer.Close()
if err != nil {
return nil, "", err
}
return body, writer.FormDataContentType(), nil
}
func createFormFile(fieldname, filename, fileContentType string, w *multipart.Writer) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldname), escapeQuotes(filename)))
h.Set("Content-Type", fileContentType)
return w.CreatePart(h)
}
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
// Request makes an HTTP request with the given interface being encoded as
// form data.
func (c *Client) Request(verb string, path string, i interface{}) (*http.Response, error) {
return c.RequestContext(context.Background(), verb, path, i)
}
// RequestContext makes an HTTP request with the given interface being encoded as
// form data.
func (c *Client) RequestContext(ctx context.Context, verb string, path string, i interface{}) (*http.Response, error) {
log.Tracef("%s %s", verb, path)
try, maxTries, backoff := 0, 2, 500*time.Millisecond
var req *http.Request
var err error
var data *APIResp
var resp *http.Response
for {
try++
if i != nil {
buf := new(bytes.Buffer)
if err = form.NewEncoder(buf).Encode(i); err != nil {
return nil, err
}
body := buf.String()
log.Tracef("%s %s Body: %s", verb, path, body)
reader := strings.NewReader(body)
req, err = http.NewRequestWithContext(ctx, verb, path, reader)
if err == nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
} else {
req, err = http.NewRequestWithContext(ctx, verb, path, nil)
}
if err != nil {
return nil, err
}
resp, err = c.HTTPClient.Do(req)
if err != nil {
return resp, err
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
resp.Body.Close()
// Replace resp.Body with new ReadCloser so that other methods can read the buffer again
resp.Body = io.NopCloser(buf)
if strings.Contains(resp.Header.Get("Content-Type"), "json") {
bodyString := buf.String()
data = new(APIResp)
if err := json.NewDecoder(strings.NewReader(bodyString)).Decode(data); err != nil {
return resp, fmt.Errorf("Json Decode into standard format failed: %v\n Body: %s", err, bodyString)
}
// Any error not related to expired CID should return
if !(strings.Contains(data.Reason, "CID is invalid") || strings.Contains(data.Reason, "Invalid session. Please login again.")) {
return resp, err
}
log.Tracef("CID invalid or expired. Trying to login again")
if err = c.Login(); err != nil {
return resp, err
}
// update the CID value in the object passed
if i != nil {
// Update CID in POST body
v := reflect.ValueOf(i)
if v.Kind() == reflect.Map {
v.SetMapIndex(reflect.ValueOf("CID"), reflect.ValueOf(c.CID))
} else {
s := v.Elem()
f := s.FieldByName("CID")
if f.IsValid() && f.CanSet() {
f.SetString(c.CID)
}
}
} else {
// Update CID in GET URL
Url, err := url.Parse(path)
if err != nil {
return resp, fmt.Errorf("failed to parse url: %v", err)
}
query := Url.Query()
query["CID"] = []string{c.CID}
Url.RawQuery = query.Encode()
path = Url.String()
}
log.WithFields(log.Fields{
"try": try,
"err": "CID is invalid",
}).Warnf("HTTP request failed with expired CID")
if try == maxTries {
return resp, fmt.Errorf("%v", data.Reason)
}
time.Sleep(backoff)
// Double the backoff time after each failed try
backoff *= 2
} else {
return resp, nil
}
}
}