forked from rokett/Citrix-NetScaler-Exporter
/
new_nitro_client.go
57 lines (45 loc) · 1.26 KB
/
new_nitro_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
package netscaler
import (
"crypto/tls"
"net/http"
"net/http/cookiejar"
"strings"
"time"
"github.com/pkg/errors"
)
// NitroClient represents the client used to connect to the API
type NitroClient struct {
url string
username string
password string
client *http.Client
}
// NewNitroClient creates a new client used to interact with the Nitro API.
// URL, username and password are passed to this function to allow connections to any NetScaler endpoint.
// The ignoreCert parameter allows self-signed certificates to be accepted. It should be used sparingly and only when you fully trust the endpoint.
func NewNitroClient(url string, username string, password string, ignoreCert bool) (*NitroClient, error) {
c := new(NitroClient)
c.url = strings.Trim(url, " /") + "/nitro/v1/"
c.username = username
c.password = password
jar, err := cookiejar.New(nil)
if err != nil {
return c, errors.Wrap(err, "error creating cookiejar")
}
c.client = &http.Client{
Timeout: 10 * time.Second,
Jar: jar,
}
if ignoreCert {
transpCfg := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
c.client.Transport = transpCfg
}
return c, nil
}
func (c *NitroClient) CloseIdleConnections() {
c.client.CloseIdleConnections()
}