Skip to content

Instantly share code, notes, and snippets.

@jortel
Created May 11, 2020 15:27
Show Gist options
  • Save jortel/611c2d49aa75b8a343b8ba9ab14afa0e to your computer and use it in GitHub Desktop.
Save jortel/611c2d49aa75b8a343b8ba9ab14afa0e to your computer and use it in GitHub Desktop.
func (p *Provider) Watch() error {
pc := property.DefaultCollector(p.client.Client)
ospec := types.ObjectSpec{
Obj: p.client.ServiceContent.RootFolder,
Skip: types.NewBool(true),
SelectSet: []types.BaseSelectionSpec{
&types.TraversalSpec{
SelectionSpec: types.SelectionSpec{
Name: "traverseFolders",
},
Type: "Folder",
Path: "childEntity",
Skip: types.NewBool(true),
SelectSet: []types.BaseSelectionSpec{
&types.TraversalSpec{
Type: "HostSystem",
Path: "vm",
Skip: types.NewBool(false),
},
&types.TraversalSpec{
Type: "ComputeResource",
Path: "host",
Skip: types.NewBool(true),
SelectSet: []types.BaseSelectionSpec{
&types.TraversalSpec{
Type: "HostSystem",
Path: "vm",
Skip: types.NewBool(false),
},
},
},
&types.TraversalSpec{
Type: "Datacenter",
Path: "hostFolder",
Skip: types.NewBool(true),
SelectSet: []types.BaseSelectionSpec{
&types.SelectionSpec{
Name: "traverseFolders",
},
},
},
},
},
},
}
pspec := []types.PropertySpec{
{
Type: VirtualMachine,
PathSet: []string{
"summary",
},
},
}
filter := &property.WaitFilter{
CreateFilter: types.CreateFilter{
This: pc.Reference(),
Spec: types.PropertyFilterSpec{
ObjectSet: []types.ObjectSpec{ospec},
PropSet: pspec,
},
},
Options: &types.WaitOptions{
MaxWaitSeconds: types.NewInt32(5),
MaxObjectUpdates: 100,
},
}
handler := func(updateSet []types.ObjectUpdate) bool {
for _, update := range updateSet {
fmt.Println(update)
}
return false
}
err := property.WaitForUpdates(p.ctx, pc, filter, handler)
if err != nil {
fmt.Println(err.Error())
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment