Exemplo n.º 1
0
def get_tenant_list_from_db(name=None):
    """
        List all tenants or tenant with the name specified
        Params:
        -- name: if "name" is specified, return a list of one tenant with name specified
        if "name" is not specified, return a list of all tenants
        Return value:
        -- error_info: return None on success or error info on failure
        -- tenant_list: return a list of tenant objects on success or None on failure
    """
    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info, None

    if not name:
        error_msg, tenant_list = auth_mgr.list_tenants()
        if error_msg:
            error_info = generate_error_info(ErrorCode.INTERNAL_ERROR,
                                             error_msg)
    else:
        error_msg, tenant = auth_mgr.get_tenant(name)
        if error_msg:
            error_info = generate_error_info(ErrorCode.INTERNAL_ERROR,
                                             error_msg)
        if error_msg or not tenant:
            tenant_list = []
        else:
            tenant_list = [tenant]

    return error_info, tenant_list
def _tenant_rm(name, remove_volumes=False):
    """ API to remove a tenant """
    logging.debug("_tenant_rm: name=%s remove_volumes=%s", name, remove_volumes)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info

    # check if vms that are a part of this tenant have any volumes mounted.
    # If they have, can't delete the tenant.
    if tenant.vms:
        logging.info("_tenant_rm. VMs in tenant are %s", tenant.vms)

        error_info = vmdk_utils.check_volumes_mounted(tenant.vms)
        if error_info:
            error_info.msg = "Cannot complete vmgroup rm. " + error_info.msg
            logging.error(error_info.msg)
            return error_info

    error_info, auth_mgr = get_auth_mgr_object()

    if error_info:
        return error_info

    error_msg = auth_mgr.remove_tenant(tenant.id, remove_volumes)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    return error_info
Exemplo n.º 3
0
def get_default_datastore_url(name):
    """
        Get default_datastore url for given tenant
        Return value:
        --- error_info: return None on success or error info on failure
        --- default_datastore: return name of default_datastore on success or None on failure
    """
    logging.debug(
        "auth_api.get_default_datastore_url: for tenant with name=%s", name)

    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info, None

    if auth_mgr.allow_all_access():
        if name == auth_data_const.DEFAULT_TENANT:
            return None, auth_data_const.VM_DS_URL
        else:
            return generate_error_info(ErrorCode.INIT_NEEDED), None

    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info, None

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info, None

    # if default_datastore is not set for this tenant, default_datastore will be None
    error_msg, default_datastore_url = tenant.get_default_datastore(
        auth_mgr.conn)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    logging.debug("returning url %s", default_datastore_url)
    return error_info, default_datastore_url
Exemplo n.º 4
0
def _tenant_rm(name, remove_volumes=False):
    """ API to remove a tenant """
    logging.debug("_tenant_rm: name=%s remove_volumes=%s", name,
                  remove_volumes)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info

    if tenant.vms:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EMPTY, name)
        logging.error(error_info.msg)
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    error_msg = auth_mgr.remove_tenant(tenant.id, remove_volumes)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    return error_info
def get_default_datastore_url(name):
    """
        Get default_datastore url for given tenant
        Return value:
        --- error_info: return None on success or error info on failure
        --- default_datastore: return name of default_datastore on success or None on failure
    """
    logging.debug("get_default_datastore: for tenant with name=%s", name)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info, None

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info, None

    error_info, auth_mgr = get_auth_mgr_object()

    if error_info:
        return error_info, None

    # if default_datastore is not set for this tenant, default_datastore will be None
    error_msg, default_datastore_url = tenant.get_default_datastore(auth_mgr.conn)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    return error_info, default_datastore_url
Exemplo n.º 6
0
def create_tenant_in_db(name, description, vms, privileges):
    """
        Create a tenant object in DB
        Return value:
        -- error_info: return None on success or error info on failure
        -- tenant: return tenant object on success or None on failure
    """
    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info, None

    error_msg, exist_tenant = auth_mgr.get_tenant(name)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR,
                                                    error_msg)
        return error_info, None

    if exist_tenant:
        error_info = error_code.generate_error_info(
            ErrorCode.TENANT_ALREADY_EXIST, name)
        return error_info, None

    error_msg, tenant = auth_mgr.create_tenant(name=name,
                                               description=description,
                                               vms=vms,
                                               privileges=privileges)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR,
                                                    error_msg)

    return error_info, tenant
