forked from vmware/govmomi
/
finder.go
699 lines (562 loc) · 15.9 KB
/
finder.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package find
import (
"errors"
"fmt"
"path"
"github.com/vmware/govmomi/list"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"golang.org/x/net/context"
)
type Finder struct {
client *vim25.Client
recurser list.Recurser
dc *object.Datacenter
folders *object.DatacenterFolders
}
func NewFinder(client *vim25.Client, all bool) *Finder {
f := &Finder{
client: client,
recurser: list.Recurser{
Collector: property.DefaultCollector(client),
All: all,
},
}
return f
}
func (f *Finder) SetDatacenter(dc *object.Datacenter) *Finder {
f.dc = dc
f.folders = nil
return f
}
type findRelativeFunc func(ctx context.Context) (object.Reference, error)
func (f *Finder) find(ctx context.Context, fn findRelativeFunc, tl bool, arg string) ([]list.Element, error) {
root := list.Element{
Path: "/",
Object: object.NewRootFolder(f.client),
}
parts := list.ToParts(arg)
if len(parts) > 0 {
switch parts[0] {
case "..": // Not supported; many edge case, little value
return nil, errors.New("cannot traverse up a tree")
case ".": // Relative to whatever
pivot, err := fn(ctx)
if err != nil {
return nil, err
}
mes, err := mo.Ancestors(ctx, f.client, f.client.ServiceContent.PropertyCollector, pivot.Reference())
if err != nil {
return nil, err
}
for _, me := range mes {
// Skip root entity in building inventory path.
if me.Parent == nil {
continue
}
root.Path = path.Join(root.Path, me.Name)
}
root.Object = pivot
parts = parts[1:]
}
}
f.recurser.TraverseLeafs = tl
es, err := f.recurser.Recurse(ctx, root, parts)
if err != nil {
return nil, err
}
return es, nil
}
func (f *Finder) datacenter() (*object.Datacenter, error) {
if f.dc == nil {
return nil, errors.New("please specify a datacenter")
}
return f.dc, nil
}
func (f *Finder) dcFolders(ctx context.Context) (*object.DatacenterFolders, error) {
if f.folders != nil {
return f.folders, nil
}
dc, err := f.datacenter()
if err != nil {
return nil, err
}
folders, err := dc.Folders(ctx)
if err != nil {
return nil, err
}
f.folders = folders
return f.folders, nil
}
func (f *Finder) dcReference(_ context.Context) (object.Reference, error) {
dc, err := f.datacenter()
if err != nil {
return nil, err
}
return dc, nil
}
func (f *Finder) vmFolder(ctx context.Context) (object.Reference, error) {
folders, err := f.dcFolders(ctx)
if err != nil {
return nil, err
}
return folders.VmFolder, nil
}
func (f *Finder) hostFolder(ctx context.Context) (object.Reference, error) {
folders, err := f.dcFolders(ctx)
if err != nil {
return nil, err
}
return folders.HostFolder, nil
}
func (f *Finder) datastoreFolder(ctx context.Context) (object.Reference, error) {
folders, err := f.dcFolders(ctx)
if err != nil {
return nil, err
}
return folders.DatastoreFolder, nil
}
func (f *Finder) networkFolder(ctx context.Context) (object.Reference, error) {
folders, err := f.dcFolders(ctx)
if err != nil {
return nil, err
}
return folders.NetworkFolder, nil
}
func (f *Finder) rootFolder(_ context.Context) (object.Reference, error) {
return object.NewRootFolder(f.client), nil
}
func (f *Finder) managedObjectList(ctx context.Context, path string, tl bool) ([]list.Element, error) {
fn := f.rootFolder
if f.dc != nil {
fn = f.dcReference
}
if len(path) == 0 {
path = "."
}
return f.find(ctx, fn, tl, path)
}
func (f *Finder) ManagedObjectList(ctx context.Context, path string) ([]list.Element, error) {
return f.managedObjectList(ctx, path, false)
}
func (f *Finder) ManagedObjectListChildren(ctx context.Context, path string) ([]list.Element, error) {
return f.managedObjectList(ctx, path, true)
}
func (f *Finder) DatacenterList(ctx context.Context, path string) ([]*object.Datacenter, error) {
es, err := f.find(ctx, f.rootFolder, false, path)
if err != nil {
return nil, err
}
var dcs []*object.Datacenter
for _, e := range es {
ref := e.Object.Reference()
if ref.Type == "Datacenter" {
dcs = append(dcs, object.NewDatacenter(f.client, ref))
}
}
if len(dcs) == 0 {
return nil, &NotFoundError{"datacenter", path}
}
return dcs, nil
}
func (f *Finder) Datacenter(ctx context.Context, path string) (*object.Datacenter, error) {
dcs, err := f.DatacenterList(ctx, path)
if err != nil {
return nil, err
}
if len(dcs) > 1 {
return nil, &MultipleFoundError{"datacenter", path}
}
return dcs[0], nil
}
func (f *Finder) DefaultDatacenter(ctx context.Context) (*object.Datacenter, error) {
dc, err := f.Datacenter(ctx, "*")
if err != nil {
return nil, toDefaultError(err)
}
return dc, nil
}
func (f *Finder) DatastoreList(ctx context.Context, path string) ([]*object.Datastore, error) {
return f.DatastoreListRecursive(ctx, path, false)
}
// DatastoreListRecursive allows for recursive calls to DatastoreList
func (f *Finder) DatastoreListRecursive(ctx context.Context, path string, recursive bool) ([]*object.Datastore, error) {
es, err := f.find(ctx, f.datastoreFolder, recursive, path)
if err != nil {
return nil, err
}
var dss []*object.Datastore
for _, e := range es {
ref := e.Object.Reference()
if ref.Type == "Datastore" {
ds := object.NewDatastore(f.client, ref)
ds.InventoryPath = e.Path
dss = append(dss, ds)
}
}
if len(dss) == 0 {
return nil, &NotFoundError{"datastore", path}
}
return dss, nil
}
func (f *Finder) Datastore(ctx context.Context, path string) (*object.Datastore, error) {
dss, err := f.DatastoreList(ctx, path)
if err != nil {
return nil, err
}
if len(dss) > 1 {
return nil, &MultipleFoundError{"datastore", path}
}
return dss[0], nil
}
func (f *Finder) DefaultDatastore(ctx context.Context) (*object.Datastore, error) {
ds, err := f.Datastore(ctx, "*")
if err != nil {
return nil, toDefaultError(err)
}
return ds, nil
}
func (f *Finder) ComputeResourceList(ctx context.Context, path string) ([]*object.ComputeResource, error) {
es, err := f.find(ctx, f.hostFolder, false, path)
if err != nil {
return nil, err
}
var crs []*object.ComputeResource
for _, e := range es {
var cr *object.ComputeResource
switch o := e.Object.(type) {
case mo.ComputeResource, mo.ClusterComputeResource:
cr = object.NewComputeResource(f.client, o.Reference())
default:
continue
}
cr.InventoryPath = e.Path
crs = append(crs, cr)
}
if len(crs) == 0 {
return nil, &NotFoundError{"compute resource", path}
}
return crs, nil
}
func (f *Finder) ComputeResource(ctx context.Context, path string) (*object.ComputeResource, error) {
crs, err := f.ComputeResourceList(ctx, path)
if err != nil {
return nil, err
}
if len(crs) > 1 {
return nil, &MultipleFoundError{"compute resource", path}
}
return crs[0], nil
}
func (f *Finder) DefaultComputeResource(ctx context.Context) (*object.ComputeResource, error) {
cr, err := f.ComputeResource(ctx, "*")
if err != nil {
return nil, toDefaultError(err)
}
return cr, nil
}
func (f *Finder) ClusterComputeResourceList(ctx context.Context, path string) ([]*object.ClusterComputeResource, error) {
es, err := f.find(ctx, f.hostFolder, false, path)
if err != nil {
return nil, err
}
var ccrs []*object.ClusterComputeResource
for _, e := range es {
var ccr *object.ClusterComputeResource
switch o := e.Object.(type) {
case mo.ClusterComputeResource:
ccr = object.NewClusterComputeResource(f.client, o.Reference())
default:
continue
}
ccr.InventoryPath = e.Path
ccrs = append(ccrs, ccr)
}
if len(ccrs) == 0 {
return nil, &NotFoundError{"cluster", path}
}
return ccrs, nil
}
func (f *Finder) ClusterComputeResource(ctx context.Context, path string) (*object.ClusterComputeResource, error) {
ccrs, err := f.ClusterComputeResourceList(ctx, path)
if err != nil {
return nil, err
}
if len(ccrs) > 1 {
return nil, &MultipleFoundError{"cluster", path}
}
return ccrs[0], nil
}
func (f *Finder) HostSystemList(ctx context.Context, path string) ([]*object.HostSystem, error) {
return f.HostSystemListRecursive(ctx, path, false)
}
// HostSystemListRecursive allows for recursive calls of HostSystemList
func (f *Finder) HostSystemListRecursive(ctx context.Context, path string, recursive bool) ([]*object.HostSystem, error) {
es, err := f.find(ctx, f.hostFolder, recursive, path)
if err != nil {
return nil, err
}
var hss []*object.HostSystem
for _, e := range es {
var hs *object.HostSystem
switch o := e.Object.(type) {
case mo.HostSystem:
hs = object.NewHostSystem(f.client, o.Reference())
case mo.ComputeResource:
cr := object.NewComputeResource(f.client, o.Reference())
hosts, err := cr.Hosts(ctx)
if err != nil {
return nil, err
}
hs = object.NewHostSystem(f.client, hosts[0])
default:
continue
}
hs.InventoryPath = e.Path
hss = append(hss, hs)
}
if len(hss) == 0 {
return nil, &NotFoundError{"host", path}
}
return hss, nil
}
func (f *Finder) HostSystem(ctx context.Context, path string) (*object.HostSystem, error) {
hss, err := f.HostSystemList(ctx, path)
if err != nil {
return nil, err
}
if len(hss) > 1 {
return nil, &MultipleFoundError{"host", path}
}
return hss[0], nil
}
func (f *Finder) DefaultHostSystem(ctx context.Context) (*object.HostSystem, error) {
hs, err := f.HostSystem(ctx, "*/*")
if err != nil {
return nil, toDefaultError(err)
}
return hs, nil
}
func (f *Finder) NetworksList(ctx context.Context, path string, recursive bool) ([]object.Network, error) {
es, err := f.find(ctx, f.networkFolder, recursive, path)
if err != nil {
return nil, err
}
var ns []object.Network
for _, e := range es {
ref := e.Object.Reference()
switch ref.Type {
case "Network":
r := object.NewNetwork(f.client, ref)
r.InventoryPath = e.Path
ns = append(ns, *r)
}
}
return ns, nil
}
func (f *Finder) SwitchList(ctx context.Context, path string) ([]object.DistributedVirtualSwitch, error) {
var ns []object.DistributedVirtualSwitch
folders, err := f.find(ctx, f.rootFolder, true, path)
if err != nil {
return ns, err
}
for _, folder := range folders {
if folder.Object.Reference().Type == "Folder" {
folder := object.NewFolder(f.client, folder.Object.Reference())
for _, k := range f.findSwitchesInFolder(ctx, folder) {
ns = append(ns, k)
}
}
}
return ns, nil
}
func (f *Finder) findSwitchesInFolder(ctx context.Context, folder *object.Folder) []object.DistributedVirtualSwitch {
var switches []object.DistributedVirtualSwitch
if children, err := folder.Children(ctx); err == nil {
for _, j := range children {
if j.Reference().Type == "VmwareDistributedVirtualSwitch" {
switches = append(switches, *object.NewDistributedVirtualSwitch(f.client, j.Reference()))
}
if j.Reference().Type == "Folder" {
switches2 := f.findSwitchesInFolder(ctx, object.NewFolder(f.client, j.Reference()))
for _, k := range switches2 {
switches = append(switches, k)
}
}
}
} else {
fmt.Printf("Error getting switches! %v\n", err)
}
return switches
}
func (f *Finder) DVPGList(ctx context.Context, path string, recursive bool) ([]object.DistributedVirtualPortgroup, error) {
es, err := f.find(ctx, f.networkFolder, recursive, path)
if err != nil {
return nil, err
}
var ns []object.DistributedVirtualPortgroup
for _, e := range es {
ref := e.Object.Reference()
switch ref.Type {
case "DistributedVirtualPortgroup":
r := object.NewDistributedVirtualPortgroup(f.client, ref)
r.InventoryPath = e.Path
ns = append(ns, *r)
}
}
return ns, nil
}
func (f *Finder) NetworkList(ctx context.Context, path string) ([]object.NetworkReference, error) {
es, err := f.find(ctx, f.networkFolder, false, path)
if err != nil {
return nil, err
}
var ns []object.NetworkReference
for _, e := range es {
ref := e.Object.Reference()
switch ref.Type {
case "Network":
r := object.NewNetwork(f.client, ref)
r.InventoryPath = e.Path
ns = append(ns, r)
case "DistributedVirtualPortgroup":
r := object.NewDistributedVirtualPortgroup(f.client, ref)
r.InventoryPath = e.Path
ns = append(ns, r)
}
}
if len(ns) == 0 {
return nil, &NotFoundError{"network", path}
}
return ns, nil
}
func (f *Finder) Network(ctx context.Context, path string) (object.NetworkReference, error) {
networks, err := f.NetworkList(ctx, path)
if err != nil {
return nil, err
}
if len(networks) > 1 {
return nil, &MultipleFoundError{"network", path}
}
return networks[0], nil
}
func (f *Finder) DefaultNetwork(ctx context.Context) (object.NetworkReference, error) {
network, err := f.Network(ctx, "*")
if err != nil {
return nil, toDefaultError(err)
}
return network, nil
}
func (f *Finder) ResourcePoolList(ctx context.Context, path string) ([]*object.ResourcePool, error) {
es, err := f.find(ctx, f.hostFolder, false, path)
if err != nil {
return nil, err
}
var rps []*object.ResourcePool
for _, e := range es {
var rp *object.ResourcePool
switch o := e.Object.(type) {
case mo.ResourcePool:
rp = object.NewResourcePool(f.client, o.Reference())
rp.InventoryPath = e.Path
rps = append(rps, rp)
}
}
if len(rps) == 0 {
return nil, &NotFoundError{"resource pool", path}
}
return rps, nil
}
func (f *Finder) ResourcePool(ctx context.Context, path string) (*object.ResourcePool, error) {
rps, err := f.ResourcePoolList(ctx, path)
if err != nil {
return nil, err
}
if len(rps) > 1 {
return nil, &MultipleFoundError{"resource pool", path}
}
return rps[0], nil
}
func (f *Finder) DefaultResourcePool(ctx context.Context) (*object.ResourcePool, error) {
rp, err := f.ResourcePool(ctx, "*/Resources")
if err != nil {
return nil, toDefaultError(err)
}
return rp, nil
}
func (f *Finder) VirtualMachineList(ctx context.Context, path string) ([]*object.VirtualMachine, error) {
return f.VirtualMachineListRecursive(ctx, path, false)
}
// VirtualMachineListRecursive allows for recursively calls to VirtualMachineList
func (f *Finder) VirtualMachineListRecursive(ctx context.Context, path string, recursive bool) ([]*object.VirtualMachine, error) {
es, err := f.find(ctx, f.vmFolder, recursive, path)
if err != nil {
return nil, err
}
var vms []*object.VirtualMachine
for _, e := range es {
switch o := e.Object.(type) {
case mo.VirtualMachine:
vm := object.NewVirtualMachine(f.client, o.Reference())
vm.InventoryPath = e.Path
vms = append(vms, vm)
}
}
if len(vms) == 0 {
return nil, &NotFoundError{"vm", path}
}
return vms, nil
}
func (f *Finder) VirtualMachine(ctx context.Context, path string) (*object.VirtualMachine, error) {
vms, err := f.VirtualMachineList(ctx, path)
if err != nil {
return nil, err
}
if len(vms) > 1 {
return nil, &MultipleFoundError{"vm", path}
}
return vms[0], nil
}
func (f *Finder) VirtualAppList(ctx context.Context, path string) ([]*object.VirtualApp, error) {
es, err := f.find(ctx, f.vmFolder, false, path)
if err != nil {
return nil, err
}
var apps []*object.VirtualApp
for _, e := range es {
switch o := e.Object.(type) {
case mo.VirtualApp:
app := object.NewVirtualApp(f.client, o.Reference())
app.InventoryPath = e.Path
apps = append(apps, app)
}
}
if len(apps) == 0 {
return nil, &NotFoundError{"app", path}
}
return apps, nil
}
func (f *Finder) VirtualApp(ctx context.Context, path string) (*object.VirtualApp, error) {
apps, err := f.VirtualAppList(ctx, path)
if err != nil {
return nil, err
}
if len(apps) > 1 {
return nil, &MultipleFoundError{"app", path}
}
return apps[0], nil
}