Created
December 11, 2015 11:23
-
-
Save higebu/76b16f973f7bc40f662f to your computer and use it in GitHub Desktop.
Example Golang script for WaitForUpdateEx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/url" | |
"os" | |
"github.com/vmware/govmomi" | |
"github.com/vmware/govmomi/property" | |
"github.com/vmware/govmomi/vim25/types" | |
"golang.org/x/net/context" | |
) | |
func exit(err error) { | |
fmt.Fprintf(os.Stderr, "Error: %s\n", err) | |
os.Exit(1) | |
} | |
func Wait(ctx context.Context, c *property.Collector, obj types.ManagedObjectReference, objType string, ps []string) error { | |
p, err := c.Create(ctx) | |
if err != nil { | |
return err | |
} | |
// Attempt to destroy the collector using the background context, as the | |
// specified context may have timed out or have been cancelled. | |
defer p.Destroy(context.Background()) | |
ospec := types.ObjectSpec{ | |
Obj: obj, | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "rpToRp"}, | |
Type: "ResourcePool", | |
Path: "resourcePool", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "rpToRp"}, | |
&types.SelectionSpec{Name: "rpToVm"}, | |
}, | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "rpToVm"}, | |
Type: "ResourcePool", | |
Path: "vm", | |
Skip: types.NewBool(false), | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "crToRp"}, | |
Type: "ClusterComputeResource", | |
Path: "resourcePool", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "rpToRp"}, | |
&types.SelectionSpec{Name: "rpToVm"}, | |
}, | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "crToH"}, | |
Type: "ClusterComputeResource", | |
Path: "host", | |
Skip: types.NewBool(false), | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "dcToHf"}, | |
Type: "Datacenter", | |
Path: "hostFolder", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "visitFolders"}, | |
}, | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "dcToVmf"}, | |
Type: "Datacenter", | |
Path: "vmFolder", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "visitFolders"}, | |
}, | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "dcToNet"}, | |
Type: "Datacenter", | |
Path: "networkFolder", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "visitFolders"}, | |
}, | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "dcToDs"}, | |
Type: "Datacenter", | |
Path: "datastore", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "visitFolders"}, | |
}, | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "hToVm"}, | |
Type: "HostSystem", | |
Path: "vm", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "visitFolders"}, | |
}, | |
}, | |
&types.TraversalSpec{ | |
SelectionSpec: types.SelectionSpec{Name: "visitFolders"}, | |
Type: "Folder", | |
Path: "childEntity", | |
Skip: types.NewBool(false), | |
SelectSet: []types.BaseSelectionSpec{ | |
&types.SelectionSpec{Name: "visitFolders"}, | |
&types.SelectionSpec{Name: "dcToHf"}, | |
&types.SelectionSpec{Name: "dcToVmf"}, | |
&types.SelectionSpec{Name: "dcToNet"}, | |
&types.SelectionSpec{Name: "crToH"}, | |
&types.SelectionSpec{Name: "crToRp"}, | |
&types.SelectionSpec{Name: "dcToDs"}, | |
&types.SelectionSpec{Name: "hToVm"}, | |
&types.SelectionSpec{Name: "rpToVm"}, | |
}, | |
}, | |
}, | |
Skip: types.NewBool(false), | |
} | |
req := types.CreateFilter{ | |
Spec: types.PropertyFilterSpec{ | |
ObjectSet: []types.ObjectSpec{ospec}, | |
PropSet: []types.PropertySpec{ | |
{ | |
PathSet: ps, | |
Type: objType, | |
}, | |
}, | |
}, | |
} | |
err = p.CreateFilter(ctx, req) | |
if err != nil { | |
return err | |
} | |
for version := ""; ; { | |
res, err := p.WaitForUpdates(ctx, version) | |
if err != nil { | |
return err | |
} | |
// Retry if the result came back empty | |
if res == nil { | |
continue | |
} | |
version = res.Version | |
for _, fs := range res.FilterSet { | |
for _, os := range fs.ObjectSet { | |
for _, cs := range os.ChangeSet { | |
fmt.Printf("version=%s name=%s op=%s val=%s\n", version, cs.Name, cs.Op, cs.Val) | |
} | |
} | |
} | |
} | |
} | |
func main() { | |
ctx, cancel := context.WithCancel(context.Background()) | |
defer cancel() | |
u, err := url.Parse("https://user:password@host/sdk") | |
// Connect and log in to ESX or vCenter | |
c, err := govmomi.NewClient(ctx, u, true) | |
if err != nil { | |
exit(err) | |
} | |
// Retrieve summary property for all VMs | |
err = Wait(ctx, c.PropertyCollector(), c.ServiceContent.RootFolder, "VirtualMachine", []string{"name"}) | |
if err != nil { | |
exit(err) | |
} | |
} |
@higebu thanks for the script !
I was wondering how the WaitForUpdatesEx() is getting implemented. The when I went inside the WaitForUpdates() I found it's internally implemented.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MOR -> object