vpnprasad
Contributor
Contributor

HostSystemTraversal in Vsphere 4.0

I am browsing through the inventory to get all the HostSystem objects. The traversal is working fine in Vsphere 4.1, 5.0 and 5.1. But when I try the same with Vcenter 4.0, I am not getting any HostSystem objects.

Appreciate if some one can point me where I am going wrong.

C# Code:

//ComputeResource to host
            SelectionSpec selCR2HostSpec = new SelectionSpec();
            selCR2HostSpec.name = "CR2Host";
            TraversalSpec trCR2HostSpec = new TraversalSpec();
            trCR2HostSpec.name = selCR2HostSpec.name;
            trCR2HostSpec.type = "ComputeResource";
            trCR2HostSpec.path = "host";
            trCR2HostSpec.skip = false;
            trCR2HostSpec.selectSet = new SelectionSpec[] { selCR2HostSpec };
            //vmFolder(hostFolder) to childentity(ComputeResource)
            SelectionSpec selHF2CRSpec = new SelectionSpec();
            selHF2CRSpec.name = "HostFolder2CR";
            TraversalSpec trHF2CRSpec = new TraversalSpec();
            trHF2CRSpec.name = selHF2CRSpec.name;
            trHF2CRSpec.type = "Folder";
            trHF2CRSpec.path = "childEntity";
            trHF2CRSpec.skip = false;
            trHF2CRSpec.selectSet = new SelectionSpec[] { selHF2CRSpec, selCR2HostSpec };
            //---------------
            //Datacenter to hostFolder
            SelectionSpec selDC2HFSpec = new SelectionSpec();
            selDC2HFSpec.name = "DC2HostFolder";
            TraversalSpec trDC2HFSpec = new TraversalSpec();
            trDC2HFSpec.name = selDC2HFSpec.name;
            trDC2HFSpec.type = "Datacenter";
            trDC2HFSpec.path = "hostFolder";
            trDC2HFSpec.skip = false;
            trDC2HFSpec.selectSet = new SelectionSpec[] { selDC2HFSpec, selHF2CRSpec, selCR2HostSpec };
            //----------------
            //rootFolder to DC
            SelectionSpec selFd2DCSpec = new SelectionSpec();
            selFd2DCSpec.name = "Folder2DC";
            TraversalSpec trFd2DCSpec = new TraversalSpec();
            trFd2DCSpec.name = selFd2DCSpec.name;
            trFd2DCSpec.type = "Folder";
            trFd2DCSpec.path = "childEntity";
            trFd2DCSpec.skip = false;
            trFd2DCSpec.selectSet = new SelectionSpec[] { selFd2DCSpec, selDC2HFSpec, selHF2CRSpec, selCR2HostSpec };
            //---------------
            PropertySpec propSpec = new PropertySpec();
            propSpec.all = false;
            propSpec.pathSet = new string[] { "name", "parent", "datastore", "summary", "configManager", "config", "runtime", "vm" };
            propSpec.type = "HostSystem";
            ObjectSpec objSpec = new ObjectSpec();
            objSpec.obj = m_rootFolder;
            objSpec.skip = false;
            objSpec.selectSet = new SelectionSpec[] { trFd2DCSpec, trDC2HFSpec, trHF2CRSpec, trCR2HostSpec};
            PropertyFilterSpec propFilterSpec = new PropertyFilterSpec();
            propFilterSpec.propSet = new PropertySpec[] { propSpec };
            propFilterSpec.objectSet = new ObjectSpec[] { objSpec };
            ObjectContent[] objArr = m_vimService.RetrieveProperties(m_propCollector, new PropertyFilterSpec[] { propFilterSpec });
                       
            return objArr;
0 Kudos
3 Replies
vpnprasad
Contributor
Contributor

I could find out some logic behind writing the traversal specs.

We dont need to point so many selection specs every time in the selectSet. Its like a linked list. Build one traversal spec (eg: tr1) and mention object type and path (to traverse) and now define a link through selectSet. This link will be another traversal spec (tr2) whose object type will be the one we have given in path and fill the path for this new traversal spec (tr2). Now set the selectSet of tr1 to tr2.

tr1.selectSet = new SelectionSpec[]{tr2};

We can go on building till the object of our interest.

Modified code:

          //ComputeResource to host
            SelectionSpec selCR2HostSpec = new SelectionSpec();
            selCR2HostSpec.name = "CR2Host";
            TraversalSpec trCR2HostSpec = new TraversalSpec();
            trCR2HostSpec.name = selCR2HostSpec.name;
            trCR2HostSpec.type = "ComputeResource";
            trCR2HostSpec.path = "host";
            trCR2HostSpec.skip = false;
            trCR2HostSpec.selectSet = new SelectionSpec[] { selCR2HostSpec };
            //---------------
            //vmFolder(hostFolder) to childEntity(ComputeResource)
            TraversalSpec trHF2CRSpec = new TraversalSpec();
            trHF2CRSpec.name = "HostFolder2CR";
            trHF2CRSpec.type = "Folder";
            trHF2CRSpec.path = "childEntity";
            trHF2CRSpec.skip = false;
            trHF2CRSpec.selectSet = new SelectionSpec[] { trCR2HostSpec };
            //---------------
            //Datacenter to hostFolder
            TraversalSpec trDC2HFSpec = new TraversalSpec();
            trDC2HFSpec.name = "DC2HostFolder";
            trDC2HFSpec.type = "Datacenter";
            trDC2HFSpec.path = "hostFolder";
            trDC2HFSpec.skip = false;
            trDC2HFSpec.selectSet = new SelectionSpec[] { trHF2CRSpec };
            //----------------
            //rootFolder to DC
            TraversalSpec trFd2DCSpec = new TraversalSpec();
            trFd2DCSpec.name = "Folder2DC";
            trFd2DCSpec.type = "Folder";
            trFd2DCSpec.path = "childEntity";
            trFd2DCSpec.skip = false;
            trFd2DCSpec.selectSet = new SelectionSpec[] { trDC2HFSpec };
            //---------------
            PropertySpec propSpec = new PropertySpec();
            propSpec.all = false;
            propSpec.pathSet = new string[] { "name", "parent", "datastore", "summary", "configManager", "config", "runtime", "vm" };
            propSpec.type = "HostSystem";
            ObjectSpec objSpec = new ObjectSpec();
            objSpec.obj = m_rootFolder;
            objSpec.skip = false;
            objSpec.selectSet = new SelectionSpec[] { trFd2DCSpec };
            PropertyFilterSpec propFilterSpec = new PropertyFilterSpec();
            propFilterSpec.propSet = new PropertySpec[] { propSpec };
            propFilterSpec.objectSet = new ObjectSpec[] { objSpec };
           
            ObjectContent[] objArr = m_vimService.RetrieveProperties(m_propCollector, new PropertyFilterSpec[] { propFilterSpec });
            return objArr;

Thanks,

Prasad

0 Kudos
vpnprasad
Contributor
Contributor

Just updating that I found the answer to my question.

0 Kudos
anil21
Contributor
Contributor

Hi Prasad,

I am trying to modify host configuration through c#.net. Wants  to modify Configuration > Advanced Setting > UserVars.

Any idea on this?

Many Thanks

0 Kudos