Exemplo n.º 7
0
def _tenant_create(name, default_datastore, description="", vm_list=None, privileges=None):
    """ API to create a tenant . Returns (ErrInfo, Tenant) """
    logging.debug("_tenant_create: name=%s description=%s vm_list=%s privileges=%s default_ds=%s",
                  name, description, vm_list, privileges, default_datastore)

    if not is_tenant_name_valid(name):
        error_info = generate_error_info(ErrorCode.TENANT_NAME_INVALID, name, VALID_TENANT_NAME_REGEXP)
        return error_info, None

    # if param "description" is not set by caller, the default value is empty string
    if not description:
        description = ""

    # VM list can be empty during tenant create. Validate only if it exists
    vms = None
    if vm_list:
        if name == auth_data_const.DEFAULT_TENANT:
            error_info = generate_error_info(ErrorCode.INVALID_ARGUMENT, vm_list)
            return error_info, None

        error_info = is_vm_duplicate(vm_list)
        if error_info:
            return error_info, None

        error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)
        if error_msg:
            not_found_vm_list = ",".join(not_found_vms)
            error_info = generate_error_info(ErrorCode.VM_NOT_FOUND, not_found_vm_list)
            return error_info, None

        error_info = vm_in_any_tenant(vms)
        if error_info:
            return error_info, None

        error_info = vmdk_utils.check_volumes_mounted(vms)
        if error_info:
            error_info.msg = "Cannot add VM to vmgroup " + error_info.msg
            logging.error(error_info.msg)
            return error_info, None

        logging.debug("_tenant_create: vms=%s", vms)

    error_info = check_default_datastore(default_datastore)
    if error_info:
        return error_info, None

    error_info, tenant = create_tenant_in_db(
        name=name,
        description=description,
        vms=vms,
        privileges=privileges)
    if error_info:
        return error_info, None

    error_info = set_default_ds(tenant=tenant,
                                default_datastore=default_datastore,
                                check_existing=False)
    if error_info:
        return error_info, None
    return None, tenant
Exemplo n.º 8
0
def _tenant_vm_replace(name, vm_list):
    """ API to replace vms for a tenant """
    logging.debug("_tenant_vm_replace: name=%s vm_list=%s", name, vm_list)
    if not vm_list:
        error_info = error_code.generate_error_info(ErrorCode.REPLACE_VM_EMPTY)
        return error_info

    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST,
                                                    name)
        return error_info

    error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)

    if error_msg:
        not_found_vm_list = ",".join(not_found_vms)
        error_info = error_code.generate_error_info(ErrorCode.VM_NOT_FOUND,
                                                    not_found_vm_list)
        return error_info

    logging.debug("_tenant_vm_replace: vms=%s", vms)
    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    vms_uuid_list = [(vm_id) for (vm_id, vm_name) in vms]
    error_msg = tenant.replace_vms(auth_mgr.conn, vms_uuid_list)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR,
                                                    error_msg)
    return error_info
Exemplo n.º 9
0
def _tenant_create(name, description="", vm_list=None, privileges=None):
    """ API to create a tenant """
    logging.debug(
        "_tenant_create: name=%s description=%s vm_list=%s privileges=%s",
        name, description, vm_list, privileges)
    if not is_tenant_name_valid(name):
        error_info = error_code.generate_error_info(
            ErrorCode.TENANT_NAME_INVALID, name, VALID_TENANT_NAME_REGEXP)
        return error_info, None

    error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)
    if error_msg:
        not_found_vm_list = ",".join(not_found_vms)
        error_info = error_code.generate_error_info(ErrorCode.VM_NOT_FOUND,
                                                    not_found_vm_list)
        return error_info, None

    # if param "description" is not set by caller, the default value is empty string
    if not description:
        description = ""

    logging.debug("_tenant_create: vms=%s", vms)
    vms_uuid_list = [(vm_id) for (vm_id, vm_name) in vms]
    error_info, tenant = create_tenant_in_db(name=name,
                                             description=description,
                                             vms=vms_uuid_list,
                                             privileges=privileges)
    if error_info:
        return error_info, None

    return None, tenant
