-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
192 lines (165 loc) · 5.15 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
package main
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
xj "github.com/basgys/goxml2json"
)
var (
PowerStatus = Attribute("pwState")
SystemDescription = Attribute("sysDesc")
SystemRevision = Attribute("sysRev")
HostName = Attribute("hostName")
OSName = Attribute("osName")
OSVersion = Attribute("osVersion")
ServiceTag = Attribute("svcTag")
ExpServiceCode = Attribute("expSvcCode")
BiosVersion = Attribute("biosVer")
FirmwareVersion = Attribute("fwVersion")
LCCFirmwareVersion = Attribute("LCCfwVersion")
IPV4Enabled = Attribute("v4Enabled")
IPV4Address = Attribute("v4IPAddr")
IPV6Enabled = Attribute("v6Enabled")
IPV6LinkLocal = Attribute("v6LinkLocal")
IPV6Address = Attribute("v6Addr")
IPV6SiteLocal = Attribute("v6SiteLocal")
MacAddress = Attribute("macAddr")
Batteries = Attribute("batteries")
FanRedundancy = Attribute("fansRedundancy")
Fans = Attribute("fans")
Intrusion = Attribute("intrusion")
PowerSupplyRedundancy = Attribute("psRedundancy")
PowerSupplies = Attribute("powerSupplies")
RMVRedundancy = Attribute("rmvsRedundancy")
RemovableStorage = Attribute("removableStorage")
Temperatures = Attribute("temperatures")
Voltages = Attribute("voltages")
KVMEnabled = Attribute("kvmEnabled")
PowerBudgetData = Attribute("budgetpowerdata")
EventLog = Attribute("eventLogEntries")
BootOnce = Attribute("vmBootOnce")
FirstBootDevice = Attribute("firstBootDevice")
VFKLicense = Attribute("vfkLicense")
User = Attribute("user")
IDRACLog = Attribute("racLogEntries")
PowerOff = PowerState(0)
PowerOn = PowerState(1)
NonMaskingInterrupt = PowerState(2)
GracefulShutdown = PowerState(3)
ColdReboot = PowerState(4)
WarmReboot = PowerState(5)
NoOverride = BootDevice(0)
PXE = BootDevice(1)
HardDrive = BootDevice(2)
BIOS = BootDevice(6)
VirtualCD = BootDevice(8)
LocalSD = BootDevice(16)
LocalCD = BootDevice(5)
)
type Attribute string
type PowerState uint8
type BootDevice uint8
type SensorType struct {
}
type Client struct {
client *http.Client
AuthToken string
Host string
}
func (c *Client) doHTTP(path, qs string, body io.Reader) (string, []*http.Cookie, error) {
method := "GET"
if strings.ContainsAny(qs, "set") {
method = "POST"
}
uri := fmt.Sprintf("https://%s/data/%s?%s", c.Host, path, qs)
req, _ := http.NewRequest(method, uri, body)
req.AddCookie(&http.Cookie{
Name: "_appwebSessionId_",
Value: c.AuthToken,
})
res, err := c.client.Do(req)
if err != nil {
return "", nil, errors.New("could not make request")
}
defer res.Body.Close()
jsonData, err := xj.Convert(res.Body)
if err != nil {
return "", nil, err
}
if res.StatusCode >= 300 || res.StatusCode < 200 {
return jsonData.String(), res.Cookies(), fmt.Errorf("got a non 200 status: %d", res.StatusCode)
}
return jsonData.String(), res.Cookies(), nil
}
func (c *Client) SetPowerState(ps PowerState) (string, error) {
res, _, err := c.doHTTP("", fmt.Sprintf("set=pwState:%d", ps), nil)
return res, err
}
func (c *Client) SetBootOverride(bo BootDevice, bootOnce bool) (string, error) {
res, _, err := c.doHTTP("", fmt.Sprintf("set=vmBootOnce:%v,firstBootDevice:%d", bootOnce, bo), nil)
return res, err
}
func (c *Client) Query(ds ...Attribute) (string, error) {
bufs := bytes.NewBufferString("")
for idx, attr := range ds {
bufs.WriteString(string(attr))
if idx < len(ds)-1 {
bufs.WriteString(",")
}
}
res, _, err := c.doHTTP("", fmt.Sprintf("get=%s", bufs.String()), nil)
return res, err
}
func NewFromCredentials(path string) (*Client, error) {
credential, err := LoadCredentials(path)
if err != nil {
if os.IsNotExist(err) {
return nil, errors.New("You should log in first")
}
return nil, err
}
c, err := NewClient(credential.Host, true)
if err != nil {
return nil, err
}
c.AuthToken = credential.AuthToken
return c, nil
}
func NewClient(host string, skipVerify bool) (*Client, error) {
c := &http.Client{
Timeout: time.Second * 5,
Transport: &http.Transport{
IdleConnTimeout: time.Second,
TLSHandshakeTimeout: time.Second * 5,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: skipVerify,
},
},
}
client := &Client{client: c, Host: host}
// err := client.Login(host, username, password)
// if err != nil {
// return nil, err
// }
return client, nil
}
func (c *Client) Login(username, password string) (string, error) {
body := bytes.NewBufferString(fmt.Sprintf("user=%s&password=%s", username, password))
_, cookies, err := c.doHTTP("login", "", body)
if err != nil {
return "", err
}
for _, cookie := range cookies {
if cookie.Name == "_appwebSessionId_" {
c.AuthToken = cookie.Value
return cookie.Value, nil
}
}
return "", errors.New("could not find auth token in cookie")
}