Search in sources :

Example 6 with Datacenter

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

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.setPathSet(new String[] { "name" });
    TraversalSpec entityParentTraversal = new TraversalSpec();
    entityParentTraversal.setType("ManagedEntity");
    entityParentTraversal.setPath("parent");
    entityParentTraversal.setName("entityParentTraversal");
    entityParentTraversal.setSelectSet(new SelectionSpec[] { new SelectionSpec(null, null, "entityParentTraversal") });
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(morEntity);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.setSelectSet(new SelectionSpec[] { entityParentTraversal });
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.setPropSet(new PropertySpec[] { pSpec });
    pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
    ObjectContent[] ocs = context.getService().retrieveProperties(context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
    assert (ocs != null);
    assert (ocs[0].getObj() != null);
    assert (ocs[0].getPropSet(0) != null);
    assert (ocs[0].getPropSet(0).getVal() != null);
    String dcName = ocs[0].getPropSet(0).getVal().toString();
    return new Pair<DatacenterMO, String>(new DatacenterMO(context, ocs[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) Pair(com.cloud.utils.Pair)

Example 7 with Datacenter

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

the class DatacenterMO method getVmPropertiesOnDatacenterVmFolder.

public ObjectContent[] getVmPropertiesOnDatacenterVmFolder(String[] propertyPaths) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.setPathSet(propertyPaths);
    TraversalSpec dc2VmFolderTraversal = new TraversalSpec();
    dc2VmFolderTraversal.setType("Datacenter");
    dc2VmFolderTraversal.setPath("vmFolder");
    dc2VmFolderTraversal.setName("dc2VmFolderTraversal");
    SelectionSpec recurseFolders = new SelectionSpec();
    recurseFolders.setName("folder2childEntity");
    TraversalSpec folder2childEntity = new TraversalSpec();
    folder2childEntity.setType("Folder");
    folder2childEntity.setPath("childEntity");
    folder2childEntity.setName(recurseFolders.getName());
    folder2childEntity.setSelectSet(new SelectionSpec[] { recurseFolders });
    dc2VmFolderTraversal.setSelectSet(new SelectionSpec[] { folder2childEntity });
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.setSelectSet(new SelectionSpec[] { dc2VmFolderTraversal });
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.setPropSet(new PropertySpec[] { pSpec });
    pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
    return _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
}
Also used : 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)

Example 8 with Datacenter

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

the class VirtualMachineMO method getHttpAccessPathInfo.

/**
	 * Retrieve path info to access VM files via vSphere web interface 
	 * @return [0] vm-name, [1] data-center-name, [2] datastore-name 
	 * @throws Exception
	 */
public String[] getHttpAccessPathInfo() throws Exception {
    String[] pathInfo = new String[3];
    Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
    VirtualMachineFileInfo fileInfo = getFileInfo();
    String vmxFilePath = fileInfo.getVmPathName();
    String[] vmxPathTokens = vmxFilePath.split("\\[|\\]|/");
    assert (vmxPathTokens.length == 4);
    // vSphere vm name
    pathInfo[1] = vmxPathTokens[1].trim();
    // vSphere datacenter name
    pathInfo[2] = dcInfo.second();
    // vSphere datastore name
    pathInfo[3] = vmxPathTokens[0].trim();
    return pathInfo;
}
Also used : VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo)

Example 9 with Datacenter

use of com.vmware.vim25.mo.Datacenter 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 10 with Datacenter

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

the class VmwareClient method constructCompleteTraversalSpec.

/*
     * @return An array of SelectionSpec covering VM, Host, Resource pool,
     * Cluster Compute Resource and Datastore.
     */
