Ejemplo n.º 1
0
def remove_customers(customer_names, user_name=None, uri=None, method=None):
    """ Remove a customer from vFense
    Args:
        customer_names: Name of the customers you are removing.

    Kwargs:
        user_name (str): The name of the user who called this function.
        uri (str): The uri that was used to call this function.
        method (str): The HTTP methos that was used to call this function.

    Basic Usage:
        >>> from vFense.core.customer.customers remove_customers
        >>> customer_names = ['nyc', 'foo']
        >>> remove_customers(customer_names)

    Return:
        Dictionary of the status of the operation.
        {
            'rv_status_code': 1012,
            'message': 'None - remove_customer - vFense was deleted',
            'http_method': None,
            'uri': None,
            'http_status': 200

        }
    """
    status = remove_customer.func_name + ' - '
    status_code = 0
    generic_status_code = 0
    vfense_status_code = 0
    customers_deleted = []
    try:
        customers_are_valid = validate_customer_names(customer_names)
        users_exist = users_exists_in_customers(customer_names)
        default_in_list = DefaultCustomers.DEFAULT in customer_names
        if customers_are_valid[0] and not users_exist[0] and not default_in_list:
            status_code, count, errors, generated_ids = (
                delete_customers(customer_names)
            )
            if status_code == DbCodes.Deleted:
                msg = 'customers %s removed' % customer_names
                generic_status_code = GenericCodes.ObjectDeleted
                vfense_status_code = CustomerCodes.CustomerDeleted
                customers_deleted = customer_names
                ### Delete all the groups that belong to these customers
                ### And remove the users from these customers as well.
                for customer_name in customer_names:
                    delete_groups_from_customer(customer_name)
                    users = (
                        fetch_users_for_customer(
                            customer_name, CustomerPerUserKeys.UserName
                        )
                    )
                    if users:
                        users = (
                            map(
                                lambda user:
                                user[CustomerPerUserKeys.UserName], users
                            )
                        )
                        delete_users_in_customer(users, customer_name)


        elif users_exist[0] and not default_in_list:
            msg = (
                'users still exist for customer %s' % 
                (' and '.join(users_exist[1]))
            )
            status_code = DbCodes.Unchanged
            generic_status_code = GenericFailureCodes.FailedToDeleteObject
            vfense_status_code = CustomerFailureCodes.UsersExistForCustomer

        elif default_in_list:
            msg = 'Can not delete the default customer'
            status_code = DbCodes.Unchanged
            generic_status_code = GenericFailureCodes.FailedToDeleteObject
            vfense_status_code = CustomerFailureCodes.CantDeleteDefaultCustomer

        else:
            msg = (
                'customer %s does not exist' %
                (' and '.join(customers_are_valid[2]))
            )
            status_code = DbCodes.Skipped
            generic_status_code = GenericCodes.InvalidId
            vfense_status_code = CustomerFailureCodes.InvalidCustomerName

        results = {
            ApiResultKeys.DB_STATUS_CODE: status_code,
            ApiResultKeys.DELETED_IDS: customers_deleted,
            ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code,
            ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code,
            ApiResultKeys.MESSAGE: status + msg,
            ApiResultKeys.DATA: [],
            ApiResultKeys.USERNAME: user_name,
            ApiResultKeys.URI: uri,
            ApiResultKeys.HTTP_METHOD: method
        }

    except Exception as e:
        logger.exception(e)
        msg = 'Failed to remove customer %s: %s' % (customer_names, str(e))
        status_code = DbCodes.Errors
        generic_status_code = GenericFailureCodes.FailedToDeleteObject
        vfense_status_code = CustomerFailureCodes.FailedToRemoveCustomer

        results = {
            ApiResultKeys.DB_STATUS_CODE: status_code,
            ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code,
            ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code,
            ApiResultKeys.DELETED_IDS: customers_deleted,
            ApiResultKeys.MESSAGE: status + msg,
            ApiResultKeys.DATA: [],
            ApiResultKeys.USERNAME: user_name,
            ApiResultKeys.URI: uri,
            ApiResultKeys.HTTP_METHOD: method
        }

    return(results)
