forked from mindjiver/gopherstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtualmachine.go
164 lines (149 loc) · 5.6 KB
/
virtualmachine.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
package gopherstack
import (
"encoding/base64"
"net/url"
"strings"
)
// Deploys a Virtual Machine and returns it's id
func (c CloudstackClient) DeployVirtualMachine(serviceofferingid string, templateid string, zoneid string, account string, diskofferingid string, displayname string, networkids []string, keypair string, projectid string, userdata string, hypervisor string) (DeployVirtualMachineResponse, error) {
var resp DeployVirtualMachineResponse
params := url.Values{}
params.Set("serviceofferingid", serviceofferingid)
params.Set("templateid", templateid)
params.Set("zoneid", zoneid)
// params.Set("account", account)
params.Set("diskofferingid", diskofferingid)
params.Set("displayname", displayname)
params.Set("hypervisor", hypervisor)
params.Set("networkids", strings.Join(networkids, ","))
params.Set("keypair", keypair)
// params.Set("projectid", projectid)
if userdata != "" {
params.Set("userdata", base64.StdEncoding.EncodeToString([]byte(userdata)))
}
response, err := NewRequest(c, "deployVirtualMachine", params)
if err != nil {
return resp, err
}
resp = response.(DeployVirtualMachineResponse)
return resp, nil
}
func (c CloudstackClient) UpdateVirtualMachine(id string, displayname string, group string, haenable string, ostypeid string, userdata string) (UpdateVirtualMachineResponse, error) {
var resp UpdateVirtualMachineResponse
params := url.Values{}
params.Set("id", id)
params.Set("displayname", displayname)
// params.Set("group", string)
// params.Set("haenable", haenable)
// params.Set("ostypeid", ostypeid)
params.Set("userdata", base64.StdEncoding.EncodeToString([]byte(userdata)))
response, err := NewRequest(c, "updateVirtualMachine", params)
if err != nil {
return resp, err
}
resp = response.(UpdateVirtualMachineResponse)
return resp, err
}
// Stops a Virtual Machine
func (c CloudstackClient) StopVirtualMachine(id string) (StopVirtualMachineResponse, error) {
var resp StopVirtualMachineResponse
params := url.Values{}
params.Set("id", id)
response, err := NewRequest(c, "stopVirtualMachine", params)
if err != nil {
return resp, err
}
resp = response.(StopVirtualMachineResponse)
return resp, err
}
// Destroys a Virtual Machine
func (c CloudstackClient) DestroyVirtualMachine(id string) (DestroyVirtualMachineResponse, error) {
var resp DestroyVirtualMachineResponse
params := url.Values{}
params.Set("id", id)
response, err := NewRequest(c, "destroyVirtualMachine", params)
if err != nil {
return resp, err
}
resp = response.(DestroyVirtualMachineResponse)
return resp, nil
}
// Returns Cloudstack string representation of the Virtual Machine state
func (c CloudstackClient) ListVirtualMachines(id string) (ListVirtualMachinesResponse, error) {
var resp ListVirtualMachinesResponse
params := url.Values{}
params.Set("id", id)
response, err := NewRequest(c, "listVirtualMachines", params)
if err != nil {
return resp, err
}
resp = response.(ListVirtualMachinesResponse)
return resp, err
}
type DeployVirtualMachineResponse struct {
Deployvirtualmachineresponse struct {
ID string `json:"id"`
Jobid string `json:"jobid"`
} `json:"deployvirtualmachineresponse"`
}
type DestroyVirtualMachineResponse struct {
Destroyvirtualmachineresponse struct {
Jobid string `json:"jobid"`
} `json:"destroyvirtualmachineresponse"`
}
type StopVirtualMachineResponse struct {
Stopvirtualmachineresponse struct {
Jobid string `json:"jobid"`
} `json:"stopvirtualmachineresponse"`
}
type Nic struct {
Gateway string `json:"gateway"`
ID string `json:"id"`
Ipaddress string `json:"ipaddress"`
Isdefault bool `json:"isdefault"`
Macaddress string `json:"macaddress"`
Netmask string `json:"netmask"`
Networkid string `json:"networkid"`
Traffictype string `json:"traffictype"`
Type string `json:"type"`
}
type Virtualmachine struct {
Account string `json:"account"`
Cpunumber float64 `json:"cpunumber"`
Cpuspeed float64 `json:"cpuspeed"`
Created string `json:"created"`
Displayname string `json:"displayname"`
Domain string `json:"domain"`
Domainid string `json:"domainid"`
Guestosid string `json:"guestosid"`
Haenable bool `json:"haenable"`
Hypervisor string `json:"hypervisor"`
ID string `json:"id"`
Keypair string `json:"keypair"`
Memory float64 `json:"memory"`
Name string `json:"name"`
Nic []Nic `json:"nic"`
Passwordenabled bool `json:"passwordenabled"`
Rootdeviceid float64 `json:"rootdeviceid"`
Rootdevicetype string `json:"rootdevicetype"`
Securitygroup []interface{} `json:"securitygroup"`
Serviceofferingid string `json:"serviceofferingid"`
Serviceofferingname string `json:"serviceofferingname"`
State string `json:"state"`
Tags []interface{} `json:"tags"`
Templatedisplaytext string `json:"templatedisplaytext"`
Templateid string `json:"templateid"`
Templatename string `json:"templatename"`
Zoneid string `json:"zoneid"`
Zonename string `json:"zonename"`
}
type ListVirtualMachinesResponse struct {
Listvirtualmachinesresponse struct {
Count float64 `json:"count"`
Virtualmachine []Virtualmachine `json:"virtualmachine"`
} `json:"listvirtualmachinesresponse"`
}
type UpdateVirtualMachineResponse struct {
Updatevirtualmachineresponse struct {
}
}