VMware {code} Community
jswager1
Contributor
Contributor

Traversals, find VMs from given ComputeResource

Relatively simple problem: given a ComputerResource or ClusterComputeResource, find all the contained VMs. Using VC 2.5 Update 3, ESX 3.5 Update 3 hosts, the VISDK 2.5, Visual Studio 2005 using the VimService2005 references. Here's the rough chunk of the code I'm using:

private TraversalSpec buildTSpec()

{

TraversalSpec crToH = new TraversalSpec();

crToH.type = "ComputeResource";

crToH.path = "host";

crToH.skip = false;

crToH.name = "crToH";

crToH.selectSet = new SelectionSpec[] {}; //A

//crToH.selectSet = new SelectionSpec[] { new SelectionSpec() }; //B

// crToH.selectSet[0].name = "HToVm"; //B

TraversalSpec HToVm = new TraversalSpec();

HToVm.type = "HostSystem";

HToVm.path = "vm";

HToVm.skip = false;

HToVm.name = "HToVm";

HToVm.selectSet = new SelectionSpec[] { new SelectionSpec() };

HToVm.selectSet[0].name = "visitFolders";

TraversalSpec visitFolders = new TraversalSpec();

visitFolders.type = "Folder";

visitFolders.path = "childEntity";

visitFolders.skip = false;

visitFolders.name = "visitFolders";

visitFolders.selectSet = new SelectionSpec[] { new SelectionSpec(), crToH, HToVm };

visitFolders.selectSet[0].name = "visitFolders";

return crToH;

}

private void BuildVMLists()

{

TraversalSpec tSpec = buildTSpec();

PropertySpec[] propSpecArray = new PropertySpec[] { new PropertySpec(), new PropertySpec(), new PropertySpec(), new PropertySpec() };

propSpecArray[0].type = "VirtualMachine";

propSpecArray[0].all = true;

propSpecArray[0].allSpecified = true;

propSpecArray[1].type = "HostSystem";

propSpecArray[1].all = true;

propSpecArray[1].allSpecified = true;

propSpecArray[2].type = "ComputeResource";

propSpecArray[2].all = true;

propSpecArray[2].allSpecified = true;

propSpecArray[3].type = "ResourcePool";

propSpecArray[3].all = true;

propSpecArray[3].allSpecified = true;

PropertyFilterSpec spec = new PropertyFilterSpec();

spec.propSet = propSpecArray;

spec.objectSet = new ObjectSpec[] { new ObjectSpec() };

//spec.objectSet[0].obj = _sic.rootFolder;

spec.objectSet[0].obj = service.FindByInventoryPath(sic.searchIndex, "/DataCenter/host/MyCluster");

spec.objectSet[0].skip = false;

spec.objectSet[http://0].selectSet = new SelectionSpec[|http://0].selectSet = new SelectionSpec[] ;

ObjectContent[] ocArray =

service.RetrieveProperties(sic.propertyCollector, new PropertyFilterSpec[] );

// code to traverse the list of returned objects/props and show any "name" properties.

}

The intent is to start from cluster, MyCluster, and find the VMs, although this code was just doing the traversal and then displaying any names that it found. The names it did return was "MyCluster" and the names of the hosts contained in the cluster, but not the names of the virtual machines on the host.

I thought the problem was obvious -traversal crToH need to traverse to HToVM; do this be commenting out the //A lines and uncommenting the //B lines. Running it fails with "SoapException was unhandled" and the details are "A specified parameter was not correct". So the three lines I modified must be the source of the problem. Did I somehow set that traversal up wrong? The MOB browser shows that the CusterComputeResource does have an HostSystem "host" array, and from there, you can go to the host, which has a "vm" array. But for some reason, it doesn't seem to allow traversals that way. Is this expected, or documented - or did I just screw something up.

Any help greatly appreciated - and thanks in advance.

Tags (3)
0 Kudos
2 Replies
jswager1
Contributor
Contributor

May have just found my own answer, but if someone with more know-how could verify, I would appreciate it.

On page 39 of the SDK, there's a hierarchy of objects. Host Systems are not connected to Virtual Machines, which might explain why I can't traverse from host system to a virtual machine. I'm guessing that I might be able to get list of VM objects (properties of the HostSystem), but cannot actually traverse since they are not connected.

0 Kudos
Steve_Jin
Expert
Expert

I have a complete traversal spec in the VI Java API. Even the language is different, but the idea is the same.

Please check the method: public static SelectionSpec [] buildFullTraversal() -- line 247 and on.

In VI Java API, you can get the VMs in a given CR in one call:

ManagedEntity[] vms = new InventoryNavigator(computerResource).searchManagedEntities("VirtualMachine");

You can port the code to C#.

Steve JIN, VMware Engineering

Creator of VI Java API:

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
0 Kudos