Example #1
0
def get_datastore(session, cluster, datastore_regex=None):
    """Get the datastore list and choose the most preferable one."""
    datastore_ret = session._call_method(vim_util, "get_dynamic_property",
                                         cluster, "ClusterComputeResource",
                                         "datastore")
    if datastore_ret is None:
        raise exception.DatastoreNotFound()

    data_store_mors = datastore_ret.ManagedObjectReference
    data_stores = session._call_method(
        vim_util, "get_properties_for_a_collection_of_objects", "Datastore",
        data_store_mors, [
            "summary.type", "summary.name", "summary.capacity",
            "summary.freeSpace", "summary.accessible",
            "summary.maintenanceMode"
        ])

    best_match = None
    while data_stores:
        best_match = _select_datastore(data_stores, best_match,
                                       datastore_regex)
        token = _get_token(data_stores)
        if not token:
            break
        data_stores = session._call_method(vim_util, "continue_to_get_objects",
                                           token)
    if best_match:
        return best_match
    if datastore_regex:
        raise exception.DatastoreNotFound(
            _("Datastore regex %s did not match any datastores") %
            datastore_regex.pattern)
    else:
        raise exception.DatastoreNotFound()
Example #2
0
def get_datastore_ref_and_name(session,
                               cluster=None,
                               host=None,
                               datastore_regex=None):
    """Get the datastore list and choose the first local storage."""
    if cluster is None and host is None:
        data_stores = session._call_method(
            vim_util, "get_objects", "Datastore", [
                "summary.type", "summary.name", "summary.capacity",
                "summary.freeSpace"
            ])
    else:
        if cluster is not None:
            datastore_ret = session._call_method(vim_util,
                                                 "get_dynamic_property",
                                                 cluster,
                                                 "ClusterComputeResource",
                                                 "datastore")
        else:
            datastore_ret = session._call_method(vim_util,
                                                 "get_dynamic_property", host,
                                                 "HostSystem", "datastore")

        if datastore_ret is None:
            raise exception.DatastoreNotFound()
        data_store_mors = datastore_ret.ManagedObjectReference
        data_stores = session._call_method(
            vim_util, "get_properties_for_a_collection_of_objects",
            "Datastore", data_store_mors, [
                "summary.type", "summary.name", "summary.capacity",
                "summary.freeSpace"
            ])

    while data_stores:
        token = _get_token(data_stores)
        results = _get_datastore_ref_and_name(data_stores, datastore_regex)
        if results:
            if token:
                session._call_method(vim_util, "cancel_retrieve", token)
            return results
        if token:
            data_stores = session._call_method(vim_util,
                                               "continue_to_get_objects",
                                               token)
        else:
            if datastore_regex:
                raise exception.DatastoreNotFound(
                    _("Datastore regex %s did not match any datastores") %
                    datastore_regex.pattern)
            else:
                raise exception.DatastoreNotFound()
    raise exception.DatastoreNotFound()
Example #3
0
def get_datastore_ref_and_name(session, cluster=None, host=None):
    """Get the datastore list and choose the first local storage."""
    if cluster is None and host is None:
        data_stores = session._call_method(
            vim_util, "get_objects", "Datastore", [
                "summary.type", "summary.name", "summary.capacity",
                "summary.freeSpace"
            ])
    else:
        if cluster is not None:
            datastore_ret = session._call_method(vim_util,
                                                 "get_dynamic_property",
                                                 cluster,
                                                 "ClusterComputeResource",
                                                 "datastore")
        else:
            datastore_ret = session._call_method(vim_util,
                                                 "get_dynamic_property", host,
                                                 "HostSystem", "datastore")

        if datastore_ret is None:
            raise exception.DatastoreNotFound()
        data_store_mors = datastore_ret.ManagedObjectReference
        data_stores = session._call_method(
            vim_util, "get_properties_for_a_collection_of_objects",
            "Datastore", data_store_mors, [
                "summary.type", "summary.name", "summary.capacity",
                "summary.freeSpace"
            ])

    for elem in data_stores:
        ds_name = None
        ds_type = None
        ds_cap = None
        ds_free = None
        for prop in elem.propSet:
            if prop.name == "summary.type":
                ds_type = prop.val
            elif prop.name == "summary.name":
                ds_name = prop.val
            elif prop.name == "summary.capacity":
                ds_cap = prop.val
            elif prop.name == "summary.freeSpace":
                ds_free = prop.val
        # Local storage identifier
        if ds_type == "VMFS" or ds_type == "NFS":
            data_store_name = ds_name
            return elem.obj, data_store_name, ds_cap, ds_free

    if data_store_name is None:
        raise exception.DatastoreNotFound()
