Search in sources :

Example 6 with ObjectContent

use of com.vmware.vim25.ObjectContent in project CloudStack-archive by CloudStack-extras.

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];
        ObjectContent[] ocs;
        if (mor.getType().equalsIgnoreCase("Datacenter")) {
            PropertySpec pSpec = new PropertySpec();
            pSpec.setType("ManagedEntity");
            pSpec.setPathSet(new String[] { "name" });
            TraversalSpec dcHostFolderTraversal = new TraversalSpec();
            dcHostFolderTraversal.setType("Datacenter");
            dcHostFolderTraversal.setPath("hostFolder");
            dcHostFolderTraversal.setName("dcHostFolderTraversal");
            ObjectSpec oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.setSelectSet(new SelectionSpec[] { dcHostFolderTraversal });
            PropertyFilterSpec pfSpec = new PropertyFilterSpec();
            pfSpec.setPropSet(new PropertySpec[] { pSpec });
            pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
            ocs = getService().retrieveProperties(getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
        } else if (mor.getType().equalsIgnoreCase("Folder")) {
            PropertySpec pSpec = new PropertySpec();
            pSpec.setType("ManagedEntity");
            pSpec.setPathSet(new String[] { "name" });
            TraversalSpec folderChildrenTraversal = new TraversalSpec();
            folderChildrenTraversal.setType("Folder");
            folderChildrenTraversal.setPath("childEntity");
            folderChildrenTraversal.setName("folderChildrenTraversal");
            ObjectSpec oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.setSelectSet(new SelectionSpec[] { folderChildrenTraversal });
            PropertyFilterSpec pfSpec = new PropertyFilterSpec();
            pfSpec.setPropSet(new PropertySpec[] { pSpec });
            pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
            ocs = getService().retrieveProperties(getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
        } else if (mor.getType().equalsIgnoreCase("ClusterComputeResource")) {
            PropertySpec pSpec = new PropertySpec();
            pSpec.setType("ManagedEntity");
            pSpec.setPathSet(new String[] { "name" });
            TraversalSpec clusterHostTraversal = new TraversalSpec();
            clusterHostTraversal.setType("ClusterComputeResource");
            clusterHostTraversal.setPath("host");
            clusterHostTraversal.setName("folderChildrenTraversal");
            ObjectSpec oSpec = new ObjectSpec();
            oSpec.setObj(mor);
            oSpec.setSkip(Boolean.TRUE);
            oSpec.setSelectSet(new SelectionSpec[] { clusterHostTraversal });
            PropertyFilterSpec pfSpec = new PropertyFilterSpec();
            pfSpec.setPropSet(new PropertySpec[] { pSpec });
            pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
            ocs = getService().retrieveProperties(getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
        } else {
            s_logger.error("Invalid inventory path, path element can only be datacenter and folder");
            return null;
        }
        if (ocs != null && ocs.length > 0) {
            boolean found = false;
            for (ObjectContent oc : ocs) {
                String name = oc.getPropSet()[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 : ObjectContent(com.vmware.vim25.ObjectContent) PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) SelectionSpec(com.vmware.vim25.SelectionSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 7 with ObjectContent

use of com.vmware.vim25.ObjectContent in project CloudStack-archive by CloudStack-extras.

the class ClusterMO method getVmPropertiesOnHyperHost.

@Override
public ObjectContent[] getVmPropertiesOnHyperHost(String[] propertyPaths) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() for VM properties. target MOR: " + _mor.get_value() + ", properties: " + new Gson().toJson(propertyPaths));
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.setPathSet(propertyPaths);
    TraversalSpec host2VmFolderTraversal = new TraversalSpec();
    host2VmFolderTraversal.setType("HostSystem");
    host2VmFolderTraversal.setPath("vm");
    host2VmFolderTraversal.setName("host2VmFolderTraversal");
    TraversalSpec cluster2HostFolderTraversal = new TraversalSpec();
    cluster2HostFolderTraversal.setType("ClusterComputeResource");
    cluster2HostFolderTraversal.setPath("host");
    cluster2HostFolderTraversal.setName("cluster2HostFolderTraversal");
    cluster2HostFolderTraversal.setSelectSet(new SelectionSpec[] { host2VmFolderTraversal });
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(getMor());
    oSpec.setSkip(Boolean.TRUE);
    oSpec.setSelectSet(new SelectionSpec[] { cluster2HostFolderTraversal });
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.setPropSet(new PropertySpec[] { pSpec });
    pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
    ObjectContent[] properties = _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() done");
    return properties;
}
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) Gson(com.google.gson.Gson)

Example 8 with ObjectContent

use of com.vmware.vim25.ObjectContent in project CloudStack-archive by CloudStack-extras.

the class ClusterMO method getDatastorePropertiesOnHyperHost.

@Override
public ObjectContent[] getDatastorePropertiesOnHyperHost(String[] propertyPaths) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() on Datastore properties. target MOR: " + _mor.get_value() + ", properties: " + new Gson().toJson(propertyPaths));
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Datastore");
    pSpec.setPathSet(propertyPaths);
    TraversalSpec cluster2DatastoreTraversal = new TraversalSpec();
    cluster2DatastoreTraversal.setType("ClusterComputeResource");
    cluster2DatastoreTraversal.setPath("datastore");
    cluster2DatastoreTraversal.setName("cluster2DatastoreTraversal");
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.setSelectSet(new SelectionSpec[] { cluster2DatastoreTraversal });
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.setPropSet(new PropertySpec[] { pSpec });
    pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
    ObjectContent[] properties = _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() done");
    return properties;
}
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) Gson(com.google.gson.Gson)

Example 9 with ObjectContent

use of com.vmware.vim25.ObjectContent in project CloudStack-archive by CloudStack-extras.

the class ClusterMO method findDatastoreByExportPath.

@Override
public ManagedObjectReference findDatastoreByExportPath(String exportPath) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastoreByExportPath(). target MOR: " + _mor.get_value() + ", exportPath: " + exportPath);
    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] { "info" });
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            DatastoreInfo dsInfo = (DatastoreInfo) oc.getPropSet(0).getVal();
            if (dsInfo != null && dsInfo instanceof NasDatastoreInfo) {
                NasDatastoreInfo info = (NasDatastoreInfo) dsInfo;
                if (info != null) {
                    String vmwareUrl = info.getUrl();
                    if (vmwareUrl.charAt(vmwareUrl.length() - 1) == '/')
                        vmwareUrl = vmwareUrl.substring(0, vmwareUrl.length() - 1);
                    URI uri = new URI(vmwareUrl);
                    if (uri.getPath().equals("/" + exportPath)) {
                        if (s_logger.isTraceEnabled())
                            s_logger.trace("vCenter API trace - findDatastoreByExportPath() done(successfully)");
                        return oc.getObj();
                    }
                }
            }
        }
    }
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastoreByExportPath() done(failed)");
    return null;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) NasDatastoreInfo(com.vmware.vim25.NasDatastoreInfo) DatastoreInfo(com.vmware.vim25.DatastoreInfo) NasDatastoreInfo(com.vmware.vim25.NasDatastoreInfo) URI(java.net.URI)