Ejemplo n.º 2
0
def remove_customers(customer_names, user_name=None, uri=None, method=None):
    """ Remove a customer from vFense
    Args:
        customer_names: Name of the customers you are removing.

    Kwargs:
        user_name (str): The name of the user who called this function.
        uri (str): The uri that was used to call this function.
        method (str): The HTTP methos that was used to call this function.

    Basic Usage:
        >>> from vFense.core.customer.customers remove_customers
        >>> customer_names = ['nyc', 'foo']
        >>> remove_customers(customer_names)

    Return:
        Dictionary of the status of the operation.
        {
            'rv_status_code': 1012,
            'message': 'None - remove_customer - vFense was deleted',
            'http_method': None,
            'uri': None,
            'http_status': 200

        }
    """
    status = remove_customer.func_name + ' - '
    status_code = 0
    generic_status_code = 0
    vfense_status_code = 0
    customers_deleted = []
    try:
        customers_are_valid = validate_customer_names(customer_names)
        users_exist = users_exists_in_customers(customer_names)
        default_in_list = DefaultCustomers.DEFAULT in customer_names
        if customers_are_valid[
                0] and not users_exist[0] and not default_in_list:
            status_code, count, errors, generated_ids = (
                delete_customers(customer_names))
            if status_code == DbCodes.Deleted:
                msg = 'customers %s removed' % customer_names
                generic_status_code = GenericCodes.ObjectDeleted
                vfense_status_code = CustomerCodes.CustomerDeleted
                customers_deleted = customer_names
                ### Delete all the groups that belong to these customers
                ### And remove the users from these customers as well.
                for customer_name in customer_names:
                    delete_groups_from_customer(customer_name)
                    users = (fetch_users_for_customer(
                        customer_name, CustomerPerUserKeys.UserName))
                    if users:
                        users = (map(
                            lambda user: user[CustomerPerUserKeys.UserName],
                            users))
                        delete_users_in_customer(users, customer_name)

        elif users_exist[0] and not default_in_list:
            msg = ('users still exist for customer %s' %
                   (' and '.join(users_exist[1])))
            status_code = DbCodes.Unchanged
            generic_status_code = GenericFailureCodes.FailedToDeleteObject
            vfense_status_code = CustomerFailureCodes.UsersExistForCustomer

        elif default_in_list:
            msg = 'Can not delete the default customer'
            status_code = DbCodes.Unchanged
            generic_status_code = GenericFailureCodes.FailedToDeleteObject
            vfense_status_code = CustomerFailureCodes.CantDeleteDefaultCustomer

        else:
            msg = ('customer %s does not exist' %
                   (' and '.join(customers_are_valid[2])))
            status_code = DbCodes.Skipped
            generic_status_code = GenericCodes.InvalidId
            vfense_status_code = CustomerFailureCodes.InvalidCustomerName

        results = {
            ApiResultKeys.DB_STATUS_CODE: status_code,
            ApiResultKeys.DELETED_IDS: customers_deleted,
            ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code,
            ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code,
            ApiResultKeys.MESSAGE: status + msg,
            ApiResultKeys.DATA: [],
            ApiResultKeys.USERNAME: user_name,
            ApiResultKeys.URI: uri,
            ApiResultKeys.HTTP_METHOD: method
        }

    except Exception as e:
        logger.exception(e)
        msg = 'Failed to remove customer %s: %s' % (customer_names, str(e))
        status_code = DbCodes.Errors
        generic_status_code = GenericFailureCodes.FailedToDeleteObject
        vfense_status_code = CustomerFailureCodes.FailedToRemoveCustomer

        results = {
            ApiResultKeys.DB_STATUS_CODE: status_code,
            ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code,
            ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code,
            ApiResultKeys.DELETED_IDS: customers_deleted,
            ApiResultKeys.MESSAGE: status + msg,
            ApiResultKeys.DATA: [],
            ApiResultKeys.USERNAME: user_name,
            ApiResultKeys.URI: uri,
            ApiResultKeys.HTTP_METHOD: method
        }

    return (results)