Exemplo n.º 10
0
def set_default_ds(tenant, default_datastore, check_existing):
    """
        Set "default_datastore" for given tenant and create a full access privilege
        to "default_datastore" if entry does not exist
        Need to check whether the default_datastore to be set is the same as exiting
        default_datastore when @Param check_existing is set to True
    """
    # @Param tenant is a DockerVolumeTenant object
    logging.debug(
        "set_default_ds: tenant_name=%s default_datastore=%s check_existing=%d",
        tenant.name, default_datastore, check_existing)

    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    datastore_url = vmdk_utils.get_datastore_url(default_datastore)
    # datastore_url will be set to "None" by "vmdk_utils.get_datastore_url" is "default_datastore"
    # is not a valid datastore
    if datastore_url is None:
        error_info = generate_error_info(ErrorCode.DS_DEFAULT_NAME_INVALID,
                                         default_datastore)
        return error_info

    existing_default_ds_url = None
    if check_existing:
        error_msg, existing_default_ds_url = tenant.get_default_datastore(
            auth_mgr.conn)
        if error_msg:
            error_info = generate_error_info(ErrorCode.INTERNAL_ERROR,
                                             error_msg)
            return error_info

        # the "default_datastore" to be set is the same as existing "default_datastore" for this tenant
        if datastore_url == existing_default_ds_url:
            return None

    error_msg = tenant.set_default_datastore(auth_mgr.conn, datastore_url)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
        return error_info
    existing_default_ds = vmdk_utils.get_datastore_name(
        existing_default_ds_url
    ) if existing_default_ds_url is not None else None
    logging.info(
        "Existing default_datastore %s is being changed to %s for tenant %s",
        existing_default_ds, default_datastore, tenant)

    # create full access privilege to default_datastore
    error_info = _tenant_access_add(name=tenant.name,
                                    datastore=default_datastore,
                                    allow_create=True)
    # privilege to default_datastore already exist, no need to create
    if error_info and error_info.code == ErrorCode.PRIVILEGE_ALREADY_EXIST:
        logging.info(error_info.msg +
                     " not overwriting the existing access privilege")
        error_info = None

    return error_info
Exemplo n.º 11
0
def _tenant_update(name,
                   new_name=None,
                   description=None,
                   default_datastore=None):
    """ API to update a tenant """
    logging.debug(
        "_tenant_update: name=%s, new_name=%s, descrption=%s, default_datastore=%s",
        name, new_name, description, default_datastore)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    if default_datastore:
        error_info = check_default_datastore(default_datastore)
        if error_info:
            return error_info
        error_info = set_default_ds(tenant=tenant,
                                    default_datastore=default_datastore,
                                    check_existing=True)
        if error_info:
            return error_info

    if new_name:
        if name == auth_data_const.DEFAULT_TENANT:
            error_info = generate_error_info(ErrorCode.TENANT_NAME_INVALID,
                                             name, VALID_TENANT_NAMES)
            return error_info

        # check whether tenant with new_name already exist or not
        error_info = check_tenant_exist(new_name)
        if error_info:
            return error_info

        if not is_tenant_name_valid(name):
            error_info = generate_error_info(ErrorCode.TENANT_NAME_INVALID,
                                             name, VALID_TENANT_NAME_REGEXP)
            return error_info

        error_msg = tenant.set_name(auth_mgr.conn, name, new_name)
        if error_msg:
            error_info = generate_error_info(ErrorCode.INTERNAL_ERROR,
                                             error_msg)
            return error_info
    if description:
        error_msg = tenant.set_description(auth_mgr.conn, description)
        if error_msg:
            error_info = generate_error_info(ErrorCode.INTERNAL_ERROR,
                                             error_msg)
            return error_info

    return None
Exemplo n.º 12
0
def _tenant_vm_replace(name, vm_list):
    """ API to replace vms for a tenant """
    logging.debug("_tenant_vm_replace: name=%s vm_list=%s", name, vm_list)

    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info

    if not vm_list:
        error_info = generate_error_info(ErrorCode.REPLACE_VM_EMPTY)
        return error_info

    error_info = is_vm_duplicate(vm_list)
    if error_info:
        return error_info

    error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)

    if error_msg:
        not_found_vm_list = ",".join(not_found_vms)
        error_info = generate_error_info(ErrorCode.VM_NOT_FOUND,
                                         not_found_vm_list)
        return error_info

    error_info = vm_already_in_tenant(name, vms)
    if error_info:
        return error_info

    error_info = vm_in_any_tenant(vms)
    if error_info:
        return error_info

    # check if vms that would be replaced out have any volumes mounted
    error_info, existing_vms = _tenant_vm_ls(name)
    if error_info:
        return error_info

    error_info = vmdk_utils.check_volumes_mounted(existing_vms)

    if error_info:
        error_info.msg = "Cannot complete vmgroup vm replace. " + error_info.msg
        logging.error(error_info.msg)
        return error_info

    logging.debug("_tenant_vm_replace: vms=%s", vms)
    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    error_msg = tenant.replace_vms(auth_mgr.conn, vms)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    return error_info
