/
isiclient.go
103 lines (89 loc) · 2.73 KB
/
isiclient.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
package isiclient
import (
"crypto/tls"
"errors"
"io/ioutil"
"net"
"net/http"
"time"
"github.com/tidwall/gjson"
"github.com/prometheus/common/log"
)
// ISIClient is used to connect to an EMC Isilon Cluster
type ISIClient struct {
UserName string
Password string
authToken string
ClusterAddress string
ClusterName string
ISIVersion string
NumNodes int64
ErrorCount float64
httpClient *http.Client
}
// CallIsiAPI uses the client auth to call against an API endpoint and returns the string response
func (c *ISIClient) CallIsiAPI(request string, retryAttempts int) (response string, err error) {
req, _ := http.NewRequest("GET", request, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(c.UserName, c.Password)
resp, err := c.httpClient.Do(req)
if err != nil {
log.Infof("\n - Error connecting to Isilon: %s", err)
return "", err
}
defer resp.Body.Close()
respText, _ := ioutil.ReadAll(resp.Body)
s := string(respText)
if resp.StatusCode == 200 {
log.Debugln(s)
} else {
if retryAttempts >= 1 {
log.Infof("Got unknown code: %v when accessing URL: %s\n Body text is: %s\n", resp.StatusCode, request, respText)
// to do need to re-auth to get back into system
log.Info("retrying command")
// now lets recursively call ourselves and hopefully we get in again
s, _ = c.CallIsiAPI(request, retryAttempts-1)
c.ErrorCount++
} else {
s = ""
}
}
return s, nil
}
// NewIsiClient returns an initialized Isilon Client.
func NewIsiClient(user string, pass string, target string) (*ISIClient, error) {
log.Debugln("Init ISI Client")
c := ISIClient{
UserName: user,
Password: pass,
ClusterAddress: target,
httpClient: &http.Client{Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 60 * time.Second},
}
reqStatusURL := "https://" + c.ClusterAddress + ":8080/platform/1/cluster/config"
// make a quick call to the API and ensure that it works
s, err := c.CallIsiAPI(reqStatusURL, 2)
if err != nil {
return nil, errors.New("error creating connection")
}
if s != "" {
c.ClusterName = gjson.Get(s, "name").String()
c.ISIVersion = gjson.Get(s, "onefs_version.release").String()
c.NumNodes = gjson.Get(s, "devices.#").Int()
return &c, nil
}
return nil, errors.New("error creating connection")
}