forked from bmc-toolbox/bmclib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupConnections.go
163 lines (134 loc) · 3.35 KB
/
setupConnections.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
package m1000e
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"github.com/bmc-toolbox/bmclib/errors"
"github.com/bmc-toolbox/bmclib/internal/httpclient"
"github.com/bmc-toolbox/bmclib/internal/sshclient"
multierror "github.com/hashicorp/go-multierror"
"github.com/bmc-toolbox/bmclib/providers/dell"
log "github.com/sirupsen/logrus"
)
// retrieves ST2 which is required to submit form data
func (m *M1000e) getSessionToken() (token string, err error) {
data, err := m.get("general")
if err != nil {
return token, err
}
//<input xmlns="" type="hidden" value="2a17b6d37baa526b75e06243d34763da" name="ST2" id="ST2" />
re := regexp.MustCompile("<input.*value=\\\"(\\w+)\\\" name=\"ST2\"")
match := re.FindSubmatch(data)
if len(match) == 0 {
return token, errors.ErrUnableToGetSessionToken
}
return string(match[1]), err
}
func (m *M1000e) loadHwData() (err error) {
url := "json?method=groupinfo"
payload, err := m.get(url)
if err != nil {
return err
}
m.cmcJSON = &dell.CMC{}
err = json.Unmarshal(payload, m.cmcJSON)
if err != nil {
return err
}
if m.cmcJSON.Chassis == nil {
return errors.ErrUnableToReadData
}
url = "json?method=blades-wwn-info"
payload, err = m.get(url)
if err != nil {
return err
}
m.cmcWWN = &dell.CMCWWN{}
err = json.Unmarshal(payload, m.cmcWWN)
if err != nil {
return err
}
return err
}
// Login initiates the connection to a chassis device
func (m *M1000e) httpLogin() (err error) {
if m.httpClient != nil {
return
}
log.WithFields(log.Fields{"step": "chassis connection", "vendor": dell.VendorID, "ip": m.ip}).Debug("connecting to chassis")
form := url.Values{}
form.Add("user", m.username)
form.Add("password", m.password)
u, err := url.Parse(fmt.Sprintf("https://%s/cgi-bin/webcgi/login", m.ip))
if err != nil {
return err
}
req, err := http.NewRequest("POST", u.String(), strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
httpClient, err := httpclient.Build()
if err != nil {
return err
}
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
auth, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if strings.Contains(string(auth), "Try Again") {
return errors.ErrLoginFailed
}
if resp.StatusCode == 404 {
return errors.ErrPageNotFound
}
m.httpClient = httpClient
err = m.loadHwData()
if err != nil {
return err
}
// retrieve session token to set config params.
token, err := m.getSessionToken()
if err != nil {
return err
}
m.SessionToken = token
return err
}
// sshLogin initiates the connection to a chassis device
func (m *M1000e) sshLogin() (err error) {
if m.sshClient != nil {
return
}
log.WithFields(log.Fields{"step": "chassis connection", "vendor": dell.VendorID, "ip": m.ip}).Debug("connecting to chassis")
m.sshClient, err = sshclient.New(m.ip, m.username, m.password)
if err != nil {
return err
}
return err
}
// Close closes the connection properly
func (m *M1000e) Close() (err error) {
if m.httpClient != nil {
_, e := m.httpClient.Get(fmt.Sprintf("https://%s/cgi-bin/webcgi/logout", m.ip))
if e != nil {
err = multierror.Append(e, err)
}
}
if m.sshClient != nil {
e := m.sshClient.Close()
if e != nil {
err = multierror.Append(e, err)
}
}
return err
}