-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsn.go
117 lines (98 loc) · 2.89 KB
/
bsn.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
/*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Copyright 2017 OpsVision Solutions
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bsn
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
//"io"
"net"
"net/http"
//"os"
"time"
)
type Client struct {
Controller string `json:"controller"`
SessionCookie string `json:"session_cookie"`
}
func New(controller string, port string) *Client {
client := &Client{}
client.Controller = fmt.Sprintf("https://%s:%s", controller, port)
return client
}
// GetSwitches retrieves a list of switches from the BSN controller
func (c *Client) GetSwitches() Switches {
var endpoint = fmt.Sprintf("%s/api/v1/data/controller/applications/bcf/info/fabric/switch", c.Controller)
var switches Switches
// Create the request
req, err := http.NewRequest("GET", endpoint, nil)
// Set headers
req.Header.Set("content-type", "application/json")
req.Header.Set("Cookie", c.SessionCookie)
// Send the payload
resp, err := c.getClient().Do(req)
if err != nil {
fmt.Printf("Error: %s\n" + err.Error())
}
defer resp.Body.Close()
// Decode the response
json.NewDecoder(resp.Body).Decode(&switches)
//io.Copy(os.Stdout, resp.Body)
return switches
}
// Authenticate with the BSN controller
func (c *Client) Authenticate(creds *Credentials) {
var endpoint = fmt.Sprintf("%s/api/v1/auth/login", c.Controller)
var payload bytes.Buffer
var authresp AuthResponse
// Encode the payload; username and password
json.NewEncoder(&payload).Encode(creds)
// Create the request
req, err := http.NewRequest("POST", endpoint, &payload)
// Set headers
req.Header.Set("content-type", "application/json")
// Send the payload
resp, err := c.getClient().Do(req)
if err != nil {
fmt.Printf("Error: %s\n" + err.Error())
}
defer resp.Body.Close()
// Decode the response
json.NewDecoder(resp.Body).Decode(&authresp)
// Store the session cookie
c.SessionCookie = "session_cookie=" + authresp.SessionCookie
}
// getClient sets up our http client
func (c *Client) getClient() *http.Client {
// Setup transport settings
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableCompression: true,
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
// Create a client
client := &http.Client{
Timeout: time.Second * 10, // 10 second timeout
Transport: tr,
}
return client
}