Search in sources :

Example 51 with TraversalSpec

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;
}
Also used : TraversalSpec(com.vmware.vim25.TraversalSpec) SelectionSpec(com.vmware.vim25.SelectionSpec) ArrayList(java.util.ArrayList)

Example 52 with 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;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VimPortType(com.vmware.vim25.VimPortType) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

TraversalSpec (com.vmware.vim25.TraversalSpec)52 ObjectSpec (com.vmware.vim25.ObjectSpec)47 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)47 PropertySpec (com.vmware.vim25.PropertySpec)47 ObjectContent (com.vmware.vim25.ObjectContent)34 ArrayList (java.util.ArrayList)30 SelectionSpec (com.vmware.vim25.SelectionSpec)16 DynamicProperty (com.vmware.vim25.DynamicProperty)14 Gson (com.google.gson.Gson)10 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)7 Pair (com.cloud.utils.Pair)4 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)2 DVPortgroupConfigInfo (com.vmware.vim25.DVPortgroupConfigInfo)2 ObjectSpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.ObjectSpecBuilder)1 PropertyFilterSpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertyFilterSpecBuilder)1 PropertySpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder)1 SelectionSpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.SelectionSpecBuilder)1 TraversalSpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.TraversalSpecBuilder)1 RetrieveOptions (com.vmware.vim25.RetrieveOptions)1