Exemplo n.º 13
0
def _tenant_access_rm(name, datastore):
    """ API to remove datastore access for a tenant """
    logging.debug("_tenant_access_rm: name=%s datastore=%s", name, datastore)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST,
                                                    name)
        return error_info

    error_info = check_datastore(datastore)
    if error_info:
        return error_info

    error_info, existing_privileges = _tenant_access_ls(name)
    if error_info:
        return error_info

    if not privilege_exist(existing_privileges, datastore):
        error_info = error_code.generate_error_info(
            ErrorCode.PRIVILEGE_NOT_FOUND, name, datastore)
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()

    if error_info:
        return error_info

    if datastore == auth.DEFAULT_DS:
        datastore_url = auth.DEFAULT_DS_URL
    else:
        datastore_url = vmdk_utils.get_datastore_url(datastore)
    logging.debug("_tenant_access_rm: datastore_url=%s", datastore_url)
    error_msg = tenant.remove_datastore_access_privileges(
        auth_mgr.conn, datastore_url)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR,
                                                    error_msg)
        return error_info

    # get dafault_datastore, if default_datastore is the same as param "datastore"
    # need to set default_datastore_url to "" in tenants table
    error_info, default_datastore = get_default_datastore(name)
    if error_info:
        return error_info

    if default_datastore == datastore:
        error_msg = tenant.set_default_datastore(auth_mgr.conn, "")
        if error_msg:
            error_info = error_code.generate_error_info(
                ErrorCode.INTERNAL_ERROR, error_msg)

    return error_info
Exemplo n.º 14
0
def _tenant_update(name,
                   new_name=None,
                   description=None,
                   default_datastore=None):
    """ API to update a tenant """
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST,
                                                    name)
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()

    if error_info:
        return error_info

    if new_name:
        # check whether tenant with new_name already exist or not
        error_info = check_tenant_exist(new_name)
        if error_info:
            return error_info, None

        if not is_tenant_name_valid(name):
            error_info = error_code.generate_error_info(
                ErrorCode.TENANT_NAME_INVALID, name, VALID_TENANT_NAME_REGEXP)
            return error_info, None

        error_msg = tenant.set_name(auth_mgr.conn, name, new_name)
        if error_msg:
            error_info = error_code.generate_error_info(
                ErrorCode.INTERNAL_ERROR, error_msg)
            return error_info
    if description:
        error_msg = tenant.set_description(auth_mgr.conn, description)
        if error_msg:
            error_info = error_code.generate_error_info(
                ErrorCode.INTERNAL_ERROR, error_msg)
            return error_info
    if default_datastore:
        error_info = check_datastore(default_datastore)
        if error_info:
            return error_info

        datastore_url = vmdk_utils.get_datastore_url(default_datastore)
        error_msg = tenant.set_default_datastore(auth_mgr.conn, datastore_url)
        if error_msg:
            error_info = error_code.generate_error_info(
                ErrorCode.INTERNAL_ERROR, error_msg)
            return error_info

    return None
Exemplo n.º 15
0
def _tenant_vm_add(name, vm_list):
    """ API to add vms for a tenant """
    logging.debug("_tenant_vm_add: name=%s vm_list=%s", name, vm_list)

    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info

    if not vm_list:
        error_info = generate_error_info(ErrorCode.VM_LIST_EMPTY)
        return error_info

    error_info = is_vm_duplicate(vm_list)
    if error_info:
        return error_info

    error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)
    if error_msg:
        not_found_vm_list = ",".join(not_found_vms)
        error_info = generate_error_info(ErrorCode.VM_NOT_FOUND,
                                         not_found_vm_list)
        return error_info

    error_info = vm_already_in_tenant(name, vms)
    if error_info:
        return error_info

    error_info = vm_in_any_tenant(vms)
    if error_info:
        return error_info

    error_info = vmdk_utils.check_volumes_mounted(vms)
    if error_info:
        error_info.msg = "Cannot add VM to vmgroup " + error_info.msg
        logging.error(error_info.msg)
        return error_info, None

    error_info, auth_mgr = get_auth_mgr_object()

    if error_info:
        return error_info

    logging.debug("_tenant_vm_add: vms=%s", vms)
    error_msg = tenant.add_vms(auth_mgr.conn, vms)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    return error_info
