This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
driver.go
492 lines (357 loc) · 10.3 KB
/
driver.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Copyright 2014 VMware, Inc. All rights reserved. Licensed under the Apache v2 License.
*/
package vmwareappcatalyst
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/docker/machine/libmachine/log"
)
type VMDelete struct {
ID string `json:"id"`
}
type VMInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Tag string `json:"tag"`
}
type Response struct {
Code int `json:"code"` // HTTP Return code
Message string `json:"message"` // Response message
}
type VM struct {
ID string `json:"id"`
Name string `json:"name"`
Tag string `json:"tag"`
}
type CloneVM struct {
ID string `json:"id"`
Name string `json:"name"`
Tag string `json:"tag"`
}
type SharedFolder struct {
GuestPath string `json:"guestPath"`
HostPath string `json:"hostPath"`
Flags int `json:"flags"`
}
// Client is the struct that holds information on the AppCatalyst endpoint
type Client struct {
Endpoint url.URL // Endpoint is the REST api endpoint AppCatalyst is listening on
HTTP http.Client // HttpClient is the client to use. Default will be used if not provided.
}
// NewClient returns a new client to be used with every AppCatalyst interaction
func NewClient(e string) (*Client, error) {
u, err := url.ParseRequestURI(e)
if err != nil {
return &Client{}, fmt.Errorf("cannot parse endpoint, make sure it's a complete URL")
}
Client := Client{
Endpoint: *u,
HTTP: http.Client{},
}
return &Client, nil
}
// GetVMSharedFolders gets shared folders information for a specific VM
func (c *Client) GetVMSharedFolders(ID string) ([]string, error) {
s := c.Endpoint
s.Path += "/api/vms/" + ID + "/folders"
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "GET", s, nil)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var folderlist []string
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &folderlist); err != nil {
return nil, err
}
return folderlist, nil
}
func (c *Client) SetVMSharedFolders(ID, operation string) (*Response, error) {
s := c.Endpoint
s.Path += "/api/vms/" + ID + "/folders"
b := bytes.NewBufferString(operation)
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "PATCH", s, b)
response := new(Response)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return response, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
if err = json.Unmarshal(body, &response); err != nil {
return response, err
}
return response, nil
}
// CloneVM creates a clone starting from an existing VM
func (c *Client) AddVMSharedFolder(ID string, gp string, hp string, flags int) (*SharedFolder, error) {
s := c.Endpoint
s.Path += "/api/vms/" + ID + "/folders"
create := SharedFolder{
GuestPath: gp,
HostPath: hp,
Flags: flags,
}
out, err := json.Marshal(create)
b := bytes.NewBufferString(string(out))
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "POST", s, b)
sf := new(SharedFolder)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return sf, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return sf, err
}
if err = json.Unmarshal(body, &sf); err != nil {
return sf, err
}
return sf, nil
}
// GetVM gets information for a specific VM
func (c *Client) GetVMSharedFolder(ID, folderID string) (*SharedFolder, error) {
s := c.Endpoint
s.Path += "/api/vms/" + ID + "/folders/" + folderID
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "GET", s, nil)
sf := new(SharedFolder)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return sf, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return sf, err
}
if err = json.Unmarshal(body, &sf); err != nil {
return sf, err
}
return sf, nil
}
// GetVM gets information for a specific VM
func (c *Client) GetVM(ID string) (*VM, error) {
s := c.Endpoint
s.Path += "/api/vms/" + ID
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "GET", s, nil)
vm := new(VM)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return vm, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return vm, err
}
if err = json.Unmarshal(body, &vm); err != nil {
return vm, err
}
return vm, nil
}
func (c *Client) GetPowerVM(ID string) (*Response, error) {
s := c.Endpoint
s.Path += "/api/vms/power/" + ID
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "GET", s, nil)
response := new(Response)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return response, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
if err = json.Unmarshal(body, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) GetVMIPAddress(ID string) (*Response, error) {
s := c.Endpoint
s.Path += "/api/vms/" + ID + "/ipaddress"
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "GET", s, nil)
r := new(Response)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return r, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return r, err
}
if err = json.Unmarshal(body, &r); err != nil {
return r, err
}
return r, nil
}
func (c *Client) PowerVM(ID, operation string) (*Response, error) {
s := c.Endpoint
s.Path += "/api/vms/power/" + ID
b := bytes.NewBufferString(operation)
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "PATCH", s, b)
response := new(Response)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return response, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return response, err
}
if err = json.Unmarshal(body, &response); err != nil {
return response, err
}
return response, nil
}
func (c *Client) DeleteVM(ID string) error {
s := c.Endpoint
s.Path += "/api/vms/" + ID
create := VMDelete{
ID: ID,
}
out, err := json.Marshal(create)
b := bytes.NewBufferString(string(out))
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "DELETE", s, b)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
// CloneVM creates a clone starting from an existing VM
func (c *Client) CloneVM(ID, name, tag string) (*CloneVM, error) {
s := c.Endpoint
s.Path += "/api/vms"
create := CloneVM{
ID: ID,
Name: name,
Tag: tag,
}
out, err := json.Marshal(create)
b := bytes.NewBufferString(string(out))
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "POST", s, b)
clonedvm := new(CloneVM)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return clonedvm, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return clonedvm, err
}
if err = json.Unmarshal(body, &clonedvm); err != nil {
return clonedvm, err
}
return clonedvm, nil
}
// ListVMs lists all the VMs available on AppCatalyst.
func (c *Client) ListVMs() ([]string, error) {
s := c.Endpoint
s.Path += "/api/vms"
// No point in checking for errors here
req := c.NewRequest(map[string]string{}, "GET", s, nil)
resp, err := checkResp(c.HTTP.Do(req))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var vmlist []string
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, &vmlist); err != nil {
return nil, err
}
return vmlist, nil
}
// NewRequest creates a new HTTP request and applies necessary auth headers if
// set.
func (c *Client) NewRequest(params map[string]string, method string, u url.URL, body io.Reader) *http.Request {
p := url.Values{}
// Build up our request parameters
for k, v := range params {
p.Add(k, v)
}
// Add the params to our URL
u.RawQuery = p.Encode()
log.Debugf("appcatalyst_driver: HTTP Debug - Method: %s URI: %s", method, u.String())
// Build the request, no point in checking for errors here as we're just
// passing a string version of an url.URL struct and http.NewRequest returns
// error only if can't process an url.ParseRequestURI().
req, _ := http.NewRequest(method, u.String(), body)
return req
}
// parseErr takes an error resp and returns a single string for use in error
// messages.
func parseErr(resp *http.Response) error {
errBody := new(Response)
// if there was an error decoding the body, just return that
if err := decodeBody(resp, errBody); err != nil {
return fmt.Errorf("error parsing error body for non-200 request: %s", err)
}
return fmt.Errorf("API Response: %d: %s", errBody.Code, errBody.Message)
}
// decodeBody is used to decode a response body
func decodeBody(resp *http.Response, out interface{}) error {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// Unmarshal the JSON body.
if err = json.Unmarshal(body, &out); err != nil {
return err
}
return nil
}
// checkResp wraps http.Client.Do() and verifies the request, if status code
// is 2XX it passes back the response, if it's a known invalid status code it
// parses the resultant XML error and returns a descriptive error, if the
// status code is not handled it returns a generic error with the status code.
func checkResp(resp *http.Response, err error) (*http.Response, error) {
// If err is already set, we can't connect to the endpoint.
if err != nil {
return resp, fmt.Errorf("Can't connect to AppCatalyst endpoint, make sure the REST API daemon is active")
}
switch i := resp.StatusCode; {
// Valid request, return the response.
case i == 200 || i == 201 || i == 202 || i == 204:
return resp, nil
// Invalid request, parse the XML error returned and return it.
case i == 400 || i == 401 || i == 403 || i == 404 || i == 405 || i == 406 || i == 408 || i == 409 || i == 415 || i == 500 || i == 503 || i == 504:
return nil, parseErr(resp)
// Unhandled response.
default:
return nil, fmt.Errorf("unhandled API response, please report this issue, status code: %s", resp.Status)
}
}