-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdc.go
251 lines (228 loc) · 7.87 KB
/
vdc.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
package iland
import (
"encoding/json"
"fmt"
"time"
)
type Vdc struct {
ID string `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
AllocationModel string `json:"allocation_model"`
ReservedCPU int `json:"reserved_cpu"`
AllocatedCPU int `json:"alloc_cpu"`
ReservedMemory int `json:"reserved_mem"`
AllocatedMemory int `json:"allocated_memory"`
NetworkQuota int `json:"network_quota"`
UsedNetworkCount int `json:"used_network_count"`
MaxHardwareVersion string `json:"max_hardware_version"`
DiskLimit int `json:"disk_limit"`
AdvancedDiskLimit int `json:"contracted_advanced_disk_limit"`
SSDDiskLimit int `json:"contracted_ssd_disk_limit"`
ArchiveDiskLimit int `json:"contracted_archive_disk_limit"`
CompanyID string `json:"company_id"`
OrgID string `json:"org_uuid"`
VCenterName string `json:"vcenter_name"`
VCloudHref string `json:"vcloud_href"`
LocationID string `json:"location_id"`
UpdatedDate int `json:"updated_date"`
}
type vdcService struct {
client *client
}
func (s *vdcService) Get(vdcID string) (Vdc, error) {
vdc := Vdc{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s", vdcID), &vdc)
if err != nil {
return Vdc{}, err
}
return vdc, nil
}
func (s *vdcService) GetStorageProfiles(vdcID string) ([]StorageProfile, error) {
schema := struct {
StorageProfiles []StorageProfile `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/storage-profiles", vdcID), &schema)
if err != nil {
return []StorageProfile{}, err
}
return schema.StorageProfiles, nil
}
type VdcSummary struct {
NumberOfVApps int `json:"number_of_vapps"`
NumberOfVms int `json:"number_of_vms"`
ReservedCPU float64 `json:"reserved_cpu"`
AllocatedCPU float64 `json:"allocated_cpu"`
ConfiguredCPU float64 `json:"configured_cpu"`
ConsumedCPU float64 `json:"consumed_cpu"`
ReservedMemory float64 `json:"reserved_mem"`
AllocatedMemory float64 `json:"allocated_mem"`
ConfiguredMemory float64 `json:"configured_mem"`
ConsumedMemory float64 `json:"consumed_mem"`
ProvisionedDisk float64 `json:"provisioned_disk"`
ConfiguredDisk float64 `json:"configured_disk"`
ConsumedDisk float64 `json:"consumed_disk"`
}
func (s *vdcService) GetSummary(vdcID string) (VdcSummary, error) {
summary := VdcSummary{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/summary", vdcID), &summary)
if err != nil {
return VdcSummary{}, err
}
return summary, nil
}
func (s *vdcService) GetVApps(vdcID string) ([]VApp, error) {
schema := struct {
VApps []VApp `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/vapps", vdcID), &schema)
if err != nil {
return []VApp{}, err
}
return schema.VApps, nil
}
func (s *vdcService) GetVirtualMachines(vdcID string) ([]VirtualMachine, error) {
schema := struct {
VirtualMachines []VirtualMachine `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/vms", vdcID), &schema)
if err != nil {
return []VirtualMachine{}, err
}
return schema.VirtualMachines, nil
}
func (s *vdcService) GetEdges(vdcID string) ([]Edge, error) {
schema := struct {
Edges []Edge `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/edges", vdcID), &schema)
if err != nil {
return []Edge{}, err
}
return schema.Edges, nil
}
func (s *vdcService) GetNetworks(vdcID string) ([]OrgVdcNetwork, error) {
schema := struct {
Networks []OrgVdcNetwork `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/org-vdc-networks", vdcID), &schema)
if err != nil {
return []OrgVdcNetwork{}, err
}
return schema.Networks, nil
}
func (s *vdcService) GetCurrentBill(vdcID string) (Billing, error) {
billing := Billing{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/billing", vdcID), &billing)
if err != nil {
return Billing{}, err
}
return billing, nil
}
func (s *vdcService) GetBill(vdcID string, month, year int) (Billing, error) {
billing := Billing{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/billing?month=%d&year=%d", vdcID, month, year), &billing)
if err != nil {
return Billing{}, err
}
return billing, nil
}
func (s *vdcService) GetCurrentVAppBill(vdcID string) ([]Billing, error) {
schema := struct {
Billing []Billing `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/vapp-bills", vdcID), &schema)
if err != nil {
return []Billing{}, err
}
return schema.Billing, nil
}
func (s *vdcService) GetVAppBill(vdcID string, month, year int) ([]Billing, error) {
schema := struct {
Billing []Billing `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/vapp-bills?month=%d&year=%d", vdcID, month, year), &schema)
if err != nil {
return []Billing{}, err
}
return schema.Billing, nil
}
func (s *vdcService) GetPerformanceCounters(vdcID string) ([]PerformanceCounter, error) {
schema := struct {
Counters []PerformanceCounter `json:"data"`
}{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/performance-counters", vdcID), &schema)
if err != nil {
return []PerformanceCounter{}, err
}
return schema.Counters, nil
}
func (s *vdcService) GetPerformance(vdcID string, counter PerformanceCounter, start, end time.Time) (Performance, error) {
startNano := getUnixMilliseconds(start)
endNano := getUnixMilliseconds(end)
performance := Performance{}
err := s.client.getObject(fmt.Sprintf("/v1/vdcs/%s/performance/%s::%s::%s?start=%d&end=%d", vdcID, counter.Group, counter.Name, counter.Type, startNano, endNano), &performance)
if err != nil {
return Performance{}, err
}
return performance, nil
}
type BuildVAppParams struct {
Name string `json:"name"`
Description string `json:"description"`
VirtualMachines []BuildVirtualMachineParams `json:"vms"`
}
func (s *vdcService) BuildVApp(vdcID string, params BuildVAppParams) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
resp, err := s.client.Post(fmt.Sprintf("/v1/vdcs/%s/actions/build-vapp", vdcID), data)
if err != nil {
return Task{}, err
}
task := Task{}
err = unmarshalBody(resp, &task)
if err != nil {
return Task{}, err
}
return task, nil
}
type DeployVAppTemplateParams struct {
Name string `json:"name"`
Description string `json:"description"`
VAppTemplateID string `json:"vapp_template_uuid"`
PreserveNetworks bool `json:"preserve_networks"`
VirtualMachines []DeployVAppTemplateVirtualMachineParams `json:"vms"`
}
type DeployVAppTemplateVirtualMachineParams struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
VirtualMachineTemplateID string `json:"vm_template_uuid"`
StorageProfileID string `json:"storage_profile_uuid,omitempty"`
Nics []VAppTemplateNicParams `json:"vnics"`
}
type VAppTemplateNicParams struct {
NetworkID string `json:"network_uuid,omitempty"`
IPAssignment string `json:"ip_assignment"`
IPAddress string `json:"ip_address,omitempty"`
PrimaryVNic bool `json:"primary_vnic"`
NetworkAdapterType string `json:"network_adapter_type"`
}
func (s *vdcService) DeployVAppTemplate(vdcID string, params DeployVAppTemplateParams) (Task, error) {
data, err := json.Marshal(¶ms)
if err != nil {
return Task{}, err
}
resp, err := s.client.Post(fmt.Sprintf("/v1/vdcs/%s/actions/add-vapp-from-template", vdcID), data)
if err != nil {
return Task{}, err
}
task := Task{}
err = unmarshalBody(resp, &task)
if err != nil {
return Task{}, err
}
return task, nil
}