-
Notifications
You must be signed in to change notification settings - Fork 1
/
vapp.go
428 lines (382 loc) · 12.6 KB
/
vapp.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
package iland
import (
"encoding/json"
"fmt"
"time"
)
type VApp struct {
ID string `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
Status string `json:"status"`
Deployed bool `json:"deployed"`
IsExpired bool `json:"is_expired"`
AllocationModel string `json:"allocation_model"`
StorageProfileIDs []string `json:"storage_profiles"`
VdcID string `json:"vdc_uuid"`
OrgID string `json:"org_uuid"`
CompanyID string `json:"company_id"`
LocationID string `json:"location_id"`
CreatedDate int `json:"created_date"`
UpdatedDate int `json:"updated_date"`
}
type vappService struct {
client *client
}
func (s *vappService) postAction(vappID, action string, params []byte) (Task, error) {
resp, err := s.client.Post(fmt.Sprintf("/v1/vapps/%s/actions/%s", vappID, action), params)
if err != nil {
return Task{}, err
}
task := Task{}
err = unmarshalBody(resp, &task)
if err != nil {
return Task{}, err
}
return task, nil
}
func (s *vappService) Get(vappID string) (VApp, error) {
vapp := VApp{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s", vappID), &vapp)
if err != nil {
return VApp{}, err
}
return vapp, nil
}
func (s *vappService) Delete(vappID string) (Task, error) {
resp, err := s.client.Delete(fmt.Sprintf("/v1/vapps/%s", vappID))
if err != nil {
return Task{}, err
}
task := Task{}
err = unmarshalBody(resp, &task)
if err != nil {
return Task{}, err
}
return task, nil
}
func (s *vappService) GetVirtualMachines(vappID string) ([]VirtualMachine, error) {
schema := struct {
VirtualMachines []VirtualMachine `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/vms", vappID), &schema)
if err != nil {
return []VirtualMachine{}, err
}
return schema.VirtualMachines, nil
}
func (s *vappService) GetNetworks(vappID string) ([]VAppNetwork, error) {
schema := struct {
Networks []VAppNetwork `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/networks", vappID), &schema)
if err != nil {
return []VAppNetwork{}, err
}
return schema.Networks, nil
}
func (s *vappService) AddOrgNetwork(vappID, orgVdcNetworkID string) (Task, error) {
resp, err := s.client.Post(fmt.Sprintf("/v1/vapps/%s/org-vdc-network/%s", vappID, orgVdcNetworkID), []byte{})
if err != nil {
return Task{}, err
}
task := Task{}
err = unmarshalBody(resp, &task)
if err != nil {
return Task{}, err
}
return task, nil
}
func (s *vappService) UpdateName(vappID, name string) (Task, error) {
params := struct {
Name string `json:"name"`
}{
Name: name,
}
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "update-name", data)
}
func (s *vappService) UpdateDescription(vappID, description string) (Task, error) {
params := struct {
Description string `json:"description"`
}{
Description: description,
}
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "update-name", data)
}
type CopyVAppParams struct {
Name string `json:"name"`
VdcID string `json:"vdc_uuid"`
}
func (s *vappService) Copy(vappID string, params CopyVAppParams) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "copy", data)
}
type MoveVAppParams struct {
Name string `json:"name"`
VdcID string `json:"vdc_uuid"`
}
func (s *vappService) Move(vappID string, params MoveVAppParams) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "move", data)
}
type BuildVirtualMachineParams struct {
Name string `json:"name"`
Description string `json:"description"`
ComputerName string `json:"computer_name"`
VAppTemplateID string `json:"vapp_template_uuid"`
VirtualMachineTemplateID string `json:"vm_template_uuid"`
StorageProfileID string `json:"storage_profile_uuid"`
CPUCount int `json:"number_of_cpus,omitempty"`
CPUCoresPerSocket int `json:"cpu_cores_per_socket,omitempty"`
EnableCPUVirtualization bool `json:"expose_cpu_virtualization"`
MemoryMB int `json:"ram,omitempty"`
HardwareVersion int `json:"hardware_version,omitempty"`
BootDelay int `json:"boot_delay,omitempty"`
Disks []Disk `json:"disks"`
Nics []BuildNicParams `json:"nics"`
}
type BuildNicParams struct {
IPAssignment string `json:"ip_assignment,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
Primary bool `json:"primary_vnic"`
Type string `json:"network_adapter_type,omitempty"`
NetworkID string `json:"network_uuid,omitempty"`
}
func (s *vappService) BuildVirtualMachines(vappID string, params []BuildVirtualMachineParams) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "build-vms", data)
}
type AddTemplateVirtualMachineParams struct {
Name string `json:"name"`
Description string `json:"description"`
NetworkID string `json:"network_uuid,omitempty"`
VAppTemplateID string `json:"vapp_template_uuid"`
TemplateVirtualMachineID string `json:"vm_template_uuid"`
IPAddress string `json:"ip_address,omitempty"`
StorageProfileID string `json:"storage_profile_uuid,omitempty"`
IPAddressingMode string `json:"ip_address_mode,omitempty"`
}
func (s *vappService) AddTemplateVirtualMachines(vappID string, params []AddTemplateVirtualMachineParams) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "add-vms-from-templates", data)
}
type CreateVAppNetworkParams struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ParentNetworkID string `json:"parent_network_uuid,omitempty"`
Gateway string `json:"gateway_address"`
Netmask string `json:"network_mask"`
IPRanges []IPRange `json:"ip_ranges"`
PrimaryDNS string `json:"primary_dns,omitempty"`
SecondaryDNS string `json:"secondary_dns,omitempty"`
DNSSuffix string `json:"dns_suffix,omitempty"`
}
func (s *vappService) CreateNetwork(vappID string, params CreateVAppNetworkParams) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "add-vapp-network", data)
}
func (s *vappService) PowerOn(vappID string) (Task, error) {
return s.postAction(vappID, "poweron", []byte{})
}
func (s *vappService) PowerOff(vappID string) (Task, error) {
return s.postAction(vappID, "poweroff", []byte{})
}
func (s *vappService) Shutdown(vappID string) (Task, error) {
return s.postAction(vappID, "shutdown", []byte{})
}
func (s *vappService) Reboot(vappID string) (Task, error) {
return s.postAction(vappID, "reboot", []byte{})
}
func (s *vappService) Reset(vappID string) (Task, error) {
return s.postAction(vappID, "reset", []byte{})
}
func (s *vappService) Suspend(vappID string) (Task, error) {
return s.postAction(vappID, "suspend", []byte{})
}
func (s *vappService) GetCurrentBill(vappID string) (Billing, error) {
billing := Billing{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/billing", vappID), &billing)
if err != nil {
return Billing{}, err
}
return billing, nil
}
func (s *vappService) GetBill(vappID string, month, year int) (Billing, error) {
billing := Billing{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/billing?month=%d&year=%d", vappID, month, year), &billing)
if err != nil {
return Billing{}, err
}
return billing, nil
}
func (s *vappService) GetAvailableStorageProfiles(vappID string) ([]StorageProfile, error) {
schema := struct {
StorageProfiles []StorageProfile `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/available-storage-profiles", vappID), &schema)
if err != nil {
return []StorageProfile{}, err
}
return schema.StorageProfiles, nil
}
func (s *vappService) GetMetadata(vappID string) ([]Metadata, error) {
schema := struct {
Metadata []Metadata `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/metadata", vappID), &schema)
if err != nil {
return []Metadata{}, err
}
return schema.Metadata, nil
}
func (s *vappService) UpdateMetadata(vappID string, metadata []Metadata) (Task, error) {
payload, err := json.Marshal(&metadata)
if err != nil {
return Task{}, err
}
resp, err := s.client.Put(fmt.Sprintf("/v1/vapps/%s/metadata", vappID), payload)
if err != nil {
return Task{}, err
}
task := Task{}
err = unmarshalBody(resp, &task)
if err != nil {
return Task{}, err
}
return task, nil
}
func (s *vappService) DeleteMetadata(vappID, metadataKey string) (Task, error) {
resp, err := s.client.Delete(fmt.Sprintf("/v1/vapps/%s/metadata/%s", vappID, metadataKey))
if err != nil {
return Task{}, err
}
task := Task{}
err = unmarshalBody(resp, &task)
if err != nil {
return Task{}, err
}
return task, nil
}
func (s *vappService) HasSnapshot(vappID string) (bool, error) {
schema := struct {
HasSnapshot bool `json:"has_snapshot"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/has-snapshot", vappID), &schema)
if err != nil {
return false, err
}
return schema.HasSnapshot, nil
}
func (s *vappService) GetSnapshot(vappID string) (Snapshot, error) {
snapshot := Snapshot{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/snapshot", vappID), &snapshot)
if err != nil {
return Snapshot{}, err
}
return snapshot, nil
}
func (s *vappService) CreateSnapshot(vappID string) (Task, error) {
params := struct {
Memory bool `json:"memory"`
Quiesce bool `json:"quiesce"`
}{
Memory: false,
Quiesce: false,
}
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "create-snapshot", data)
}
func (s *vappService) RestoreSnapshot(vappID string) (Task, error) {
return s.postAction(vappID, "restore-snapshot", []byte{})
}
func (s *vappService) RemoveSnapshot(vappID string) (Task, error) {
return s.postAction(vappID, "remove-snapshot", []byte{})
}
type VAppStartupSetting struct {
VirtualMachineName string `json:"vm_name"`
Order int `json:"ord"`
StartAction string `json:"startup_action"`
StopAction string `json:"stop_action"`
StartDelay int `json:"start_delay"`
StopDelay int `json:"stop_delay"`
}
func (s *vappService) GetStartupSettings(vappID string) ([]VAppStartupSetting, error) {
schema := struct {
Settings []VAppStartupSetting `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/startup-section", vappID), &schema)
if err != nil {
return []VAppStartupSetting{}, err
}
return schema.Settings, nil
}
func (s *vappService) UpdateStartupSettings(vappID string, params []VAppStartupSetting) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
return s.postAction(vappID, "update-startup-section", data)
}
func (s *vappService) GetPerformanceCounters(vappID string) ([]PerformanceCounter, error) {
schema := struct {
Counters []PerformanceCounter `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/performance-counters", vappID), &schema)
if err != nil {
return []PerformanceCounter{}, err
}
return schema.Counters, nil
}
func (s *vappService) GetPerformance(vappID string, counter PerformanceCounter, start, end time.Time) (Performance, error) {
startNano := getUnixMilliseconds(start)
endNano := getUnixMilliseconds(end)
performance := Performance{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/performance/%s::%s::%s?start=%d&end=%d", vappID, counter.Group, counter.Name, counter.Type, startNano, endNano), &performance)
if err != nil {
return Performance{}, err
}
return performance, nil
}
type VAppSummary struct {
NumberOfVms int `json:"number_of_vms"`
ReservedCPU float64 `json:"reserved_cpu"`
ConsumedCPU float64 `json:"consumed_cpu"`
ReservedMemory float64 `json:"reserved_mem"`
ConsumedMemory float64 `json:"consumed_mem"`
ProvisionedDisk float64 `json:"provisioned_disk"`
ConsumedDisk float64 `json:"consumed_disk"`
}
func (s *vappService) GetSummary(vdcID string) (VAppSummary, error) {
summary := VAppSummary{}
err := s.client.getObject(fmt.Sprintf("/v1/vapps/%s/summary", vdcID), &summary)
if err != nil {
return VAppSummary{}, err
}
return summary, nil
}