VMware {code} Community
lorygi
Contributor
Contributor

Unable to make PropertyCollector getHosts() work properly

Hi. I'm using VMware vSphere Web Services SDK (c#).
I'm trying, without success, to use the PropertyCollector.getHosts().
It retrives 0 objects (ObjectContent[]).


To me there is something strange in the "ComputeResource" TraversalSpec
(that is infact declared but never used).


Unfortunatly I'm not familiar with PropertyCollector so I cannot figure out what's wrong

(ah, same behaviour connecting to a single ESXi and to a vCenter server provided with 3 ESXi).

Thanks for your support and patience.

Lorenzo

PS: below the code, coming from the SDK examples

static ObjectContent[] getHosts(ManagedObjectReference dcMoRef) {
      // PropertySpec specifies what properties to
      // retrieve from what type of Managed Object
      PropertySpec pSpec = new PropertySpec();
      pSpec.type="HostSystem";
      pSpec.pathSet=new String[]
         { "name", "runtime.connectionState" };
      SelectionSpec recurseFolders = new SelectionSpec();
      recurseFolders.name="folder2childEntity";
      TraversalSpec computeResource2host = new TraversalSpec();
      computeResource2host.type="ComputeResource";
      computeResource2host.path="host";
      TraversalSpec folder2childEntity = new TraversalSpec();
      folder2childEntity.type="Folder";
      folder2childEntity.path="childEntity";
      folder2childEntity.name=recurseFolders.name;
      // Add BOTH of the specifications to this specification
      folder2childEntity.selectSet=new SelectionSpec[]
         { recurseFolders};

      // Traverse from a Datacenter through
      // the 'hostFolder' property
      TraversalSpec dc2hostFolder = new TraversalSpec();
      dc2hostFolder.type="Datacenter";
      dc2hostFolder.path="hostFolder";
      dc2hostFolder.selectSet=new SelectionSpec[]
         { folder2childEntity };
      ObjectSpec oSpec = new ObjectSpec();
      oSpec.obj=dcMoRef;
      oSpec.skip= true;
      oSpec.selectSet=new SelectionSpec[] { dc2hostFolder };
      PropertyFilterSpec pfSpec = new PropertyFilterSpec();
      pfSpec.propSet=new PropertySpec[] { pSpec };
      pfSpec.objectSet=new ObjectSpec[] { oSpec };
      return _service.RetrieveProperties(
            _sic.propertyCollector,
               new PropertyFilterSpec[] { pfSpec });
   }

0 Kudos
2 Replies
lorygi
Contributor
Contributor

Hello.

For those interested onto the issue, I solved changing the function in this way

   public static ObjectContent[] getHosts(
            ManagedObjectReference dcMoRef)
        {
            // PropertySpec specifies what properties to
            // retrieve from what type of Managed Object
            // This spec selects HostSystem information
            PropertySpec hostPropSpec = new PropertySpec();
            hostPropSpec.type = "HostSystem";
            hostPropSpec.pathSet = new String[] { "network", "name",
                "summary.hardware", "runtime.connectionState",
                "summary.overallStatus", "summary.quickStats" };
            // The following TraversalSpec and SelectionSpec
            // objects create the following relationship:
            //
            // Datacenter -> hostFolder -> childEntity(ComputeResources) -> HostSystem(s)
            TraversalSpec dc2hostFolder = new TraversalSpec();
            dc2hostFolder.type = "Folder";
            dc2hostFolder.path = "childEntity";
            TraversalSpec managedEntities2Hosts = new TraversalSpec();
            managedEntities2Hosts.type = "ComputeResource";
            managedEntities2Hosts.path = "host";
            dc2hostFolder.selectSet = new SelectionSpec[] { managedEntities2Hosts };
            // a. Traverse from a Datacenter through
            // the 'hostFolder' property
            TraversalSpec dc2network = new TraversalSpec();
            dc2network.type = "Datacenter";
            dc2network.path = "hostFolder";
            dc2network.selectSet = new SelectionSpec[] {dc2hostFolder};
            // ObjectSpec specifies the starting object and
            // any TraversalSpecs used to specify other objects
            // for consideration
            ObjectSpec oSpec = new ObjectSpec();
            oSpec.obj = dcMoRef;
            oSpec.skip = true;
            oSpec.selectSet = new SelectionSpec[] { dc2network };
            // PropertyFilterSpec is used to hold the ObjectSpec and
            // PropertySpec for the call
            PropertyFilterSpec pfSpec = new PropertyFilterSpec();
            pfSpec.propSet = new PropertySpec[] { hostPropSpec};
            pfSpec.objectSet = new ObjectSpec[] { oSpec };
            // RetrieveProperties() returns the properties
            // selected from the PropertyFilterSpec
            return _service.RetrieveProperties(
                    _sic.propertyCollector,
                    new PropertyFilterSpec[] { pfSpec });
        }
0 Kudos
lorygi
Contributor
Contributor

it works on both esxi 4.1 stand alone, and on vCenter Server with more than one hypervisor

0 Kudos