Search in sources :

Example 31 with ManagedEntity

use of com.vmware.vim25.mo.ManagedEntity in project cloudstack by apache.

the class DatacenterMO method getOwnerDatacenter.

public static Pair<DatacenterMO, String> getOwnerDatacenter(VmwareContext context, ManagedObjectReference morEntity) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Datacenter");
    pSpec.getPathSet().add("name");
    TraversalSpec entityParentTraversal = new TraversalSpec();
    entityParentTraversal.setType("ManagedEntity");
    entityParentTraversal.setPath("parent");
    entityParentTraversal.setName("entityParentTraversal");
    SelectionSpec selSpec = new SelectionSpec();
    selSpec.setName("entityParentTraversal");
    entityParentTraversal.getSelectSet().add(selSpec);
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(morEntity);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(entityParentTraversal);
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);
    List<ObjectContent> ocs = context.getService().retrieveProperties(context.getPropertyCollector(), pfSpecArr);
    assert (ocs != null && ocs.size() > 0);
    assert (ocs.get(0).getObj() != null);
    assert (ocs.get(0).getPropSet().get(0) != null);
    assert (ocs.get(0).getPropSet().get(0).getVal() != null);
    String dcName = ocs.get(0).getPropSet().get(0).getVal().toString();
    return new Pair<DatacenterMO, String>(new DatacenterMO(context, ocs.get(0).getObj()), dcName);
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) SelectionSpec(com.vmware.vim25.SelectionSpec) ArrayList(java.util.ArrayList) Pair(com.cloud.utils.Pair)

Example 32 with ManagedEntity

use of com.vmware.vim25.mo.ManagedEntity in project cloudstack by apache.

the class VmwareContext method getHostMorByPath.

public ManagedObjectReference getHostMorByPath(String inventoryPath) throws Exception {
    assert (inventoryPath != null);
    String[] tokens;
    if (inventoryPath.startsWith("/"))
        tokens = inventoryPath.substring(1).split("/");
    else
        tokens = inventoryPath.split("/");
    ManagedObjectReference mor = getRootFolder();
    for (int i = 0; i < tokens.length; i++) {
        String token = tokens[i];
        List<ObjectContent> ocs;
        PropertySpec pSpec = null;
        ObjectSpec oSpec = null;
        if (mor.getType().equalsIgnoreCase("Datacenter")) {
            pSpec = new PropertySpec();
            pSpec.setAll(false);
            pSpec.setType("ManagedEntity");
            pSpec.getPathSet().add("name");
            TraversalSpec dcHostFolderTraversal = new TraversalSpec();
            dcHostFolderTraversal.setType("Datacenter");
            dcHostFolderTraversal.setPath("hostFolder");
            dcHostFolderTraversal.setName("dcHostFolderTraversal");
            oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.getSelectSet().add(dcHostFolderTraversal);
        } else if (mor.getType().equalsIgnoreCase("Folder")) {
            pSpec = new PropertySpec();
            pSpec.setAll(false);
            pSpec.setType("ManagedEntity");
            pSpec.getPathSet().add("name");
            TraversalSpec folderChildrenTraversal = new TraversalSpec();
            folderChildrenTraversal.setType("Folder");
            folderChildrenTraversal.setPath("childEntity");
            folderChildrenTraversal.setName("folderChildrenTraversal");
            oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.getSelectSet().add(folderChildrenTraversal);
        } else if (mor.getType().equalsIgnoreCase("ClusterComputeResource")) {
            pSpec = new PropertySpec();
            pSpec.setType("ManagedEntity");
            pSpec.getPathSet().add("name");
            TraversalSpec clusterHostTraversal = new TraversalSpec();
            clusterHostTraversal.setType("ClusterComputeResource");
            clusterHostTraversal.setPath("host");
            clusterHostTraversal.setName("folderChildrenTraversal");
            oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.getSelectSet().add(clusterHostTraversal);
        } else {
            s_logger.error("Invalid inventory path, path element can only be datacenter and folder");
            return null;
        }
        PropertyFilterSpec pfSpec = new PropertyFilterSpec();
        pfSpec.getPropSet().add(pSpec);
        pfSpec.getObjectSet().add(oSpec);
        List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
        pfSpecArr.add(pfSpec);
        ocs = getService().retrieveProperties(getPropertyCollector(), pfSpecArr);
        if (ocs != null && ocs.size() > 0) {
            boolean found = false;
            for (ObjectContent oc : ocs) {
                String name = oc.getPropSet().get(0).getVal().toString();
                if (name.equalsIgnoreCase(token) || name.equalsIgnoreCase("host")) {
                    mor = oc.getObj();
                    found = true;
                    if (name.equalsIgnoreCase("host"))
                        i--;
                    break;
                }
            }
            if (!found) {
                s_logger.error("Path element points to an un-existing inventory entity");
                return null;
            }
        } else {
            s_logger.error("Path element points to an un-existing inventory entity");
            return null;
        }
    }
    return mor;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ArrayList(java.util.ArrayList) 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

ManagedEntity (com.vmware.vim25.mo.ManagedEntity)24 RemoteException (java.rmi.RemoteException)14 InventoryNavigator (com.vmware.vim25.mo.InventoryNavigator)12 MalformedURLException (java.net.MalformedURLException)10 HostSystem (com.vmware.vim25.mo.HostSystem)7 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)6 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)5 ObjectSpec (com.vmware.vim25.ObjectSpec)5 PerfCounterInfo (com.vmware.vim25.PerfCounterInfo)5 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)5 PropertySpec (com.vmware.vim25.PropertySpec)5 TraversalSpec (com.vmware.vim25.TraversalSpec)5 Datastore (com.vmware.vim25.mo.Datastore)5 ArrayList (java.util.ArrayList)5 VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)4 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)4 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)4 VcenterSystemException (com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)4 ObjectContent (com.vmware.vim25.ObjectContent)4 SelectionSpec (com.vmware.vim25.SelectionSpec)4