Ejemplo n.º 3
0
def remove_users_from_customer(
    usernames, customer_name,
    user_name=None, uri=None, method=None
    ):
    """Remove users from a customer
    Args:
        usernames (list): List of usernames, you are
            removing from the customer.
        customer_name (str): Name of the customer,
            you want to remove the users from

    Kwargs:
        user_name (str): The name of the user who called this function.
        uri (str): The uri that was used to call this function.
        method (str): The HTTP methos that was used to call this function.

    Basic Usage:
        >>> from vFense.customer.customers remove_users_from_customer
        >>> usernames = ['tester1', 'tester2']
        >>> customer_name = 'Tester'
        >>> remove_users_from_customer(usernames, customer_name)

    Return:
        Dictionary of the status of the operation.
    {
        'rv_status_code': 1004,
        'message': 'None - remove_users_from_customer - removed customers from user alien: TopPatch and vFense does not exist',
        'http_method': None,
        'uri': None,
        'http_status': 409
    }
    """
    status = remove_users_from_customer.func_name + ' - '
    admin_in_list = DefaultUsers.ADMIN in usernames
    try:
        if not admin_in_list:
            status_code, _, _, _ = (
                delete_users_in_customer(usernames, customer_name)
            )
            if status_code == DbCodes.Deleted:
                msg = (
                    'removed users %s from customer %s' %
                    (' and '.join(usernames), customer_name)
                )
                generic_status_code = GenericCodes.ObjectDeleted
                vfense_status_code = UserCodes.UsersRemovedFromCustomer

            elif status_code == DbCodes.Skipped:
                msg = 'invalid customer name or invalid username'
                generic_status_code = GenericCodes.InvalidId
                vfense_status_code = CustomerFailureCodes.InvalidCustomerName

            elif status_code == DbCodes.DoesNotExist:
                msg = 'customer name or username does not exist'
                generic_status_code = GenericCodes.DoesNotExist
                vfense_status_code = CustomerFailureCodes.UsersDoNotExistForCustomer

        else:
            msg = 'can not remove the admin user from any customer'
            status_code = DbCodes.Skipped
            generic_status_code = GenericCodes.InvalidId
            vfense_status_code = UserFailureCodes.CantDeleteAdminFromCustomer


        results = {
            ApiResultKeys.DB_STATUS_CODE: status_code,
            ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code,
            ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code,
            ApiResultKeys.MESSAGE: status + msg,
            ApiResultKeys.DATA: [],
            ApiResultKeys.USERNAME: user_name,
            ApiResultKeys.URI: uri,
            ApiResultKeys.HTTP_METHOD: method
        }

    except Exception as e:
        logger.exception(e)
        msg = (
            'Failed to remove users %s from customer %s: %s' % 
            (' and '.join(usernames), customer_name, str(e))
        )
        status_code = DbCodes.Errors
        generic_status_code = GenericFailureCodes.FailedToDeleteObject
        vfense_status_code = UserFailureCodes.FailedToRemoveUsersFromCustomer

        results = {
            ApiResultKeys.DB_STATUS_CODE: status_code,
            ApiResultKeys.GENERIC_STATUS_CODE: generic_status_code,
            ApiResultKeys.VFENSE_STATUS_CODE: vfense_status_code,
            ApiResultKeys.MESSAGE: status + msg,
            ApiResultKeys.DATA: [],
            ApiResultKeys.USERNAME: user_name,
            ApiResultKeys.URI: uri,
            ApiResultKeys.HTTP_METHOD: method
        }

    return(results)