private List<SelectionSpec> constructCompleteTraversalSpec() {
    // ResourcePools to VM: RP -> VM
    TraversalSpec rpToVm = new TraversalSpec();
    rpToVm.setName("rpToVm");
    rpToVm.setType("ResourcePool");
    rpToVm.setPath("vm");
    rpToVm.setSkip(Boolean.FALSE);
    // VirtualApp to VM: vApp -> VM
    TraversalSpec vAppToVM = new TraversalSpec();
    vAppToVM.setName("vAppToVM");
    vAppToVM.setType("VirtualApp");
    vAppToVM.setPath("vm");
    // Host to VM: HostSystem -> VM
    TraversalSpec hToVm = new TraversalSpec();
    hToVm.setType("HostSystem");
    hToVm.setPath("vm");
    hToVm.setName("hToVm");
    hToVm.getSelectSet().add(getSelectionSpec("VisitFolders"));
    hToVm.setSkip(Boolean.FALSE);
    // DataCenter to DataStore: DC -> DS
    TraversalSpec dcToDs = new TraversalSpec();
    dcToDs.setType("Datacenter");
    dcToDs.setPath("datastore");
    dcToDs.setName("dcToDs");
    dcToDs.setSkip(Boolean.FALSE);
    // Recurse through all ResourcePools
    TraversalSpec rpToRp = new TraversalSpec();
    rpToRp.setType("ResourcePool");
    rpToRp.setPath("resourcePool");
    rpToRp.setSkip(Boolean.FALSE);
    rpToRp.setName("rpToRp");
    rpToRp.getSelectSet().add(getSelectionSpec("rpToRp"));
    TraversalSpec crToRp = new TraversalSpec();
    crToRp.setType("ComputeResource");
    crToRp.setPath("resourcePool");
    crToRp.setSkip(Boolean.FALSE);
    crToRp.setName("crToRp");
    crToRp.getSelectSet().add(getSelectionSpec("rpToRp"));
    TraversalSpec crToH = new TraversalSpec();
    crToH.setSkip(Boolean.FALSE);
    crToH.setType("ComputeResource");
    crToH.setPath("host");
    crToH.setName("crToH");
    TraversalSpec dcToHf = new TraversalSpec();
    dcToHf.setSkip(Boolean.FALSE);
    dcToHf.setType("Datacenter");
    dcToHf.setPath("hostFolder");
    dcToHf.setName("dcToHf");
    dcToHf.getSelectSet().add(getSelectionSpec("VisitFolders"));
    TraversalSpec vAppToRp = new TraversalSpec();
    vAppToRp.setName("vAppToRp");
    vAppToRp.setType("VirtualApp");
    vAppToRp.setPath("resourcePool");
    vAppToRp.getSelectSet().add(getSelectionSpec("rpToRp"));
    TraversalSpec dcToVmf = new TraversalSpec();
    dcToVmf.setType("Datacenter");
    dcToVmf.setSkip(Boolean.FALSE);
    dcToVmf.setPath("vmFolder");
    dcToVmf.setName("dcToVmf");
    dcToVmf.getSelectSet().add(getSelectionSpec("VisitFolders"));
    // For Folder -> Folder recursion
    TraversalSpec visitFolders = new TraversalSpec();
    visitFolders.setType("Folder");
    visitFolders.setPath("childEntity");
    visitFolders.setSkip(Boolean.FALSE);
    visitFolders.setName("VisitFolders");
    List<SelectionSpec> sspecarrvf = new ArrayList<SelectionSpec>();
    sspecarrvf.add(getSelectionSpec("crToRp"));
    sspecarrvf.add(getSelectionSpec("crToH"));
    sspecarrvf.add(getSelectionSpec("dcToVmf"));
    sspecarrvf.add(getSelectionSpec("dcToHf"));
    sspecarrvf.add(getSelectionSpec("vAppToRp"));
    sspecarrvf.add(getSelectionSpec("vAppToVM"));
    sspecarrvf.add(getSelectionSpec("dcToDs"));
    sspecarrvf.add(getSelectionSpec("hToVm"));
    sspecarrvf.add(getSelectionSpec("rpToVm"));
    sspecarrvf.add(getSelectionSpec("VisitFolders"));
    visitFolders.getSelectSet().addAll(sspecarrvf);
    List<SelectionSpec> resultspec = new ArrayList<SelectionSpec>();
    resultspec.add(visitFolders);
    resultspec.add(crToRp);
    resultspec.add(crToH);
    resultspec.add(dcToVmf);
    resultspec.add(dcToHf);
    resultspec.add(vAppToRp);
    resultspec.add(vAppToVM);
    resultspec.add(dcToDs);
    resultspec.add(hToVm);
    resultspec.add(rpToVm);
    resultspec.add(rpToRp);
    return resultspec;
}
Also used : TraversalSpec(com.vmware.vim25.TraversalSpec) SelectionSpec(com.vmware.vim25.SelectionSpec) ArrayList(java.util.ArrayList)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)28 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)23 TraversalSpec (com.vmware.vim25.TraversalSpec)21 ObjectSpec (com.vmware.vim25.ObjectSpec)20 PropertySpec (com.vmware.vim25.PropertySpec)20 HostSystem (com.vmware.vim25.mo.HostSystem)20 ArrayList (java.util.ArrayList)17 RemoteException (java.rmi.RemoteException)15 ObjectContent (com.vmware.vim25.ObjectContent)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 VcenterObjectConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectConnectionException)11 VcenterObjectNotFoundException (com.emc.storageos.vcentercontroller.exceptions.VcenterObjectNotFoundException)11 VcenterServerConnectionException (com.emc.storageos.vcentercontroller.exceptions.VcenterServerConnectionException)11 VcenterSystemException (com.emc.storageos.vcentercontroller.exceptions.VcenterSystemException)11 SelectionSpec (com.vmware.vim25.SelectionSpec)11 ClusterComputeResource (com.vmware.vim25.mo.ClusterComputeResource)11 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)10 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)10 VcenterDataCenter (com.emc.storageos.db.client.model.VcenterDataCenter)10 Vcenter (com.emc.storageos.db.client.model.Vcenter)9