forked from mrxinu/gosolar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gosolar.go
253 lines (208 loc) · 6.03 KB
/
gosolar.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package gosolar
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
)
// Client structure for the SolarWinds (SWIS) connection.
type Client struct {
// connection parameters
URL string
Username string
Password string
Server string
// internal state
http *http.Client
}
// NewClient creates a new reference to the Client struct.
func NewClient(host, user, pass string, useSSL bool, ignoreSSL bool) *Client {
var prefix = "http"
if useSSL {
prefix = "https"
}
return &Client{
URL: fmt.Sprintf("%s://%s:17778/SolarWinds/InformationService/v3/Json/", prefix, host),
Server: host,
Username: user,
Password: pass,
http: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: ignoreSSL,
},
MaxIdleConnsPerHost: 4, // DEFAULT: 2
},
},
}
}
func (c *Client) post(endpoint string, body interface{}) ([]byte, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %v", err)
}
req, err := http.NewRequest("POST", c.URL+endpoint, &buf)
if err != nil {
return nil, fmt.Errorf("failed to create a new request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.Username, c.Password)
res, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to submit query: %v", err)
}
output, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("query failed - status code %d: %v", res.StatusCode, err)
}
res.Body.Close()
if res.StatusCode >= 400 {
return nil, fmt.Errorf("swis failure message [status: %d]:\n%s",
res.StatusCode, string(output))
}
return output, nil
}
func (c *Client) get(endpoint string) ([]byte, error) {
req, err := http.NewRequest("GET", c.URL+endpoint, nil)
if err != nil {
return nil, fmt.Errorf("failed to create new request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(c.Username, c.Password)
res, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to submit query: %v", err)
}
defer func() {
io.Copy(ioutil.Discard, res.Body)
err2 := res.Body.Close()
if err2 != nil {
log.Fatalf("failed to close result body: %v", err2)
}
}()
output, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("query failed - status code %d: %v", res.StatusCode, err)
}
if res.StatusCode >= 400 {
return nil, fmt.Errorf("swis failure message [status: %d]:\n%s",
res.StatusCode, string(output))
}
return output, nil
}
// Query retrieves a result from the SolarWinds API.
func (c *Client) Query(query string, parameters interface{}) ([]byte, error) {
req := struct {
Query string `json:"query"`
Parameters interface{} `json:"parameters"`
}{
Query: query,
Parameters: parameters,
}
result, err := c.post("Query", &req)
if err != nil {
return nil, fmt.Errorf("failed to query: %v", err)
}
sr := struct {
Result *json.RawMessage `json:"results"`
}{}
err = json.Unmarshal(result, &sr)
if err != nil {
return nil, err
}
return []byte(*sr.Result), nil
}
// QueryOne wraps QueryRow which wraps post and extracts a single value.
func (c *Client) QueryOne(query string, parameters interface{}) (interface{}, error) {
res, err := c.QueryRow(query, parameters)
if err != nil {
return nil, err
}
m := make(map[string]interface{})
if json.Unmarshal(res, &m); err != nil {
return nil, fmt.Errorf("could not unmarshal the result: %v", err)
}
var value interface{}
for _, v := range m {
value = v
break
}
return value, nil
}
// QueryRow wraps query and pulls a single row from the result.
func (c *Client) QueryRow(query string, parameters interface{}) ([]byte, error) {
res, err := c.Query(query, parameters)
if err != nil {
return nil, err
}
return res[1 : len(res)-1], nil
}
// QueryColumn wraps Query and pulls a single column of values into a slice of maps.
func (c *Client) QueryColumn(query string, parameters interface{}) ([]interface{}, error) {
res, err := c.Query(query, parameters)
if err != nil {
return nil, err
}
var rows []map[string]interface{}
if json.Unmarshal(res, &rows); err != nil {
return nil, fmt.Errorf("could not unmarshal the result: %v", err)
}
var values []interface{}
for _, m := range rows {
for _, v := range m {
values = append(values, v)
}
}
return values, nil
}
// Create calls the create endpoint and passes the entity and body.
func (c *Client) Create(entity, body interface{}) ([]byte, error) {
endpoint := fmt.Sprintf("Create/%s", entity)
return c.post(endpoint, body)
}
func (c *Client) Read(uri string) ([]byte, error) {
return c.get(uri)
}
// Invoke calls the invoke endpoint with the entity and verb along with a body.
func (c *Client) Invoke(entity, verb string, body interface{}) ([]byte, error) {
endpoint := fmt.Sprintf("Invoke/%s/%s", entity, verb)
return c.post(endpoint, body)
}
// BulkDelete wraps post and send a slice of URIs to delete.
func (c *Client) BulkDelete(uris []string) ([]byte, error) {
req := map[string][]string{
"uris": uris,
}
return c.post("BulkDelete", req)
}
// Delete wraps post and uses the DELETE method to delete an entity.
func (c *Client) Delete(uri string) ([]byte, error) {
req, err := http.NewRequest("DELETE", c.URL+uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create a new request: %v", err)
}
req.SetBasicAuth(c.Username, c.Password)
res, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to delete: %v", err)
}
output, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("delete failed - status code %d: %v", res.StatusCode, err)
}
res.Body.Close()
if res.StatusCode >= 400 {
return nil, fmt.Errorf("swis failure message [status: %d]:\n%s",
res.StatusCode, string(output))
}
return output, nil
}
// Update wraps the post function passing the URI and body to update.
func (c *Client) Update(uri string, body map[string]interface{}) ([]byte, error) {
return c.post(uri, body)
}