use of com.vmware.vim25.TraversalSpec in project cloudstack by apache.
the class VMwareUtil method getVMTraversalSpec.
private static TraversalSpec getVMTraversalSpec() {
// Create a TraversalSpec that starts from the 'root' objects
// and traverses the inventory tree to get to the VirtualMachines.
// Build the traversal specs bottoms up
// TraversalSpec to get to the VM in a vApp
TraversalSpec vAppToVM = new TraversalSpec();
vAppToVM.setName("vAppToVM");
vAppToVM.setType("VirtualApp");
vAppToVM.setPath("vm");
// TraversalSpec 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<>();
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<>();
sSpecArr.add(sSpec);
sSpecArr.add(dataCenterToVMFolder);
sSpecArr.add(vAppToVM);
sSpecArr.add(vAppToVApp);
traversalSpec.getSelectSet().addAll(sSpecArr);
return traversalSpec;
}
use of com.vmware.vim25.TraversalSpec in project cloudstack by apache.
the class VMwareUtil method getVms.
public static Map<String, ManagedObjectReference> getVms(VMwareConnection connection) throws Exception {
Map<String, ManagedObjectReference> nameToVm = new HashMap<>();
ManagedObjectReference rootFolder = connection.getServiceContent().getRootFolder();
TraversalSpec tSpec = getVMTraversalSpec();
PropertySpec propertySpec = new PropertySpec();
propertySpec.setAll(Boolean.FALSE);
propertySpec.getPathSet().add("name");
propertySpec.setType("VirtualMachine");
ObjectSpec objectSpec = new ObjectSpec();
objectSpec.setObj(rootFolder);
objectSpec.setSkip(Boolean.TRUE);
objectSpec.getSelectSet().add(tSpec);
PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
propertyFilterSpec.getPropSet().add(propertySpec);
propertyFilterSpec.getObjectSet().add(objectSpec);
List<PropertyFilterSpec> lstPfs = new ArrayList<>(1);
lstPfs.add(propertyFilterSpec);
VimPortType vimPortType = connection.getVimPortType();
ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();
List<ObjectContent> lstObjectContent = retrievePropertiesAllObjects(lstPfs, vimPortType, propertyCollector);
if (lstObjectContent != null) {
for (ObjectContent oc : lstObjectContent) {
ManagedObjectReference mor = oc.getObj();
List<DynamicProperty> dps = oc.getPropSet();
String vmName = null;
if (dps != null) {
for (DynamicProperty dp : dps) {
vmName = (String) dp.getVal();
}
}
if (vmName != null) {
nameToVm.put(vmName, mor);
}
}
}
return nameToVm;
}
Aggregations