Thanks stumpr,
Based upon your suggestion I have coded in below fashion. I need to make improvements in below code for two things.
1. The code below will scan all hosts present in datacentre. Instead I want to limit it to hostname (dns) which I pass.
2. I want to browse only few datastores (its list exists with me) associated to that host.
How can we modify the below program to satisfy points 1 and 2.
TraversalSpec
type= "HostSystem"
path = "datastore"
PropertySpec
obj="HostSystem"
pathSet[]="name"
PropertySpec
obj="Datastore"
pathSet[]="summary.capacity"
PropertySpec
obj="Datastore"
pathSet[]="summary.freeSpace"
PropertySpec
obj="Datastore"
pathSet[]="summary.uncommitted"
Create a ContainerView for host.
// Get references to the ViewManager and PropertyCollector
ManagedObjectReference viewMgrRef = sContent.getViewManager();
ManagedObjectReference propColl = sContent.getPropertyCollector();
// use a container view for host to define the traversal
// - invoke the VimPortType method createContainerView (corresponds
// to the ViewManager method) - pass the ViewManager MOR and
// the other parameters required for the method invocation
// (use a List<String> for the type parameter's string[])
List<String> hostList = new ArrayList<String>();
hostList.add("HostSystem");
ManagedObjectReference cViewRef =
methods.createContainerView(viewMgrRef,
sContent.getRootFolder(),
hostList,
true);
// create an object spec to define the beginning of the traversal;
// container view is the root object for this traversal
ObjectSpec oSpec = new ObjectSpec();
oSpec.setObj(cViewRef);
oSpec.setSkip(true);
// create a traversal spec to select all objects in the view
TraversalSpec tSpec = new TraversalSpec();
tSpec.setName("traverseEntities");
tSpec.setPath("view");
tSpec.setSkip(false);
tSpec.setType("ContainerView");
// add the traversal spec to the object spec;
// the accessor method (getSelectSet) returns a reference
// to the mapped XML representation of the list; using this
// reference to add the spec will update the selectSet list
oSpec.getSelectSet().add(tSpec);
// extend from host to datastore
TraversalSpec tSpecHsD = new TraversalSpec();
tSpecHsD.setType("HostSystem");
tSpecHsD.setPath("datastore");
tSpecHsD.setSkip(false);
// add the datastore traversal specs
// to the host traversal;
// the accessor method (getSelectSet) returns a reference
// to the mapped XML representation of the list; using this
// reference to add the spec will update the selectSet list
tSpec.getSelectSet().add(tSpecHsD);
// specify the properties for retrieval
// (host name, datastore summary capacity,datastore summary freeSpace, datastore summary uncommitted);
// the accessor method (getPathSet) returns a reference to the mapped
// XML representation of the list; using this reference to add the
// property names will update the pathSet list
PropertySpec pSpec = new PropertySpec();
pSpec.setType("HostSystem");
pSpec.getPathSet().add("name");
PropertySpec pSpecDs = new PropertySpec();
pSpecDs.setType("Datastore");
pSpecDs.getPathSet().add("summary.capacity");
pSpecDs.getPathSet().add("summary.freeSpace");
pSpecDs.getPathSet().add("summary.uncommitted");
// create a PropertyFilterSpec and add the object and
// property specs to it; use the getter methods to reference
// the mapped XML representation of the lists and add the specs
// directly to the objectSet and propSet lists
PropertyFilterSpec fSpec = new PropertyFilterSpec();
fSpec.getObjectSet().add(oSpec);
fSpec.getPropSet().add(pSpec);
fSpec.getPropSet().add(pSpecDs);
// Create a list for the filters and add the spec to it
List<PropertyFilterSpec> fSpecList = new ArrayList<PropertyFilterSpec>();
fSpecList.add(fSpec);
// get the data from the server
RetrieveOptions ro = new RetrieveOptions();
RetrieveResult props = methods.retrievePropertiesEx(propColl,fSpecList,ro);
// go through the returned list and print out the data
if (props != null) {
for (ObjectContent oc : props.getObjects()) {
String value = null;
String path = null;
List<DynamicProperty> dps = oc.getPropSet();
if (dps != null) {
for (DynamicProperty dp : dps) {
path = dp.getName();
if (path.equals("name")) {
value = (String) dp.getVal();
}
else if (path.equals("summary.capacity")) {
// summary.capacity is a boolean
value = String.valueOf( dp.getVal() );
}
else if (path.equals("summary.freeSpace")) {
// summary.freeSpace is an xsd:long
value = String.valueOf( dp.getVal() );
}
else if (path.equals("summary.uncommitted")) {
// summary.uncommitted is an xsd:long
value = String.valueOf( dp.getVal() );
}
System.out.println(path + " = " + value);
}
}
}
}