Hi all,
I am new to VMware and I was trying to understand and play with the Java SDK . th eSDK has getVMByVMname(string vmName) method that includes getVMTraversalSpec() method and retrievePropertiesAllObjects
( List<PropertyFilterSpec> listpfs) that work just fine for the virtual machine. Now the problem arises when I try to make my own code that gets getHostByHostname(String hostName) method which includes getHostTraversalSpec() and retrievePropertiesAllObjects ( List<PropertyFilterSpec> listpfs). It keeps giving me the "duplicate TraversalSpec name" error. The error comes at the fourth line of retrievePropertiesAllObjects(List<PropertyFilterSpec> listpfs) function
RetrieveResult rslts = vimPort.retrievePropertiesEx( propCollectorRef, listpfs, propObjectRetrieveOpts);
. could somebody please help me? It has taken me days to try to make it work but I still get the same error.
the host code is just a slight variation of the Vmcode given in the SDK. The code is given below
/**
*
* @return TraversalSpec specification to get to the VirtualMachine managed
* object.
*/
private static TraversalSpec getVMTraversalSpec() {
// Create a traversal spec that starts from the 'root' objects
// and traverses the inventory tree to get to the VirtualMachines.
// Build the traversal specs bottoms up
// Traversal to get to the VM in a VApp
TraversalSpec vAppToVM = new TraversalSpec();
vAppToVM.setName("vAppToVM");
vAppToVM.setType("VirtualApp");
vAppToVM.setPath("vm");
// Traversal spec for VApp to VApp
TraversalSpec vAppToVApp = new TraversalSpec();
vAppToVApp.setName("vAppToVApp");
vAppToVApp.setType("VirtualApp");
vAppToVApp.setPath("resourcePool");
// SelectionSpec for VApp to VApp recursion
SelectionSpec vAppRecursion = new SelectionSpec();
vAppRecursion.setName("vAppToVApp");
// SelectionSpec to get to a VM in the VApp
SelectionSpec vmInVApp = new SelectionSpec();
vmInVApp.setName("vAppToVM");
// SelectionSpec for both VApp to VApp and VApp to VM
List<SelectionSpec> vAppToVMSS = new ArrayList<SelectionSpec>();
vAppToVMSS.add(vAppRecursion);
vAppToVMSS.add(vmInVApp);
vAppToVApp.getSelectSet().addAll(vAppToVMSS);
// This SelectionSpec is used for recursion for Folder recursion
SelectionSpec sSpec = new SelectionSpec();
sSpec.setName("VisitFolders");
// Traversal to get to the vmFolder from DataCenter
TraversalSpec dataCenterToVMFolder = new TraversalSpec();
dataCenterToVMFolder.setName("DataCenterToVMFolder");
dataCenterToVMFolder.setType("Datacenter");
dataCenterToVMFolder.setPath("vmFolder");
dataCenterToVMFolder.setSkip(false);
dataCenterToVMFolder.getSelectSet().add(sSpec);
// TraversalSpec to get to the DataCenter from rootFolder
TraversalSpec traversalSpec = new TraversalSpec();
traversalSpec.setName("VisitFolders");
traversalSpec.setType("Folder");
traversalSpec.setPath("childEntity");
traversalSpec.setSkip(false);
List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
sSpecArr.add(sSpec);
sSpecArr.add(dataCenterToVMFolder);
sSpecArr.add(vAppToVM);
sSpecArr.add(vAppToVApp);
traversalSpec.getSelectSet().addAll(sSpecArr);
return traversalSpec;
}
private static TraversalSpec getHostTraversalSpec() {
// Create a traversal spec that starts from the 'root' objects
// and traverses the inventory tree to get to the VirtualMachines.
// Build the traversal specs bottoms up
// Traversal to get to the Host a VApp
TraversalSpec vAppToHost = new TraversalSpec();
vAppToHost.setName("vAppToHost");
vAppToHost.setType("VirtualApp");
vAppToHost.setPath("host");
// Traversal spec for VApp to VApp
TraversalSpec vAppToVApp = new TraversalSpec();
vAppToVApp.setName("vAppToVApp");
vAppToVApp.setType("VirtualApp");
vAppToVApp.setPath("resourcePool");
// SelectionSpec for VApp to VApp recursion
SelectionSpec vAppRecursion = new SelectionSpec();
vAppRecursion.setName("vAppToVApp");
// SelectionSpec to get to a Host in the VApp
SelectionSpec hostInVApp = new SelectionSpec();
hostInVApp.setName("vAppToHost");
// SelectionSpec for both VApp to VApp and VApp to Host
List<SelectionSpec> vAppToHosts = new ArrayList<SelectionSpec>();
vAppToHosts.add(vAppRecursion);
vAppToHosts.add(hostInVApp);
vAppToVApp.getSelectSet().addAll(vAppToHosts);
// Traversal to get to the host from ComputeResource
TraversalSpec computeResourceToHostSystem = new TraversalSpec();
computeResourceToHostSystem.setName("computeResourceToHostSystem");
computeResourceToHostSystem.setType("ComputeResource");
computeResourceToHostSystem.setPath("host");
computeResourceToHostSystem.setSkip(false);
// This SelectionSpec is used for recursion for Folder recursion
SelectionSpec sSpec = new SelectionSpec();
sSpec.setName("VisitFolders");
// SelectionSpec sSpec1 = new SelectionSpec();
// sSpec1.setName("VisitFolders");
//SelectionSpec[] sSpecs = new SelectionSpec[]{sSpec};
List<SelectionSpec> sSpecs = new ArrayList<SelectionSpec>();
computeResourceToHostSystem.getSelectSet().add(sSpec);//.setSelectSet(sSpecs);
sSpecs.add(sSpec);
// Traversal to get to the hostFolder from DataCenter
TraversalSpec dataCenterToHostFolder = new TraversalSpec();
dataCenterToHostFolder.setName("DataCenterToHostFolder");
dataCenterToHostFolder.setType("Datacenter");
dataCenterToHostFolder.setPath("hostFolder");
dataCenterToHostFolder.setSkip(false);
dataCenterToHostFolder.getSelectSet().add(sSpec);
sSpecs.add(sSpec);
// TraversalSpec to get to the DataCenter from rootFolder
TraversalSpec traversalSpec = new TraversalSpec();
traversalSpec.setName("VisitFolders");
traversalSpec.setType("Folder");
traversalSpec.setPath("childEntity");
traversalSpec.setSkip(false);
List<SelectionSpec> sSpecArr = new ArrayList<SelectionSpec>();
sSpecArr.add(sSpec);
sSpecArr.add(computeResourceToHostSystem);
sSpecArr.add(dataCenterToHostFolder);
sSpecArr.add(vAppToHost);
sSpecArr.add(vAppToVApp);
sSpecArr.add(vAppToVApp);
traversalSpec.getSelectSet().addAll(sSpecArr);
return traversalSpec;
}
/**
* Get the MOR of the Virtual Machine by its name.
*
* @param vmName
* The name of the Virtual Machine
* @return The Managed Object reference for this VM
*/
private static ManagedObjectReference getVmByVMname(String vmName) {
ManagedObjectReference retVal = null;
ManagedObjectReference rootFolder = serviceContent.getRootFolder();
try {
TraversalSpec tSpec = getVMTraversalSpec();
// Create Property Spec
PropertySpec propertySpec = new PropertySpec();
propertySpec.setAll(Boolean.FALSE);
propertySpec.getPathSet().add("name");
propertySpec.setType("VirtualMachine");
// Now create Object Spec
ObjectSpec objectSpec = new ObjectSpec();
objectSpec.setObj(rootFolder);
objectSpec.setSkip(Boolean.TRUE);
objectSpec.getSelectSet().add(tSpec);
// Create PropertyFilterSpec using the PropertySpec and ObjectPec
// created above.
PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
propertyFilterSpec.getPropSet().add(propertySpec);
propertyFilterSpec.getObjectSet().add(objectSpec);
List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(
1);
listpfs.add(propertyFilterSpec);
List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
if (listobjcont != null) {
for (ObjectContent oc : listobjcont) {
ManagedObjectReference mr = oc.getObj();
String vmnm = null;
List<DynamicProperty> dps = oc.getPropSet();
if (dps != null) {
for (DynamicProperty dp : dps) {
vmnm = (String) dp.getVal();
}
}
if (vmnm != null && vmnm.equals(vmName)) {
retVal = mr;
break;
}
}
}
} catch (SOAPFaultException sfe) {
printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
return retVal;
}
//gets the host Managed object reference by its name
private static ManagedObjectReference getHostByHostname(String hostName) { //getHostByName(string entityname)
ManagedObjectReference retVal = null;
ManagedObjectReference rootFolder = serviceContent.getRootFolder();
try {
TraversalSpec tSpec = getHostTraversalSpec();//getVMTraversalSpec();//
// Create Property Spec
PropertySpec propertySpec = new PropertySpec();
propertySpec.setAll(Boolean.FALSE);
propertySpec.getPathSet().add("name");//random guess ("resourcePool");
propertySpec.setType("HostSystem");//propertySpec.setType("VirtualMachine");
// Now create Object Spec
ObjectSpec objectSpec = new ObjectSpec();
objectSpec.setObj(rootFolder);
objectSpec.setSkip(Boolean.TRUE);
objectSpec.getSelectSet().add(tSpec);
// Create PropertyFilterSpec using the PropertySpec and ObjectPec
// created above.
PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
propertyFilterSpec.getPropSet().add(propertySpec);//propertyFilterSpec.setPropSet(propertySpecs);
propertyFilterSpec.getObjectSet().add(objectSpec);//propertyFilterSpec.setObjectSet(objectSpecs);
List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
listpfs.add(propertyFilterSpec);
List<ObjectContent> listobjcont = retrievePropertiesAllObjects(listpfs);
//PropertyFilterSpec[] propertyFilterSpecs = new PropertyFilterSpec[]{propertyFilterSpec};
//ObjectContent[] oCont = vimPort.retrievePropertiesAllObjects(propCollectorRef, propertyFilterSpecs);
if (listobjcont != null) {
for (ObjectContent oc : listobjcont) {
ManagedObjectReference mr = oc.getObj();
String vmnm = null;
List<DynamicProperty> dps = oc.getPropSet();
if (dps != null) {
for (DynamicProperty dp : dps) {
vmnm = (String) dp.getVal();
}
}
if (vmnm != null && vmnm.equals(hostName)) {
retVal = mr;
break;
}
}
}
} catch (SOAPFaultException sfe) {
printSoapFaultException(sfe);
} catch (Exception e) {
e.printStackTrace();
}
return retVal;
}
private static List<ObjectContent> retrievePropertiesAllObjects(
List<PropertyFilterSpec> listpfs) throws Exception {
RetrieveOptions propObjectRetrieveOpts = new RetrieveOptions();
List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();
try {
RetrieveResult rslts = vimPort.retrievePropertiesEx(
propCollectorRef, listpfs, propObjectRetrieveOpts);
if (rslts != null && rslts.getObjects() != null
&& !rslts.getObjects().isEmpty()) {
listobjcontent.addAll(rslts.getObjects());
}
String token = null;
if (rslts != null && rslts.getToken() != null) {
token = rslts.getToken();
}
while (token != null && !token.isEmpty()) {
rslts = vimPort.continueRetrievePropertiesEx(propCollectorRef,
token);
token = null;
if (rslts != null) {
token = rslts.getToken();
if (rslts.getObjects() != null
&& !rslts.getObjects().isEmpty()) {
listobjcontent.addAll(rslts.getObjects());
}
}
}
} catch (SOAPFaultException sfe) {
printSoapFaultException(sfe);
} catch (Exception e) {
System.out.println(" : Failed Getting Contents");
e.printStackTrace();
}
return listobjcontent;
}