Exemplo n.º 16
0
def _tenant_access_rm(name, datastore):
    """ API to remove datastore access for a tenant """
    logging.debug("_tenant_access_rm: name=%s datastore=%s", name, datastore)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info

    error_info = check_datastore(datastore)
    if error_info:
        return error_info

    datastore_url = vmdk_utils.get_datastore_url(datastore)

    error_info, existing_privileges = _tenant_access_ls(name)
    if error_info:
        return error_info

    if not privilege_exist(existing_privileges, datastore_url):
        error_info = generate_error_info(ErrorCode.PRIVILEGE_NOT_FOUND, name,
                                         datastore)
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    # get dafault_datastore for this tenant
    # if the default_datastore is equal to param "datastore", which means
    # we are trying to remove a row in "privilege" table with datastore which is
    # marked as default_datastore of this tenant, should return with error
    error_info, default_datastore_url = get_default_datastore_url(name)
    if error_info:
        return error_info

    if default_datastore_url == datastore_url:
        error_info = generate_error_info(
            ErrorCode.PRIVILEGE_REMOVE_NOT_ALLOWED)
        return error_info

    logging.debug("_tenant_access_rm: datastore_url=%s", datastore_url)
    error_msg = tenant.remove_datastore_access_privileges(
        auth_mgr.conn, datastore_url)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
        return error_info

    return None
Exemplo n.º 17
0
def _tenant_vm_rm(name, vm_list):
    """ API to remove vms for a tenant """
    logging.debug("_tenant_vm_rm: name=%s vm_list=%s", name, vm_list)

    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info

    if not vm_list:
        error_info = error_code.generate_error_info(ErrorCode.VM_LIST_EMPTY)
        return error_info

    error_info = is_vm_duplicate(vm_list)
    if error_info:
        return error_info

    error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)
    if error_msg:
        not_found_vm_list = ",".join(not_found_vms)
        error_info = error_code.generate_error_info(ErrorCode.VM_NOT_FOUND, not_found_vm_list)
        return error_info

    vms_uuid_list = [(vm_id) for (vm_id, vm_name) in vms]

    # check if vms to be removed have any volumes mounted.
    error_info = vmdk_utils.check_volumes_mounted(vms_uuid_list)

    if error_info:
        error_info.msg = "Cannot complete vmgroup vm rm. " + error_info.msg
        logging.error(error_info.msg)
        return error_info

    logging.debug("_tenant_vm_rm: vms=%s", vms)

    error_info = vm_not_exist(name, vms)
    if error_info:
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    error_msg = tenant.remove_vms(auth_mgr.conn, vms_uuid_list)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    return error_info
Exemplo n.º 18
0
def _tenant_vm_add(name, vm_list):
    """ API to add vms for a tenant """
    logging.debug("_tenant_vm_add: name=%s vm_list=%s", name, vm_list)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST,
                                                    name)
        return error_info

    if not vm_list:
        error_info = error_code.generate_error_info(ErrorCode.VM_LIST_EMPTY)
        return error_info

    error_info = is_vm_duplicate(vm_list)
    if error_info:
        return error_info

    error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)
    if error_msg:
        not_found_vm_list = ",".join(not_found_vms)
        error_info = error_code.generate_error_info(ErrorCode.VM_NOT_FOUND,
                                                    not_found_vm_list)
        return error_info

    error_info = vm_already_in_tenant(name, vms)
    if error_info:
        return error_info

    error_info = vm_in_any_tenant(vms)
    if error_info:
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()

    if error_info:
        return error_info

    logging.debug("_tenant_vm_add: vms=%s", vms)
    vms_uuid_list = [(vm_id) for (vm_id, vm_name) in vms]
    error_msg = tenant.add_vms(auth_mgr.conn, vms_uuid_list)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR,
                                                    error_msg)
    return error_info
