-
Notifications
You must be signed in to change notification settings - Fork 0
/
virtualapp.go
76 lines (65 loc) · 1.84 KB
/
virtualapp.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
package abiquo_api
import (
"encoding/json"
)
type VirtualAppCollection struct {
AbstractCollection
Collection []VirtualApp
}
type VirtualApp struct {
DTO
Error int `json:"error,omitempty"`
HighDisponibility int `json:"highDisponibility,omitempty"`
Name string `json:"name,omitempty"`
PublicApp int `json:"publicApp,omitempty"`
State string `json:"state,omitempty"`
}
func (v *VirtualApp) Delete(c *AbiquoClient) error {
edit_lnk, _ := v.GetLink("edit")
_, err := c.checkResponse(c.client.R().Delete(edit_lnk.Href))
if err != nil {
return err
}
return nil
}
func (v *VirtualApp) GetVMs(c *AbiquoClient) ([]VirtualMachine, error) {
var vms []VirtualMachine
var vmsCol VirtualMachineCollection
vms_raw, err := v.FollowLink("virtualmachines", c)
if err != nil {
return vms, err
}
json.Unmarshal(vms_raw.Body(), &vmsCol)
for {
for _, vm := range vmsCol.Collection {
vms = append(vms, vm)
}
if vmsCol.HasNext() {
next_link := vmsCol.GetNext()
vms_raw, err = c.checkResponse(c.client.R().SetHeader("Accept", "application/vnd.abiquo.virtualmachines+json").
Get(next_link.Href))
if err != nil {
return vms, err
}
vmsCol = VirtualMachineCollection{}
json.Unmarshal(vms_raw.Body(), &vmsCol)
} else {
break
}
}
return vms, nil
}
func (v *VirtualApp) CreateVM(vm VirtualMachine, c *AbiquoClient) (VirtualMachine, error) {
var vm_created VirtualMachine
body, _ := json.Marshal(vm)
vms_lnk, _ := v.GetLink("virtualmachines")
vm_raw, err := c.checkResponse(c.client.R().SetHeader("Accept", "application/vnd.abiquo.virtualmachine+json").
SetHeader("Content-Type", "application/vnd.abiquo.virtualmachine+json").
SetBody(body).
Post(vms_lnk.Href))
if err != nil {
return vm_created, err
}
json.Unmarshal(vm_raw.Body(), &vm_created)
return vm_created, nil
}