-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdc.go
More file actions
410 lines (380 loc) · 10.9 KB
/
vdc.go
File metadata and controls
410 lines (380 loc) · 10.9 KB
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
package vcdusage
import (
"fmt"
"strings"
"github.com/destel/rill"
"github.com/joomcode/errorx"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
)
func isValidStatus(status string) bool {
valid := []int{2, 3, 4, 8}
for _, v := range valid {
if status == types.VAppStatuses[v] {
return true
}
}
return false
}
// VDC is a wrapper around a govcd.Vdc object with references to the corresponding Admin Org and
// vcdusage.Client.
type VDC struct {
Obj *govcd.Vdc
AdminOrg *govcd.AdminOrg
Client *Client
}
// VDCs is a slice of vcdusage.VDC wrappers.
type VDCs []VDC
// CoreCount retrieves the used CPU MHz for a VDC and calculates the number of cores used
// by all VDCs by dividing the total used CPU MHz by the CPU speed.
//
// For example, if the speed is 3.1 GHz and the used amount is 49.6, the core count is 16.
func (vdcs VDCs) CoreCount() uint64 {
vdcSlice := rill.FromSlice(vdcs, nil)
count := uint64(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
count += vdc.CoreCount()
return nil
})
return count
}
// Memory retrieves the amount of used memory to all VDCs, represented as a DataStorage type.
func (vdcs VDCs) Memory() DataStorage {
vdcSlice := rill.FromSlice(vdcs, nil)
mem := DataStorage(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
mem += vdc.Memory()
return nil
})
return mem
}
// Memory retrieves the amount of used storage to all VDCs, represented as a DataStorage type.
func (vdcs VDCs) Storage() DataStorage {
vdcSlice := rill.FromSlice(vdcs, nil)
stor := DataStorage(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
stor += vdc.Storage()
return nil
})
return stor
}
// VMCount retrieves the number of VMs deployed in all VDCs.
func (vdcs VDCs) VMCount() uint64 {
vdcSlice := rill.FromSlice(vdcs, nil)
count := uint64(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
count += vdc.VMCount()
return nil
})
return count
}
// PoweredOnVMCount retrieves the number of powered on VMs deployed in all VDCs.
func (vdcs VDCs) PoweredOnVMCount() uint64 {
vdcSlice := rill.FromSlice(vdcs, nil)
count := uint64(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
count += vdc.PoweredOnVMCount()
return nil
})
return count
}
// VMCountWithQuery retrieves the number of VMs matching all of the provided queries in all VDCs.
// If PoweredOn is false (default), VMs that are both powered on or off will be included.
func (vdcs VDCs) VMCountWithQuery(queries ...VMQuerySetter) uint64 {
vdcSlice := rill.FromSlice(vdcs, nil)
count := uint64(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
count += vdc.VMCountWithQuery(queries...)
return nil
})
return count
}
// VMCountWithQuery retrieves the number of cores on VMs matching all of the provided queries in
// all VDCs. If PoweredOn is false (default), VMs that are both powered on or off will be included.
func (vdcs VDCs) VMCoreCountWithQuery(queries ...VMQuerySetter) uint64 {
vdcSlice := rill.FromSlice(vdcs, nil)
count := uint64(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
count += vdc.VMCoreCountWithQuery(queries...)
return nil
})
return count
}
// Speed retrieves the max CPU speed of all VDCs in MHz. This is required for calculating core count.
func (vdcs VDCs) Speed() uint64 {
vdcSlice := rill.FromSlice(vdcs, nil)
speed := uint64(0)
rill.ForEach(vdcSlice, len(vdcs), func(vdc VDC) error {
vdcSpeed := vdc.Speed()
if vdcSpeed > speed {
speed = vdcSpeed
}
return nil
})
return speed
}
// Speed retrieves the CPU speed of a VDC in MHz. This is required for calculating core count.
func (vdc *VDC) Speed() uint64 {
avdc, err := vdc.AdminOrg.GetAdminVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return 0
}
if avdc.AdminVdc.VCpuInMhz2 == nil {
return 0
}
return uint64(*avdc.AdminVdc.VCpuInMhz2)
}
// CoreCount retrieves the used CPU MHz for a VDC and calculates the number of cores used
// by the VDC by dividing the total used CPU MHz by the CPU speed.
//
// For example, if the speed is 3.1 GHz and the used amount is 49.6, the core count is 16.
func (vdc *VDC) CoreCount() uint64 {
speed := vdc.Speed()
if speed == 0 {
return 0
}
cores := uint64(0)
avdc, err := vdc.AdminOrg.GetAdminVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return 0
}
for _, capacity := range avdc.AdminVdc.ComputeCapacity {
c := uint64(capacity.CPU.Used)
cores += c
}
return cores / speed
}
// Memory retrieves the amount of used memory to an oVDC, represented as a DataStorage type.
func (vdc *VDC) Memory() DataStorage {
avdc, err := vdc.AdminOrg.GetAdminVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return 0
}
bm := float64(0)
for _, capacity := range avdc.AdminVdc.ComputeCapacity {
switch capacity.Memory.Units {
case "KB":
bm += float64(capacity.Memory.Used * kb)
case "MB":
bm += float64(capacity.Memory.Used * mb)
case "GB":
bm += float64(capacity.Memory.Used * gb)
case "TB":
bm += float64(capacity.Memory.Used * tb)
default:
bm += float64(capacity.Memory.Used)
}
}
return DataStorage(bm)
}
// allStorageProfiles retrieves all storage profiles for a VDC and filters out partial duplicates, for
// example, when Veeam CDP creates a storage profile for the datastore.
func (vdc *VDC) allStorageProfiles() ([]*types.VdcStorageProfile, error) {
avdc, err := vdc.AdminOrg.GetAdminVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return nil, err
}
profiles := make([]*types.VdcStorageProfile, 0)
for _, stor := range avdc.AdminVdc.VdcStorageProfiles.VdcStorageProfile {
sp, err := vdc.Client.VCD.GetStorageProfileById(stor.ID)
if err != nil {
return nil, err
}
profiles = append(profiles, sp)
}
return profiles, nil
}
// storageProfile retrieves the default storage profile for the VDC.
func (vdc *VDC) defaultStorageProfile() (*types.VdcStorageProfile, error) {
avdc, err := vdc.AdminOrg.GetAdminVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return nil, err
}
ref, err := avdc.GetDefaultStorageProfileReference()
if err != nil {
return nil, err
}
sp, err := vdc.Client.VCD.GetStorageProfileById(ref.ID)
if err != nil {
return nil, err
}
return sp, nil
}
// Storage retrieves the total amount of 'requested' storage for an oVDC using the oVDC default
// storage policy.
func (vdc *VDC) Storage() DataStorage {
profile, err := vdc.defaultStorageProfile()
if err != nil {
return 0
}
sb := profile.StorageUsedMB * mb
return DataStorage(sb)
}
// StorageAll retrieves the total amount of used storage for an oVDC, totaling the 'requested'
// storage for all storage policies.
func (vdc *VDC) StorageAll() DataStorage {
profiles, err := vdc.allStorageProfiles()
if err != nil {
return 0
}
bs := float64(0)
for _, prof := range profiles {
sb := prof.StorageUsedMB * mb
bs += float64(sb)
}
return DataStorage(bs)
}
// VMCount retrieves the number of VMs deployed in the VDC.
func (vdc *VDC) VMCount() uint64 {
ovdc, err := vdc.AdminOrg.GetVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return 0
}
vms, err := ovdc.QueryVmList(types.VmQueryFilterAll)
if err != nil {
return 0
}
count := uint64(0)
for _, vm := range vms {
vm := vm
if isValidStatus(vm.Status) && !vm.VAppTemplate && !vm.Deleted {
count++
}
}
return count
}
// PoweredOnVMCount retrieves the number of powered-on VMs deployed in the VDC.
func (vdc *VDC) PoweredOnVMCount() uint64 {
ovdc, err := vdc.AdminOrg.GetVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return 0
}
vms, err := ovdc.QueryVmList(types.VmQueryFilterAll)
if err != nil {
return 0
}
count := uint64(0)
for _, vm := range vms {
if vm.Status == types.VAppStatuses[4] && !vm.VAppTemplate && !vm.Deleted {
count++
}
}
return count
}
func (vdc *VDC) queryVMs(queries ...VMQuerySetter) ([]*types.QueryResultVMRecordType, error) {
ovdc, err := vdc.AdminOrg.GetVDCById(vdc.Obj.Vdc.ID, false)
if err != nil {
return nil, err
}
query := &VMQuery{
Name: nil,
GuestOS: nil,
PoweredOn: false,
}
for _, set := range queries {
set(query)
}
vms, err := ovdc.QueryVmList(types.VmQueryFilterOnlyDeployed)
if err != nil {
return nil, err
}
if query.PoweredOn {
_vms := make([]*types.QueryResultVMRecordType, 0, len(vms))
for _, vm := range vms {
if vm.Status == types.VAppStatuses[4] {
_vms = append(_vms, vm)
}
}
vms = _vms
}
if query.Name != nil {
_vms := make([]*types.QueryResultVMRecordType, 0, len(vms))
for _, vm := range vms {
if query.Name.MatchString(vm.Name) {
_vms = append(_vms, vm)
}
}
vms = _vms
}
if query.GuestOS != nil {
_vms := make([]*types.QueryResultVMRecordType, 0, len(vms))
for _, vm := range vms {
if query.GuestOS.MatchString(vm.GuestOS) {
_vms = append(_vms, vm)
}
}
vms = _vms
}
return vms, nil
}
// VMCountWithQuery retrieves the number of VMs matching all of the provided queries.
// If PoweredOn is false (default), VMs that are both powered on or off will be included.
func (vdc *VDC) VMCountWithQuery(queries ...VMQuerySetter) uint64 {
vms, err := vdc.queryVMs(queries...)
if err != nil {
return 0
}
return uint64(len(vms))
}
// VMCoreCountWithQuery retrieves the number cores on VMs matching all of the provided queries.
// If PoweredOn is false (default), VMs that are both powered on or off will be included.
func (vdc *VDC) VMCoreCountWithQuery(queries ...VMQuerySetter) uint64 {
vms, err := vdc.queryVMs(queries...)
if err != nil {
return 0
}
count := uint64(0)
for _, vm := range vms {
count += uint64(vm.Cpus)
}
return count
}
// Org retrieves a vCloud Organization object.
func (client *Client) Org(orgID string) (*govcd.AdminOrg, error) {
if !strings.HasPrefix(orgID, "urn:vcloud:org:") {
orgID = fmt.Sprintf("urn:vcloud:org:%s", orgID)
}
org, err := client.VCD.GetAdminOrgById(orgID)
if err != nil {
err = errorx.Decorate(err, "failed to retrieve org '%s'", orgID)
return nil, err
}
return org, nil
}
// VDC retrieves a single VDC associated with an organization by its ID and provides a wrapper for
// utilization functions for each VDC.
func (client *Client) VDC(orgID string, id string) (*VDC, error) {
org, err := client.Org(orgID)
if err != nil {
return nil, err
}
obj, err := org.GetVDCById(id, false)
if err != nil {
err = errorx.Decorate(err, "failed to retrieve VDC '%s' for org '%s'", id, orgID)
return nil, err
}
vdc := &VDC{
Obj: obj,
AdminOrg: org,
Client: client,
}
return vdc, nil
}
// VDCs retrieves all VDCs associated with an organization and provides a wrapper for utilization
// functions for each VDC.
func (client *Client) VDCs(orgID string) (VDCs, error) {
org, err := client.Org(orgID)
if err != nil {
return nil, err
}
vdcs, err := org.GetAllVDCs(false)
if err != nil {
err = errorx.Decorate(err, "failed to retrieve VDCs for org '%s'", orgID)
return nil, err
}
vdcObjs := make([]VDC, 0, len(vdcs))
for _, vdc := range vdcs {
vdcObjs = append(vdcObjs, VDC{Obj: vdc, AdminOrg: org, Client: client})
}
return vdcObjs, nil
}