Documentation
¶
Index ¶
- type ContainerView
- func (v ContainerView) Find(ctx context.Context, kind []string, filter property.Filter) ([]types.ManagedObjectReference, error)
- func (v ContainerView) Retrieve(ctx context.Context, kind []string, ps []string, dst interface{}, ...) error
- func (v ContainerView) RetrieveWithFilter(ctx context.Context, kind []string, ps []string, dst interface{}, ...) error
- type ListView
- type ManagedObjectView
- type Manager
- func (m Manager) CreateContainerView(ctx context.Context, container types.ManagedObjectReference, ...) (*ContainerView, error)
- func (m Manager) CreateListView(ctx context.Context, objects []types.ManagedObjectReference) (*ListView, error)
- func (m Manager) CreateTaskView(ctx context.Context, watch *types.ManagedObjectReference) (*TaskView, error)
- type TaskView
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContainerView ¶ added in v0.13.0
type ContainerView struct {
ManagedObjectView
}
Example (RetrieveClusters) ¶
package main
import (
"context"
"fmt"
"log"
"sort"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
)
func main() {
model := simulator.VPX()
model.Cluster = 3
simulator.Run(func(ctx context.Context, c *vim25.Client) error {
m := view.NewManager(c)
kind := []string{"ClusterComputeResource"}
v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, kind, true)
if err != nil {
log.Fatal(err)
}
var clusters []mo.ClusterComputeResource
var names []string
err = v.Retrieve(ctx, kind, []string{"name"}, &clusters)
if err != nil {
return err
}
for _, cluster := range clusters {
names = append(names, cluster.Name)
}
sort.Strings(names)
fmt.Println(names)
return v.Destroy(ctx)
}, model)
}
Output: [DC0_C0 DC0_C1 DC0_C2]
func NewContainerView ¶ added in v0.13.0
func NewContainerView(c *vim25.Client, ref types.ManagedObjectReference) *ContainerView
func (ContainerView) Find ¶ added in v0.14.0
func (v ContainerView) Find(ctx context.Context, kind []string, filter property.Filter) ([]types.ManagedObjectReference, error)
Find returns object references for entities of type kind, matching the given filter.
Example ¶
Create a view of all VMs in a specific subfolder, powering off all VMs within
package main
import (
"context"
"fmt"
"log"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
)
func main() {
model := simulator.VPX()
model.Folder = 1 // put everything inside subfolders
simulator.Run(func(ctx context.Context, c *vim25.Client) error {
folder, err := object.NewSearchIndex(c).FindByInventoryPath(ctx, "/F0/DC0/vm/F0")
if err != nil {
return err
}
m := view.NewManager(c)
kind := []string{"VirtualMachine"} // include VMs only, ignoring other object types
// Root of the view is the subfolder moid (true == recurse into any subfolders of the root)
v, err := m.CreateContainerView(ctx, folder.Reference(), kind, true)
if err != nil {
log.Fatal(err)
}
vms, err := v.Find(ctx, kind, property.Filter{})
if err != nil {
return err
}
for _, id := range vms {
vm := object.NewVirtualMachine(c, id)
task, err := vm.PowerOff(ctx)
if err != nil {
return err
}
if err = task.Wait(ctx); err != nil {
return err
}
}
fmt.Println(len(vms))
return v.Destroy(ctx)
}, model)
}
Output: 4
func (ContainerView) Retrieve ¶ added in v0.14.0
func (v ContainerView) Retrieve(ctx context.Context, kind []string, ps []string, dst interface{}, pspec ...types.PropertySpec) error
Retrieve populates dst as property.Collector.Retrieve does, for all entities in the view of types specified by kind.
Example ¶
Create a view of all hosts in the inventory, printing host names that belong to a cluster and excluding standalone hosts.
package main
import (
"context"
"fmt"
"log"
"sort"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
)
func main() {
model := simulator.VPX()
model.Datacenter = 2
simulator.Run(func(ctx context.Context, c *vim25.Client) error {
m := view.NewManager(c)
kind := []string{"HostSystem"}
v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, kind, true)
if err != nil {
log.Fatal(err)
}
var hosts []mo.HostSystem
var names []string
err = v.Retrieve(ctx, kind, []string{"summary.config.name", "parent"}, &hosts)
if err != nil {
return err
}
for _, host := range hosts {
if host.Parent.Type != "ClusterComputeResource" {
continue
}
names = append(names, host.Summary.Config.Name)
}
sort.Strings(names)
fmt.Println(names)
return v.Destroy(ctx)
}, model)
}
Output: [DC0_C0_H0 DC0_C0_H1 DC0_C0_H2 DC1_C0_H0 DC1_C0_H1 DC1_C0_H2]
func (ContainerView) RetrieveWithFilter ¶ added in v0.14.0
func (v ContainerView) RetrieveWithFilter(ctx context.Context, kind []string, ps []string, dst interface{}, filter property.Filter) error
RetrieveWithFilter populates dst as Retrieve does, but only for entities matching the given filter.
Example ¶
Create a view of all VMs in the inventory, printing VM names that end with "_VM1".
package main
import (
"context"
"fmt"
"log"
"sort"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
)
func main() {
simulator.Run(func(ctx context.Context, c *vim25.Client) error {
m := view.NewManager(c)
kind := []string{"VirtualMachine"}
v, err := m.CreateContainerView(ctx, c.ServiceContent.RootFolder, kind, true)
if err != nil {
log.Fatal(err)
}
var vms []mo.VirtualMachine
var names []string
err = v.RetrieveWithFilter(ctx, kind, []string{"name"}, &vms, property.Filter{"name": "*_VM1"})
if err != nil {
return err
}
for _, vm := range vms {
names = append(names, vm.Name)
}
sort.Strings(names)
fmt.Println(names)
return v.Destroy(ctx)
})
}
Output: [DC0_C0_RP0_VM1 DC0_H0_VM1]
type ListView ¶
type ListView struct {
ManagedObjectView
}
func NewListView ¶
func NewListView(c *vim25.Client, ref types.ManagedObjectReference) *ListView
type ManagedObjectView ¶ added in v0.16.0
func NewManagedObjectView ¶ added in v0.16.0
func NewManagedObjectView(c *vim25.Client, ref types.ManagedObjectReference) *ManagedObjectView
func (*ManagedObjectView) Destroy ¶ added in v0.16.0
func (v *ManagedObjectView) Destroy(ctx context.Context) error
func (*ManagedObjectView) TraversalSpec ¶ added in v0.16.0
func (v *ManagedObjectView) TraversalSpec() *types.TraversalSpec
type Manager ¶
func NewManager ¶
func (Manager) CreateContainerView ¶ added in v0.13.0
func (m Manager) CreateContainerView(ctx context.Context, container types.ManagedObjectReference, managedObjectTypes []string, recursive bool) (*ContainerView, error)
func (Manager) CreateListView ¶
func (Manager) CreateTaskView ¶ added in v0.16.0
func (m Manager) CreateTaskView(ctx context.Context, watch *types.ManagedObjectReference) (*TaskView, error)
CreateTaskView creates a new ListView that optionally watches for a ManagedEntity's recentTask updates.