Example 10 with ObjectContent

use of com.vmware.vim25.ObjectContent in project CloudStack-archive by CloudStack-extras.

the class DatacenterMO method getAllVmsOnDatacenter.

public List<Pair<ManagedObjectReference, String>> getAllVmsOnDatacenter() throws Exception {
    List<Pair<ManagedObjectReference, String>> vms = new ArrayList<Pair<ManagedObjectReference, String>>();
    ObjectContent[] ocs = getVmPropertiesOnDatacenterVmFolder(new String[] { "name" });
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            String vmName = oc.getPropSet(0).getVal().toString();
            vms.add(new Pair<ManagedObjectReference, String>(oc.getObj(), vmName));
        }
    }
    return vms;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) ArrayList(java.util.ArrayList) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Aggregations

ObjectContent (com.vmware.vim25.ObjectContent)65 ObjectSpec (com.vmware.vim25.ObjectSpec)44 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)44 PropertySpec (com.vmware.vim25.PropertySpec)44 TraversalSpec (com.vmware.vim25.TraversalSpec)38 ArrayList (java.util.ArrayList)38 DynamicProperty (com.vmware.vim25.DynamicProperty)34 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)15 Pair (com.cloud.utils.Pair)12 Gson (com.google.gson.Gson)10 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)9 SelectionSpec (com.vmware.vim25.SelectionSpec)9 HashMap (java.util.HashMap)7 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)5 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)4 OptionValue (com.vmware.vim25.OptionValue)4 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)2 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)2 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 DVPortgroupConfigInfo (com.vmware.vim25.DVPortgroupConfigInfo)2