Search in sources :

Example 1 with Folder

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

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

the class DatastoreMO method folderExists.

public boolean folderExists(String folderParentDatastorePath, String folderName) throws Exception {
    HostDatastoreBrowserMO browserMo = getHostDatastoreBrowserMO();
    HostDatastoreBrowserSearchResults results = browserMo.searchDatastore(folderParentDatastorePath, folderName, true);
    if (results != null) {
        FileInfo[] info = results.getFile();
        if (info != null && info.length > 0) {
            s_logger.info("Folder " + folderName + " exists on datastore");
            return true;
        }
    }
    s_logger.info("Folder " + folderName + " does not exist on datastore");
    return false;
}
Also used : HostDatastoreBrowserSearchResults(com.vmware.vim25.HostDatastoreBrowserSearchResults) FileInfo(com.vmware.vim25.FileInfo)

Example 3 with Folder

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

the class VirtualMachineMO method getVmxHttpAccessUrl.

public String getVmxHttpAccessUrl() throws Exception {
    Pair<DatacenterMO, String> dcInfo = getOwnerDatacenter();
    VirtualMachineFileInfo fileInfo = getFileInfo();
    String vmxFilePath = fileInfo.getVmPathName();
    String[] vmxPathTokens = vmxFilePath.split("\\[|\\]|/");
    StringBuffer sb = new StringBuffer("https://" + _context.getServerAddress() + "/folder/");
    sb.append(URLEncoder.encode(vmxPathTokens[2].trim()));
    sb.append("/");
    sb.append(URLEncoder.encode(vmxPathTokens[3].trim()));
    sb.append("?dcPath=");
    sb.append(URLEncoder.encode(dcInfo.second()));
    sb.append("&dsName=");
    sb.append(URLEncoder.encode(vmxPathTokens[1].trim()));
    return sb.toString();
}
Also used : VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo)

Example 4 with Folder

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

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

the class VmwareResource method deleteUnregisteredVmFiles.

private void deleteUnregisteredVmFiles(VirtualMachineFileLayoutEx vmFileLayout, DatacenterMO dcMo, boolean deleteDisks) throws Exception {
    s_logger.debug("Deleting files associated with an existing VM that was unregistered");
    DatastoreFile vmFolder = null;
    try {
        List<VirtualMachineFileLayoutExFileInfo> fileInfo = vmFileLayout.getFile();
        for (VirtualMachineFileLayoutExFileInfo file : fileInfo) {
            DatastoreFile fileInDatastore = new DatastoreFile(file.getName());
            // In case of linked clones, VM file layout includes the base disk so don't delete all disk files.
            if (file.getType().startsWith("disk") || file.getType().startsWith("digest"))
                continue;
            else if (file.getType().equals("config"))
                vmFolder = new DatastoreFile(fileInDatastore.getDatastoreName(), fileInDatastore.getDir());
            DatastoreMO dsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(fileInDatastore.getDatastoreName()));
            s_logger.debug("Deleting file: " + file.getName());
            dsMo.deleteFile(file.getName(), dcMo.getMor(), true);
        }
        // Delete files that are present in the VM folder - this will take care of the VM disks as well.
        DatastoreMO vmFolderDsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(vmFolder.getDatastoreName()));
        String[] files = vmFolderDsMo.listDirContent(vmFolder.getPath());
        if (deleteDisks) {
            for (String file : files) {
                String vmDiskFileFullPath = String.format("%s/%s", vmFolder.getPath(), file);
                s_logger.debug("Deleting file: " + vmDiskFileFullPath);
                vmFolderDsMo.deleteFile(vmDiskFileFullPath, dcMo.getMor(), true);
            }
        }
        // Delete VM folder
        if (deleteDisks || files.length == 0) {
            s_logger.debug("Deleting folder: " + vmFolder.getPath());
            vmFolderDsMo.deleteFolder(vmFolder.getPath(), dcMo.getMor());
        }
    } catch (Exception e) {
        String message = "Failed to delete files associated with an existing VM that was unregistered due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.warn(message, e);
    }
}
Also used : DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VirtualMachineFileLayoutExFileInfo(com.vmware.vim25.VirtualMachineFileLayoutExFileInfo) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)30 TraversalSpec (com.vmware.vim25.TraversalSpec)15 ArrayList (java.util.ArrayList)15 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)13 SelectionSpec (com.vmware.vim25.SelectionSpec)12 ObjectSpec (com.vmware.vim25.ObjectSpec)11 PropertySpec (com.vmware.vim25.PropertySpec)11 TaskInfo (com.vmware.vim25.TaskInfo)10 RemoteException (java.rmi.RemoteException)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)8 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)8 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)8 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)8 VirtualDisk (com.vmware.vim25.VirtualDisk)7 Operation (com.vmware.xenon.common.Operation)7 VimNames (com.vmware.photon.controller.model.adapters.vsphere.util.VimNames)6 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)6 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)6 VirtualMachineDefinedProfileSpec (com.vmware.vim25.VirtualMachineDefinedProfileSpec)6 VirtualMachineRelocateSpec (com.vmware.vim25.VirtualMachineRelocateSpec)6