Example #4
0
def get_datastore(session, cluster, datastore_regex=None,
                  storage_policy=None,
                  allowed_ds_types=ALL_SUPPORTED_DS_TYPES):
    """Get the datastore list and choose the most preferable one."""
    datastore_ret = session._call_method(
                                vim_util,
                                "get_dynamic_property", cluster,
                                "ClusterComputeResource", "datastore")
    # If there are no hosts in the cluster then an empty string is
    # returned
    if not datastore_ret:
        raise exception.DatastoreNotFound()

    data_store_mors = datastore_ret.ManagedObjectReference
    data_stores = session._call_method(vim_util,
                            "get_properties_for_a_collection_of_objects",
                            "Datastore", data_store_mors,
                            ["summary.type", "summary.name",
                             "summary.capacity", "summary.freeSpace",
                             "summary.accessible",
                             "summary.maintenanceMode"])

    best_match = None
    while data_stores:
        best_match = _select_datastore(session,
                                       data_stores,
                                       best_match,
                                       datastore_regex,
                                       storage_policy,
                                       allowed_ds_types)
        token = _get_token(data_stores)
        if not token:
            break
        data_stores = session._call_method(vim_util,
                                           "continue_to_get_objects",
                                           token)
    if best_match:
        return best_match

    if storage_policy:
        raise exception.DatastoreNotFound(
            _("Storage policy %s did not match any datastores")
            % storage_policy)
    elif datastore_regex:
        raise exception.DatastoreNotFound(
            _("Datastore regex %s did not match any datastores")
            % datastore_regex.pattern)
    else:
        raise exception.DatastoreNotFound()
Example #5
0
def get_datastore(session, cluster, datastore_regex=None,
                  storage_policy=None,
                  allowed_ds_types=ALL_SUPPORTED_DS_TYPES):
    """Get the datastore list and choose the most preferable one."""
    datastore_ret = session._call_method(vutil,
                                         "get_object_property",
                                         cluster,
                                         "datastore")
    # If there are no datastores in the cluster then an exception is
    # raised
    if not datastore_ret:
        raise exception.DatastoreNotFound()

    datastore_mors = datastore_ret.ManagedObjectReference
    result = session._call_method(vim_util,
                            "get_properties_for_a_collection_of_objects",
                            "Datastore", datastore_mors,
                            ["summary.type", "summary.name",
                             "summary.capacity", "summary.freeSpace",
                             "summary.accessible",
                             "summary.maintenanceMode"])

    best_match = None
    with vutil.WithRetrieval(session.vim, result) as datastores:
        best_match = _select_datastore(session,
                                       datastores,
                                       best_match,
                                       datastore_regex,
                                       storage_policy,
                                       allowed_ds_types)

    if best_match:
        return best_match

    if storage_policy:
        raise exception.DatastoreNotFound(
            _("Storage policy %s did not match any datastores")
            % storage_policy)
    elif datastore_regex:
        raise exception.DatastoreNotFound(
            _("Datastore regex %s did not match any datastores")
            % datastore_regex.pattern)
    else:
        raise exception.DatastoreNotFound()
Example #6
0
 def _check_if_tmp_folder_exists():
     # Copy the contents of the VM that were there just before the
     # snapshot was taken
     ds_ref_ret = vim_util.get_dynamic_property(
         self._session._get_vim(), vm_ref, "VirtualMachine",
         "datastore")
     if not ds_ref_ret:
         raise exception.DatastoreNotFound()
     ds_ref = ds_ref_ret.ManagedObjectReference[0]
     ds_browser = vim_util.get_dynamic_property(
         self._session._get_vim(), ds_ref, "Datastore", "browser")
     # Check if the vmware-tmp folder exists or not. If not, create one
     tmp_folder_path = vm_util.build_datastore_path(
         datastore_name, "vmware-tmp")
     if not self._path_exists(ds_browser, tmp_folder_path):
         self._mkdir(
             vm_util.build_datastore_path(datastore_name, "vmware-tmp"))