Search in sources :

Example 11 with HostDatastoreBrowserSearchResults

use of com.vmware.vim25.HostDatastoreBrowserSearchResults in project cloudstack by apache.

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) {
        List<FileInfo> info = results.getFile();
        if (info != null && info.size() > 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 12 with HostDatastoreBrowserSearchResults

use of com.vmware.vim25.HostDatastoreBrowserSearchResults in project cloudstack by apache.

the class DatastoreMO method searchFileInSubFolders.

public String searchFileInSubFolders(String fileName, boolean caseInsensitive, String excludeFolders) throws Exception {
    String datastorePath = "[" + getName() + "]";
    String rootDirectoryFilePath = String.format("%s %s", datastorePath, fileName);
    String[] searchExcludedFolders = getSearchExcludedFolders(excludeFolders);
    if (fileExists(rootDirectoryFilePath)) {
        return rootDirectoryFilePath;
    }
    String parentFolderPath;
    String absoluteFileName = null;
    s_logger.info("Searching file " + fileName + " in " + datastorePath);
    HostDatastoreBrowserMO browserMo = getHostDatastoreBrowserMO();
    ArrayList<HostDatastoreBrowserSearchResults> results = browserMo.searchDatastoreSubFolders("[" + getName() + "]", fileName, caseInsensitive);
    if (results != null && results.size() > 1) {
        s_logger.warn("Multiple files with name " + fileName + " exists in datastore " + datastorePath + ". Trying to choose first file found in search attempt.");
    } else if (results == null) {
        String msg = "No file found with name " + fileName + " found in datastore " + datastorePath;
        s_logger.error(msg);
        throw new CloudException(msg);
    }
    for (HostDatastoreBrowserSearchResults result : results) {
        List<FileInfo> info = result.getFile();
        if (info != null && info.size() > 0) {
            for (FileInfo fi : info) {
                absoluteFileName = parentFolderPath = result.getFolderPath();
                s_logger.info("Found file " + fileName + " in datastore at " + absoluteFileName);
                if (parentFolderPath.endsWith("]"))
                    absoluteFileName += " ";
                else if (!parentFolderPath.endsWith("/"))
                    absoluteFileName += "/";
                absoluteFileName += fi.getPath();
                if (isValidCloudStackFolderPath(parentFolderPath, searchExcludedFolders)) {
                    return absoluteFileName;
                }
                break;
            }
        }
    }
    return absoluteFileName;
}
Also used : HostDatastoreBrowserSearchResults(com.vmware.vim25.HostDatastoreBrowserSearchResults) FileInfo(com.vmware.vim25.FileInfo) CloudException(com.cloud.exception.CloudException)

Example 13 with HostDatastoreBrowserSearchResults

use of com.vmware.vim25.HostDatastoreBrowserSearchResults in project cloudstack by apache.

the class HostDatastoreBrowserMO method searchDatastore.

public HostDatastoreBrowserSearchResults searchDatastore(String datastorePath, HostDatastoreBrowserSearchSpec searchSpec) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - searchDatastore(). target mor: " + _mor.getValue() + ", file datastore path: " + datastorePath);
    try {
        ManagedObjectReference morTask = _context.getService().searchDatastoreTask(_mor, datastorePath, searchSpec);
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (result) {
            _context.waitForTaskProgressDone(morTask);
            return (HostDatastoreBrowserSearchResults) _context.getVimClient().getDynamicProperty(morTask, "info.result");
        } else {
            s_logger.error("VMware searchDaastore_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
    } finally {
        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - searchDatastore() done");
    }
    return null;
}
Also used : HostDatastoreBrowserSearchResults(com.vmware.vim25.HostDatastoreBrowserSearchResults) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 14 with HostDatastoreBrowserSearchResults

use of com.vmware.vim25.HostDatastoreBrowserSearchResults in project cloudstack by apache.

the class HostDatastoreBrowserMO method searchDatastore.

public HostDatastoreBrowserSearchResults searchDatastore(String datastorePath, String fileName, boolean caseInsensitive) throws Exception {
    HostDatastoreBrowserSearchSpec spec = new HostDatastoreBrowserSearchSpec();
    spec.setSearchCaseInsensitive(caseInsensitive);
    spec.getMatchPattern().add(fileName);
    return searchDatastore(datastorePath, spec);
}
Also used : HostDatastoreBrowserSearchSpec(com.vmware.vim25.HostDatastoreBrowserSearchSpec)

Example 15 with HostDatastoreBrowserSearchResults

use of com.vmware.vim25.HostDatastoreBrowserSearchResults in project cloudstack by apache.

the class HostDatastoreBrowserMO method searchDatastoreSubFolders.

public ArrayList<HostDatastoreBrowserSearchResults> searchDatastoreSubFolders(String datastorePath, String fileName, boolean caseInsensitive) throws Exception {
    HostDatastoreBrowserSearchSpec spec = new HostDatastoreBrowserSearchSpec();
    spec.setSearchCaseInsensitive(caseInsensitive);
    spec.getMatchPattern().add(fileName);
    return searchDatastoreSubFolders(datastorePath, spec);
}
Also used : HostDatastoreBrowserSearchSpec(com.vmware.vim25.HostDatastoreBrowserSearchSpec)

Aggregations

HostDatastoreBrowserSearchResults (com.vmware.vim25.HostDatastoreBrowserSearchResults)11 FileInfo (com.vmware.vim25.FileInfo)8 HostDatastoreBrowserSearchSpec (com.vmware.vim25.HostDatastoreBrowserSearchSpec)6 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)5 FileQueryFlags (com.vmware.vim25.FileQueryFlags)2 ArrayList (java.util.ArrayList)2 CloudException (com.cloud.exception.CloudException)1 DatastoreFile (com.cloud.hypervisor.vmware.mo.DatastoreFile)1 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)1 HostDatastoreBrowserMO (com.cloud.hypervisor.vmware.mo.HostDatastoreBrowserMO)1 Before (org.junit.Before)1