Skip to content

Commit a1a9d84

Browse files
committed
examples: add VirtualDeviceList_SelectByBackingInfo
1 parent 61c4000 commit a1a9d84

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

object/example_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,60 @@ func ExampleNetworkReference_EthernetCardBackingInfo() {
536536
// Output: 1 of 2 NICs match backing
537537
}
538538

539+
func ExampleVirtualDeviceList_SelectByBackingInfo() {
540+
simulator.Run(func(ctx context.Context, c *vim25.Client) error {
541+
finder := find.NewFinder(c)
542+
vm, err := finder.VirtualMachine(ctx, "DC0_H0_VM0")
543+
if err != nil {
544+
return err
545+
}
546+
547+
backing := &types.VirtualPCIPassthroughVmiopBackingInfo{
548+
Vgpu: "grid_v100-4q",
549+
}
550+
551+
gpu := &types.VirtualPCIPassthrough{
552+
VirtualDevice: types.VirtualDevice{Backing: backing},
553+
}
554+
555+
err = vm.AddDevice(ctx, gpu) // add a GPU to this VM
556+
if err != nil {
557+
return err
558+
}
559+
560+
device, err := vm.Device(ctx) // get the VM's virtual device list
561+
if err != nil {
562+
return err
563+
}
564+
565+
// find the device with the given backing info
566+
gpus := device.SelectByBackingInfo(backing)
567+
568+
name := gpus[0].(*types.VirtualPCIPassthrough).Backing.(*types.VirtualPCIPassthroughVmiopBackingInfo).Vgpu
569+
570+
fmt.Println(name)
571+
572+
// example alternative to using SelectByBackingInfo for the use-case above:
573+
for i := range device {
574+
switch d := device[i].(type) {
575+
case *types.VirtualPCIPassthrough:
576+
switch b := d.Backing.(type) {
577+
case *types.VirtualPCIPassthroughVmiopBackingInfo:
578+
if b.Vgpu == backing.Vgpu {
579+
fmt.Println(b.Vgpu)
580+
}
581+
}
582+
}
583+
}
584+
585+
return nil
586+
})
587+
588+
// Output:
589+
// grid_v100-4q
590+
// grid_v100-4q
591+
}
592+
539593
// Find a VirtualMachine's Cluster
540594
func ExampleVirtualMachine_resourcePoolOwner() {
541595
simulator.Run(func(ctx context.Context, c *vim25.Client) error {

0 commit comments

Comments
 (0)