Exemplo n.º 19
0
def check_tenant_exist(name):
    """ Check tenant with @param name exist or not
        Return value:
        -- Return None if tenant with given name does not exist
        -- Return error_info on failure or the tenant with given name exists
    """
    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    error_msg, exist_tenant = auth_mgr.get_tenant(name)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
        return error_info

    if exist_tenant:
        error_info = generate_error_info(ErrorCode.TENANT_ALREADY_EXIST, name)
        return error_info
def check_volumes_mounted(vm_list):
    """
    Return error_info if any vm in @param vm_list have docker volume mounted
    """
    for vm_id in vm_list:
        vm = vmdk_ops.findVmByUuid(vm_id)
        if vm:
            for d in vm.config.hardware.device:
                if check_docker_volume(d):
                    error_info = error_code.generate_error_info(
                        error_code.ErrorCode.VM_WITH_MOUNTED_VOLUMES,
                        vm.config.name)
                    return error_info
        else:
            error_info = error_code.generate_error_info(
                error_code.ErrorCode.VM_NOT_FOUND, vm_id)
            return error_info
    return None
Exemplo n.º 21
0
def get_auth_mgr_object():
    """ Get a auth_mgr object which needed to connect to auth DB. """
    # auth.get_auth_mgr will not throw an Exception
    # it will return err_msg when it fails
    err_msg, auth_mgr = auth.get_auth_mgr()
    if err_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR, err_msg)
        return error_info, None
    return None, auth_mgr
Exemplo n.º 22
0
def check_default_datastore(datastore_name):
    """
        Check datastore with given name is a valid value for default_datastore
        Returns None for success and err message for errors
    """
    # The valid default_datastore name are:
    # named datastore existing on the host
    # hard coded datastore name "_VM_DS"
    # "_ALL_DS" is not a valid value to set as "default_datastore"
    if datastore_name == auth_data_const.VM_DS:
        return None
    if datastore_name == auth_data_const.ALL_DS:
        return generate_error_info(ErrorCode.DS_DEFAULT_CANNOT_USE_ALL_DS)

    if not vmdk_utils.validate_datastore(datastore_name):
        error_info = generate_error_info(ErrorCode.DS_NOT_EXIST, datastore_name)
        return error_info

    return None
Exemplo n.º 23
0
def check_datastore(datastore_name):
    """ Check datastore with given name is a valid datastore or not """
    if datastore_name == auth_data_const.DEFAULT_DS:
        return None

    if not vmdk_utils.validate_datastore(datastore_name):
        error_info = error_code.generate_error_info(ErrorCode.DS_NOT_EXIST, datastore_name)
        return error_info

    return None
Exemplo n.º 24
0
def is_vm_duplicate(vm_list):
    """
    Check if vm names in vm_list contain duplicates
    """

    if len(vm_list) != len(set(vm_list)):
        error_info = error_code.generate_error_info(ErrorCode.VM_DUPLICATE, vm_list)
        logging.error(error_info.msg)
        return error_info

    return None
Exemplo n.º 25
0
def _tenant_vm_ls(name):
    """ API to get vms for a tenant """
    logging.debug("_tenant_vm_ls: name=%s", name)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info, None

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info, None
    # tenant.vms is a list of vm_uuid of vms which belong to this tenant
    return None, tenant.vms
Exemplo n.º 26
0
def _tenant_access_ls(name):
    """ Handle tenant access ls command. Returns (ErrInfo, [list of privileges]) """
    logging.debug("_tenant_access_ls: name=%s", name)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info, None

    if not tenant:
        error_info = generate_error_info(ErrorCode.TENANT_NOT_EXIST, name)
        return error_info, None

    return None, tenant.privileges
