github.com/vmware/govmomi@v0.43.0/examples/virtualmachines/main.go (about) 1 /* 2 Copyright (c) 2017 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 This example program shows how the `view` package can 19 be used to navigate a vSphere inventory structure using govmomi. 20 */ 21 22 package main 23 24 import ( 25 "context" 26 "fmt" 27 28 "github.com/vmware/govmomi/examples" 29 "github.com/vmware/govmomi/view" 30 "github.com/vmware/govmomi/vim25" 31 "github.com/vmware/govmomi/vim25/mo" 32 ) 33 34 func main() { 35 examples.Run(func(ctx context.Context, c *vim25.Client) error { 36 // Create view of VirtualMachine objects 37 m := view.NewManager(c) 38 39 v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, []string{"VirtualMachine"}, true) 40 if err != nil { 41 return err 42 } 43 44 defer v.Destroy(ctx) 45 46 // Retrieve summary property for all machines 47 // Reference: http://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.VirtualMachine.html 48 var vms []mo.VirtualMachine 49 err = v.Retrieve(ctx, []string{"VirtualMachine"}, []string{"summary"}, &vms) 50 if err != nil { 51 return err 52 } 53 54 // Print summary per vm (see also: govc/vm/info.go) 55 56 for _, vm := range vms { 57 fmt.Printf("%s: %s\n", vm.Summary.Config.Name, vm.Summary.Config.GuestFullName) 58 } 59 60 return nil 61 }) 62 }