github.com/vmware/govmomi@v0.51.0/object/example_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package object_test 6 7 import ( 8 "context" 9 "fmt" 10 "log" 11 12 "github.com/vmware/govmomi/find" 13 "github.com/vmware/govmomi/object" 14 "github.com/vmware/govmomi/property" 15 "github.com/vmware/govmomi/simulator" 16 "github.com/vmware/govmomi/view" 17 "github.com/vmware/govmomi/vim25" 18 "github.com/vmware/govmomi/vim25/mo" 19 "github.com/vmware/govmomi/vim25/types" 20 ) 21 22 func ExampleResourcePool_Owner() { 23 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 24 finder := find.NewFinder(c) 25 26 for _, name := range []string{"DC0_H0_VM0", "DC0_C0_RP0_VM0"} { 27 vm, err := finder.VirtualMachine(ctx, name) 28 if err != nil { 29 return err 30 } 31 32 pool, err := vm.ResourcePool(ctx) 33 if err != nil { 34 return err 35 } 36 37 owner, err := pool.Owner(ctx) 38 if err != nil { 39 return err 40 } 41 42 fmt.Printf("%s owner is a %T\n", name, owner) 43 } 44 45 return nil 46 }) 47 // Output: 48 // DC0_H0_VM0 owner is a *object.ComputeResource 49 // DC0_C0_RP0_VM0 owner is a *object.ClusterComputeResource 50 } 51 52 func ExampleVirtualMachine_CreateSnapshot() { 53 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 54 vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0") 55 if err != nil { 56 return err 57 } 58 59 task, err := vm.CreateSnapshot(ctx, "backup", "Backup", false, false) 60 if err != nil { 61 return err 62 } 63 if err = task.Wait(ctx); err != nil { 64 return err 65 } 66 67 id, err := vm.FindSnapshot(ctx, "backup") 68 if err != nil { 69 return err 70 } 71 72 var snapshot mo.VirtualMachineSnapshot 73 err = vm.Properties(ctx, *id, []string{"config.hardware.device"}, &snapshot) 74 if err != nil { 75 return err 76 } 77 78 fmt.Printf("%d devices", len(snapshot.Config.Hardware.Device)) 79 80 return nil 81 }) 82 // Output: 13 devices 83 } 84 85 func ExampleVirtualMachine_Customize() { 86 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 87 vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0") 88 if err != nil { 89 return err 90 } 91 task, err := vm.PowerOff(ctx) 92 if err != nil { 93 return err 94 } 95 if err = task.Wait(ctx); err != nil { 96 return err 97 } 98 99 spec := types.CustomizationSpec{ 100 NicSettingMap: []types.CustomizationAdapterMapping{ 101 types.CustomizationAdapterMapping{ 102 Adapter: types.CustomizationIPSettings{ 103 Ip: &types.CustomizationFixedIp{ 104 IpAddress: "192.168.1.100", 105 }, 106 SubnetMask: "255.255.255.0", 107 Gateway: []string{"192.168.1.1"}, 108 DnsServerList: []string{"192.168.1.1"}, 109 DnsDomain: "ad.domain", 110 }, 111 }, 112 }, 113 Identity: &types.CustomizationLinuxPrep{ 114 HostName: &types.CustomizationFixedName{ 115 Name: "hostname", 116 }, 117 Domain: "ad.domain", 118 TimeZone: "Etc/UTC", 119 HwClockUTC: types.NewBool(true), 120 }, 121 GlobalIPSettings: types.CustomizationGlobalIPSettings{ 122 DnsSuffixList: []string{"ad.domain"}, 123 DnsServerList: []string{"192.168.1.1"}, 124 }, 125 } 126 127 task, err = vm.Customize(ctx, spec) 128 if err != nil { 129 return err 130 } 131 if err = task.Wait(ctx); err != nil { 132 return err 133 } 134 135 task, err = vm.PowerOn(ctx) 136 if err != nil { 137 return err 138 } 139 if err = task.Wait(ctx); err != nil { 140 return err 141 } 142 143 ip, err := vm.WaitForIP(ctx) 144 if err != nil { 145 return err 146 } 147 148 fmt.Println(ip) 149 150 return nil 151 }) 152 // Output: 192.168.1.100 153 } 154 155 func ExampleVirtualMachine_HostSystem() { 156 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 157 vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0") 158 if err != nil { 159 return err 160 } 161 162 host, err := vm.HostSystem(ctx) 163 if err != nil { 164 return err 165 } 166 167 name, err := host.ObjectName(ctx) 168 if err != nil { 169 return err 170 } 171 172 fmt.Println(name) 173 174 return nil 175 }) 176 // Output: DC0_H0 177 } 178 179 func ExampleVirtualMachine_ResourcePool() { 180 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 181 finder := find.NewFinder(c) 182 vm, err := finder.VirtualMachine(ctx, "DC0_C0_RP0_VM0") 183 if err != nil { 184 return err 185 } 186 187 pool, err := vm.ResourcePool(ctx) 188 if err != nil { 189 return err 190 } 191 192 name, err := pool.ObjectName(ctx) 193 if err != nil { 194 return err 195 } 196 197 fmt.Println(name) 198 199 // The InventoryPath field not populated unless Finder.ResourcePool() or 200 // Finder.ResourcePoolList() was used. But we can populate it explicity. 201 pool.InventoryPath, err = find.InventoryPath(ctx, c, pool.Reference()) 202 if err != nil { 203 return err 204 } 205 206 fmt.Println(pool.InventoryPath) 207 208 return nil 209 }) 210 // Output: 211 // Resources 212 // /DC0/host/DC0_C0/Resources 213 } 214 215 func ExampleVirtualMachine_Clone() { 216 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 217 finder := find.NewFinder(c) 218 dc, err := finder.Datacenter(ctx, "DC0") 219 if err != nil { 220 return err 221 } 222 223 finder.SetDatacenter(dc) 224 225 vm, err := finder.VirtualMachine(ctx, "DC0_H0_VM0") 226 if err != nil { 227 return err 228 } 229 230 folders, err := dc.Folders(ctx) 231 if err != nil { 232 return err 233 } 234 235 spec := types.VirtualMachineCloneSpec{ 236 PowerOn: false, 237 } 238 239 task, err := vm.Clone(ctx, folders.VmFolder, "example-clone", spec) 240 if err != nil { 241 return err 242 } 243 244 info, err := task.WaitForResult(ctx) 245 if err != nil { 246 return err 247 } 248 249 clone := object.NewVirtualMachine(c, info.Result.(types.ManagedObjectReference)) 250 name, err := clone.ObjectName(ctx) 251 if err != nil { 252 return err 253 } 254 255 fmt.Println(name) 256 257 return nil 258 }) 259 // Output: example-clone 260 } 261 262 func ExampleFolder_CreateVM() { 263 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 264 finder := find.NewFinder(c) 265 dc, err := finder.Datacenter(ctx, "DC0") 266 if err != nil { 267 return err 268 } 269 270 finder.SetDatacenter(dc) 271 272 folders, err := dc.Folders(ctx) 273 if err != nil { 274 return err 275 } 276 277 pool, err := finder.ResourcePool(ctx, "DC0_C0/Resources") 278 if err != nil { 279 return err 280 } 281 282 spec := types.VirtualMachineConfigSpec{ 283 Name: "example-vm", 284 GuestId: string(types.VirtualMachineGuestOsIdentifierOtherGuest), 285 Files: &types.VirtualMachineFileInfo{ 286 VmPathName: "[LocalDS_0]", 287 }, 288 } 289 290 task, err := folders.VmFolder.CreateVM(ctx, spec, pool, nil) 291 if err != nil { 292 return err 293 } 294 295 info, err := task.WaitForResult(ctx) 296 if err != nil { 297 return err 298 } 299 300 vm := object.NewVirtualMachine(c, info.Result.(types.ManagedObjectReference)) 301 name, err := vm.ObjectName(ctx) 302 if err != nil { 303 return err 304 } 305 306 fmt.Println(name) 307 308 return nil 309 }) 310 // Output: example-vm 311 } 312 313 func ExampleVirtualMachine_Reconfigure() { 314 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 315 vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0") 316 if err != nil { 317 return err 318 } 319 320 spec := types.VirtualMachineConfigSpec{Annotation: "example reconfig"} 321 322 task, err := vm.Reconfigure(ctx, spec) 323 if err != nil { 324 return err 325 } 326 327 err = task.Wait(ctx) 328 if err != nil { 329 return err 330 } 331 332 var obj mo.VirtualMachine 333 err = vm.Properties(ctx, vm.Reference(), []string{"config.annotation"}, &obj) 334 if err != nil { 335 return err 336 } 337 338 fmt.Println(obj.Config.Annotation) 339 340 return nil 341 }) 342 // Output: example reconfig 343 } 344 345 func ExampleCommon_Destroy() { 346 model := simulator.VPX() 347 model.Datastore = 2 348 349 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 350 // Change to "LocalDS_0" will cause ResourceInUse error, 351 // as simulator VMs created by the VPX model use "LocalDS_0". 352 ds, err := find.NewFinder(c).Datastore(ctx, "LocalDS_1") 353 if err != nil { 354 return err 355 } 356 357 task, err := ds.Destroy(ctx) 358 if err != nil { 359 return err 360 } 361 362 if err = task.Wait(ctx); err != nil { 363 return err 364 } 365 366 fmt.Println("destroyed", ds.InventoryPath) 367 return nil 368 }, model) 369 // Output: destroyed /DC0/datastore/LocalDS_1 370 } 371 372 func ExampleCommon_Rename() { 373 model := simulator.VPX() 374 375 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 376 dc, err := find.NewFinder(c).Datacenter(ctx, "DC0") 377 if err != nil { 378 return err 379 } 380 381 task, err := dc.Rename(ctx, "MyDC") 382 if err != nil { 383 return err 384 } 385 386 if err = task.Wait(ctx); err != nil { 387 return err 388 } 389 390 name, err := dc.ObjectName(ctx) 391 if err != nil { 392 return err 393 } 394 395 fmt.Println(name) 396 return nil 397 }, model) 398 // Output: MyDC 399 } 400 401 func ExampleCustomFieldsManager_Set() { 402 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 403 m, err := object.GetCustomFieldsManager(c) 404 if err != nil { 405 return err 406 } 407 408 any := []string{"ManagedEntity"} 409 field, err := m.Add(ctx, "backup", any[0], nil, nil) // adds the custom field "backup" to all types 410 if err != nil { 411 return err 412 } 413 414 v, err := view.NewManager(c).CreateContainerView(ctx, c.ServiceContent.RootFolder, any, true) 415 if err != nil { 416 log.Fatal(err) 417 } 418 419 all, err := v.Find(ctx, any, nil) // gives us the count of all objects in the inventory 420 if err != nil { 421 return err 422 } 423 424 refs, err := v.Find(ctx, []string{"VirtualMachine", "Datastore"}, nil) 425 if err != nil { 426 return err 427 } 428 429 for _, ref := range refs { 430 err = m.Set(ctx, ref, field.Key, "true") // sets the custom value "backup=true" on specific types 431 if err != nil { 432 return err 433 } 434 } 435 436 // filter used to find objects with "backup=true" 437 filter := property.Match{"customValue": &types.CustomFieldStringValue{ 438 CustomFieldValue: types.CustomFieldValue{Key: field.Key}, 439 Value: "true", 440 }} 441 442 var objs []mo.ManagedEntity 443 err = v.RetrieveWithFilter(ctx, any, []string{"name", "customValue"}, &objs, filter) 444 if err != nil { 445 return err 446 } 447 448 fmt.Printf("backup %d of %d objects", len(objs), len(all)) 449 return v.Destroy(ctx) 450 }) 451 // Output: backup 5 of 22 objects 452 } 453 454 func ExampleCustomizationSpecManager_Info() { 455 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 456 m := object.NewCustomizationSpecManager(c) 457 info, err := m.Info(ctx) 458 if err != nil { 459 return err 460 } 461 462 for i := range info { 463 item, err := m.GetCustomizationSpec(ctx, info[i].Name) 464 if err != nil { 465 return err 466 } 467 fmt.Printf("%s=%T\n", item.Info.Name, item.Spec.Identity) 468 } 469 return nil 470 }) 471 // Output: 472 // vcsim-linux=*types.CustomizationLinuxPrep 473 // vcsim-linux-static=*types.CustomizationLinuxPrep 474 // vcsim-windows-static=*types.CustomizationSysprep 475 // vcsim-windows-domain=*types.CustomizationSysprep 476 } 477 478 func ExampleNetworkReference_EthernetCardBackingInfo() { 479 model := simulator.VPX() 480 model.OpaqueNetwork = 1 // Create 1 NSX backed OpaqueNetwork per DC 481 482 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 483 finder := find.NewFinder(c) 484 vm, err := finder.VirtualMachine(ctx, "DC0_H0_VM0") 485 if err != nil { 486 return err 487 } 488 489 // finder.Network returns an object.NetworkReference 490 net, err := finder.Network(ctx, "DC0_NSX0") 491 if err != nil { 492 return err 493 } 494 495 // EthernetCardBackingInfo creates the backing for any network type: 496 // "Network", "DistributedVirtualPortgroup" or "OpaqueNetwork" 497 backing, err := net.EthernetCardBackingInfo(ctx) 498 if err != nil { 499 return err 500 } 501 502 device, err := object.EthernetCardTypes().CreateEthernetCard("e1000", backing) 503 if err != nil { 504 return err 505 } 506 507 err = vm.AddDevice(ctx, device) 508 if err != nil { 509 return err 510 } 511 512 list, err := vm.Device(ctx) 513 if err != nil { 514 return err 515 } 516 517 nics := list.SelectByType((*types.VirtualEthernetCard)(nil)) // All VM NICs (DC0_DVPG0 + DC0_NSX0) 518 match := list.SelectByBackingInfo(backing) // VM NIC with DC0_NSX0 backing 519 520 fmt.Printf("%d of %d NICs match backing\n", len(match), len(nics)) 521 522 return nil 523 }, model) 524 // Output: 1 of 2 NICs match backing 525 } 526 527 func ExampleVirtualDeviceList_SelectByBackingInfo() { 528 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 529 finder := find.NewFinder(c) 530 vm, err := finder.VirtualMachine(ctx, "DC0_H0_VM0") 531 if err != nil { 532 return err 533 } 534 535 backing := &types.VirtualPCIPassthroughVmiopBackingInfo{ 536 Vgpu: "grid_v100-4q", 537 } 538 539 gpu := &types.VirtualPCIPassthrough{ 540 VirtualDevice: types.VirtualDevice{Backing: backing}, 541 } 542 543 err = vm.AddDevice(ctx, gpu) // add a GPU to this VM 544 if err != nil { 545 return err 546 } 547 548 device, err := vm.Device(ctx) // get the VM's virtual device list 549 if err != nil { 550 return err 551 } 552 553 // find the device with the given backing info 554 gpus := device.SelectByBackingInfo(backing) 555 556 name := gpus[0].(*types.VirtualPCIPassthrough).Backing.(*types.VirtualPCIPassthroughVmiopBackingInfo).Vgpu 557 558 fmt.Println(name) 559 560 // example alternative to using SelectByBackingInfo for the use-case above: 561 for i := range device { 562 switch d := device[i].(type) { 563 case *types.VirtualPCIPassthrough: 564 switch b := d.Backing.(type) { 565 case *types.VirtualPCIPassthroughVmiopBackingInfo: 566 if b.Vgpu == backing.Vgpu { 567 fmt.Println(b.Vgpu) 568 } 569 } 570 } 571 } 572 573 return nil 574 }) 575 576 // Output: 577 // grid_v100-4q 578 // grid_v100-4q 579 } 580 581 // Find a VirtualMachine's Cluster 582 func ExampleVirtualMachine_resourcePoolOwner() { 583 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 584 obj, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_C0_RP0_VM0") 585 if err != nil { 586 return err 587 } 588 589 pool, err := obj.ResourcePool(ctx) 590 if err != nil { 591 return err 592 } 593 594 // ResourcePool owner will be a ComputeResource or ClusterComputeResource 595 cluster, err := pool.Owner(ctx) 596 if err != nil { 597 return err 598 } 599 600 fmt.Printf("%s", cluster.Reference().Type) 601 602 return nil 603 }) 604 // Output: ClusterComputeResource 605 } 606 607 func ExampleHostConfigManager_OptionManager() { 608 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 609 m := view.NewManager(c) 610 kind := []string{"HostSystem"} 611 612 v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, kind, true) 613 if err != nil { 614 log.Fatal(err) 615 } 616 617 refs, err := v.Find(ctx, kind, nil) 618 if err != nil { 619 return err 620 } 621 622 setting := "" 623 624 for _, ref := range refs { 625 host := object.NewHostSystem(c, ref) 626 m, err := host.ConfigManager().OptionManager(ctx) 627 if err != nil { 628 return err 629 } 630 631 opt := []types.BaseOptionValue{&types.OptionValue{ 632 Key: "vcrun", 633 Value: "Config.HostAgent.plugins.hostsvc.esxAdminsGroup", 634 }} 635 636 err = m.Update(ctx, opt) 637 if err != nil { 638 return err 639 } 640 641 opt, err = m.Query(ctx, "vcrun") 642 if err != nil { 643 return err 644 } 645 setting = opt[0].GetOptionValue().Value.(string) 646 } 647 648 fmt.Println(setting) 649 650 return v.Destroy(ctx) 651 }) 652 // Output: Config.HostAgent.plugins.hostsvc.esxAdminsGroup 653 }