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
Example #2
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
Example #3
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
Example #4
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
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