Exemplo n.º 27
0
def _tenant_vm_rm(name, vm_list):
    """ API to remove vms for a tenant """
    logging.debug("_tenant_vm_rm: name=%s vm_list=%s", name, vm_list)
    error_info, tenant = get_tenant_from_db(name)
    if error_info:
        return error_info

    if not tenant:
        error_info = error_code.generate_error_info(ErrorCode.TENANT_NOT_EXIST,
                                                    name)
        return error_info

    error_msg, vms, not_found_vms = generate_tuple_from_vm_list(vm_list)
    if error_msg:
        not_found_vm_list = ",".join(not_found_vms)
        error_info = error_code.generate_error_info(ErrorCode.VM_NOT_FOUND,
                                                    not_found_vm_list)
        return error_info

    logging.debug("_tenant_vm_rm: vms=%s", vms)

    error_info, existing_vms = _tenant_vm_ls(name)
    if error_info:
        return error_info

    if any_vm_not_exist(existing_vms, vms):
        error_info = error_code.generate_error_info(ErrorCode.VM_NOT_IN_TENANT,
                                                    vm_list, name)
        return error_info

    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info

    vms_uuid_list = [(vm_id) for (vm_id, vm_name) in vms]
    error_msg = tenant.remove_vms(auth_mgr.conn, vms_uuid_list)
    if error_msg:
        error_info = error_code.generate_error_info(ErrorCode.INTERNAL_ERROR,
                                                    error_msg)
    return error_info
Exemplo n.º 28
0
def check_usage_quota(datastore, volume_totalsize_in_MB):
    """
        Check if the requested quota is valid in the given datastore
        Return None if the usage_quota is valid
        Return error_info if the usage_quota is invalid
    """
    # usage_quota on "_VM_DS" and "_ALL_DS" should be "Unset"
    if datastore == auth_data_const.VM_DS or datastore == auth_data_const.ALL_DS:
        if volume_totalsize_in_MB is not None:
            error_info = generate_error_info(
                ErrorCode.PRIVILEGE_SET_TOTAL_VOLUME_SIZE_LIMIT_NOT_ALLOWED,
                datastore)
            return error_info
Exemplo n.º 29
0
def vm_not_exist(name, vms):
    """
        Check whether any vm in @param "vms" does not exist in tenant @param "name"
    """
    error_info, existing_vms = _tenant_vm_ls(name)
    if error_info:
        return error_info

    for vm_id, vm_name in vms:
        if not vm_id in existing_vms:
            error_info = error_code.generate_error_info(ErrorCode.VM_NOT_IN_TENANT, vm_name, name)
            logging.error(error_info.msg)
            return error_info

    return None
Exemplo n.º 30
0
def get_tenant_name(tenant_uuid):
    """
        Get tenant name with given tenant_uuid
        Return value:
        -- error_info: return None on success or error info on failure
        -- tenant_name: return tenant name on success or None on failure
    """
    error_info, auth_mgr = get_auth_mgr_object()
    if error_info:
        return error_info, None

    error_msg, tenant_name = auth_mgr.get_tenant_name(tenant_uuid)
    if error_msg:
        error_info = generate_error_info(ErrorCode.INTERNAL_ERROR, error_msg)
    return error_info, tenant_name
Exemplo n.º 31
0
def create_vm(si, vm_name, datastore_name):
    """ Create a VM """
    content = si.RetrieveContent()
    datacenter = content.rootFolder.childEntity[0]
    vm_folder = datacenter.vmFolder
    hosts = datacenter.hostFolder.childEntity
    resource_pool = hosts[0].resourcePool
    logging.info("datacenter={0} vm_folder={1} hosts={2} resource_pool={3}".format(datacenter, vm_folder,
                                                                                   hosts, resource_pool))
    # bare minimum VM shell, no disks. Feel free to edit
    vmx_file = vim.vm.FileInfo(logDirectory=None,
                               snapshotDirectory=None,
                               suspendDirectory=None,
                               vmPathName='[' + datastore_name + '] ')

    config = vim.vm.ConfigSpec(
        name=vm_name,
        memoryMB=128,
        numCPUs=1,
        files=vmx_file,
        guestId='rhel5_64Guest',
        version='vmx-11'
    )

    task = vm_folder.CreateVM_Task(config=config, pool=resource_pool)
    vmdk_ops.wait_for_tasks(si, [task])

    logging.info("create_vm: vm_name=%s, datastore_name=%s", vm_name, datastore_name)
    vm = task.info.result
    if vm:
        logging.info("Found: VM %s", vm_name)
        if vm.runtime.powerState == vim.VirtualMachinePowerState.poweredOff:
            logging.info("Attempting to power on %s", vm_name)
            task = vm.PowerOnVM_Task()
            vmdk_ops.wait_for_tasks(si, [task])
    else:
        error_info = error_code.generate_error_info(error_code.ErrorCode.VM_NOT_FOUND, vm_name)
        logging.error("Cannot find vm %s", vm_name)
        return error_info, None

    return None, vm