Esempio n. 1
0
def get_mock_customer_logic():
    global metadata_logic_mock
    metadata_logic_mock = mock.MagicMock()

    if metadata.logic.return_error == 0:
        res = Models.CustomerResultWrapper(transaction_id='1',
                                           id='1',
                                           links={},
                                           updated=None,
                                           created='1')

        metadata_logic_mock.add_customer_metadata.return_value = res
        metadata_logic_mock.update_customer_metadata.return_value = res

    elif metadata.logic.return_error == 1:
        metadata_logic_mock.add_customer_metadata.side_effect = SystemError()
        metadata_logic_mock.update_customer_metadata.side_effect = SystemError()

    elif metadata.logic.return_error == 2:
        metadata_logic_mock.add_customer_metadata.side_effect = ErrorStatus(status_code=404)
        metadata_logic_mock.update_customer_metadata.side_effect = ErrorStatus(status_code=404)

    elif metadata.logic.return_error == 3:
        metadata_logic_mock.add_customer_metadata.side_effect = LookupError()
        metadata_logic_mock.update_customer_metadata.side_effect = LookupError()

    elif metadata.logic.return_error == 4:
        metadata_logic_mock.add_customer_metadata.side_effect = AttributeError()
        metadata_logic_mock.update_customer_metadata.side_effect = AttributeError()

    elif metadata.logic.return_error == 5:
        metadata_logic_mock.add_customer_metadata.side_effect = ValueError()
        metadata_logic_mock.update_customer_metadata.side_effect = ValueError()

    return metadata_logic_mock
Esempio n. 2
0
    def replace_users(self, customer_uuid, region_name, users, transaction_id):
        datamanager = None
        try:
            datamanager = DataManager()
            datamanager.begin_transaction()

            customer_id = datamanager.get_customer_id_by_uuid(customer_uuid)
            if customer_id is None:
                raise ErrorStatus(
                    404, "customer {} does not exist".format(customer_uuid))

            region_id = datamanager.get_region_id_by_name(region_name)
            if region_id is None:
                raise ErrorStatus(404,
                                  "region {} not found".format(region_name))

            # delete older default user
            user_role_record = datamanager.get_record('user_role')
            user_role_record.delete_all_users_from_region(
                customer_uuid, region_name)  # -1 is default region
            result = self.add_users(customer_uuid, region_name, users,
                                    transaction_id, datamanager)
            datamanager.commit()
            return result

        except Exception as exception:
            datamanager.rollback()
            LOG.log_exception("Failed to replace_default_users", exception)
            raise
Esempio n. 3
0
def get_mock_customer_logic():
    global customer_logic_mock
    customer_logic_mock = mock.MagicMock()

    if root.CustomerLogic.return_error == 0:
        res = Models.CustomerResultWrapper(transaction_id='1',
                                           id='1',
                                           links={},
                                           updated=None,
                                           created='1')

        list_res = Models.CustomerSummaryResponse()
        list_res.customers.append(Models.CustomerSummary(name='1', id='1', description='1'))

        customer_logic_mock.create_customer.return_value = res
        customer_logic_mock.update_customer.return_value = res
        customer_logic_mock.get_customer.return_value = Models.Customer(**RET_CUSTOMER_JSON)
        customer_logic_mock.get_customer_list_by_criteria.return_value = list_res

    elif root.CustomerLogic.return_error == 1:
        customer_logic_mock.create_customer.side_effect = SystemError()
        customer_logic_mock.update_customer.side_effect = SystemError()
        customer_logic_mock.get_customer.side_effect = SystemError()
        customer_logic_mock.delete_customer_by_uuid.side_effect = SystemError()
        customer_logic_mock.get_customer_list_by_criteria.side_effect = SystemError()

    else:
        customer_logic_mock.create_customer.side_effect = ErrorStatus(status_code=404)
        customer_logic_mock.update_customer.side_effect = ErrorStatus(status_code=404)
        customer_logic_mock.get_customer.side_effect = ErrorStatus(status_code=404)
        customer_logic_mock.delete_customer_by_uuid.side_effect = ErrorStatus(
            status_code=409)
        customer_logic_mock.get_customer_list_by_criteria.side_effect = ErrorStatus(status_code=404)

    return customer_logic_mock
Esempio n. 4
0
    def add_default_users(self,
                          customer_uuid,
                          users,
                          transaction_id,
                          p_datamanager=None):
        datamanager = None
        try:
            if p_datamanager is None:
                datamanager = DataManager()
                datamanager.begin_transaction()
            else:
                datamanager = p_datamanager

            customer_id = datamanager.get_customer_id_by_uuid(customer_uuid)

            if customer_id is None:
                raise ErrorStatus(
                    404, "customer {} does not exist".format(customer_uuid))

            self.add_users_to_db(datamanager,
                                 customer_id,
                                 -1,
                                 users,
                                 adding=True)

            customer_record = datamanager.get_record('customer')
            customer = customer_record.read_customer(customer_id)

            timestamp = utils.get_time_human()
            datamanager.flush(
            )  # i want to get any exception created by this insert
            if len(customer.customer_customer_regions) > 1:
                RdsProxy.send_customer(customer, transaction_id, "PUT")

            if p_datamanager is None:
                datamanager.commit()

            base_link = '{0}{1}/'.format(conf.server.host_ip,
                                         pecan.request.path)

            result_users = [{
                'id': user.id,
                'added': timestamp,
                'links': {
                    'self': base_link + user.id
                }
            } for user in users]
            user_result_wrapper = UserResultWrapper(
                transaction_id=transaction_id, users=result_users)

            return user_result_wrapper

        except Exception as exception:
            datamanager.rollback()
            if 'Duplicate' in exception.message:
                raise ErrorStatus(409, exception.message)
            LOG.log_exception("Failed to add_default_users", exception)
            raise
Esempio n. 5
0
    def replace_regions(self, customer_uuid, regions, transaction_id):
        datamanager = DataManager()
        customer_record = datamanager.get_record('customer')
        customer_region = datamanager.get_record('customer_region')
        try:
            customer_id = datamanager.get_customer_id_by_uuid(customer_uuid)
            if customer_id is None:
                raise ErrorStatus(
                    404,
                    "customer with id {} does not exist".format(customer_uuid))

            old_sql_customer = customer_record.read_customer_by_uuid(
                customer_uuid)
            if old_sql_customer is None:
                raise ErrorStatus(
                    404,
                    "customer with id {} does not exist".format(customer_id))
            old_customer_dict = old_sql_customer.get_proxy_dict()
            datamanager.session.expire(old_sql_customer)

            customer_region.delete_all_regions_for_customer(customer_id)

            self.add_regions_to_db(regions, customer_id, datamanager)
            timestamp = utils.get_time_human()

            new_sql_customer = datamanager.get_cusomer_by_id(customer_id)

            new_sql_customer = self.add_default_users_to_empty_regions(
                new_sql_customer)
            new_customer_dict = new_sql_customer.get_proxy_dict()

            datamanager.flush(
            )  # i want to get any exception created by this insert

            new_customer_dict["regions"] = self.resolve_regions_actions(
                old_customer_dict["regions"], new_customer_dict["regions"])

            RdsProxy.send_customer_dict(new_customer_dict, transaction_id,
                                        "PUT")
            datamanager.commit()

            base_link = '{0}{1}/'.format(conf.server.host_ip,
                                         pecan.request.path)

            result_regions = [{
                'id': region.name,
                'added': timestamp,
                'links': {
                    'self': base_link + region.name
                }
            } for region in regions]
            region_result_wrapper = RegionResultWrapper(
                transaction_id=transaction_id, regions=result_regions)

            return region_result_wrapper
        except Exception as exp:
            datamanager.rollback()
            raise exp
Esempio n. 6
0
    def post(self, customer):
        LOG.info("CustomerController - CreateCustomer: " + str(customer))
        authentication.authorize(request, 'customers:create')
        try:
            uuid = None
            if not customer.custId:
                uuid = utils.make_uuid()
            else:
                if not CustomerController.validate_cust_id(customer.custId):
                    utils.audit_trail('create customer',
                                      request.transaction_id, request.headers,
                                      customer.custId)
                    raise ErrorStatus('400', None)
                try:
                    uuid = utils.create_existing_uuid(customer.custId)
                except TypeError:
                    raise ErrorStatus(
                        409.1, 'Customer ID {0} already exists'.format(
                            customer.custId))

            customer_logic = CustomerLogic()
            try:
                result = customer_logic.create_customer(
                    customer, uuid, request.transaction_id)
            except oslo_db.exception.DBDuplicateEntry as exception:
                raise ErrorStatus(
                    409.2, 'Customer field {0} already exists'.format(
                        exception.columns))

            LOG.info("CustomerController - Customer Created: " + str(result))
            event_details = 'Customer {} {} created in regions: {}, with users: {}'.format(
                uuid, customer.name, [r.name for r in customer.regions],
                [u.id for u in customer.users])
            utils.audit_trail('create customer',
                              request.transaction_id,
                              request.headers,
                              uuid,
                              event_details=event_details)
            return result

        except ErrorStatus as exception:
            LOG.log_exception("CustomerController - Failed to CreateCustomer",
                              exception)
            raise err_utils.get_error(request.transaction_id,
                                      message=exception.message,
                                      status_code=exception.status_code)

        except Exception as exception:
            LOG.log_exception("CustomerController - Failed to CreateCustomer",
                              exception)
            raise err_utils.get_error(request.transaction_id,
                                      status_code=500,
                                      error_details=exception.message)
Esempio n. 7
0
 def flush(self):
     try:
         self.session.flush()
     except oslo_db.exception.DBDuplicateEntry as exception:
         raise ErrorStatus(409.2, 'Duplicate Entry {0} already exist'.format(exception.columns))
     except Exception:
         raise
Esempio n. 8
0
    def get_customer(self, customer):

        datamanager = DataManager()

        sql_customer = datamanager.get_cusomer_by_uuid_or_name(customer)

        if not sql_customer:
            raise ErrorStatus(404, 'customer: {0} not found'.format(customer))

        ret_customer = sql_customer.to_wsme()
        if sql_customer.get_real_customer_regions():
            # if we have regions in sql_customer

            resp = requests.get(conf.api.rds_server.base +
                                conf.api.rds_server.status + sql_customer.uuid,
                                verify=conf.verify).json()

            for item in ret_customer.regions:
                for status in resp['regions']:
                    if status['region'] == item.name:
                        item.status = status['status']
                        if status['error_msg']:
                            item.error_message = status['error_msg']
            ret_customer.status = resp['status']
        else:
            ret_customer.status = 'no regions'

        return ret_customer
Esempio n. 9
0
    def delete_default_users(self, customer_uuid, user_id, transaction_id):
        datamanager = DataManager()
        try:
            customer = datamanager.get_cusomer_by_uuid(customer_uuid)
            if customer is None:
                raise ErrorStatus(
                    404, "customer {} does not exist".format(customer_uuid))

            user_role_record = datamanager.get_record('user_role')
            result = user_role_record.delete_user_from_region(
                customer_uuid, 'DEFAULT', user_id)

            if result.rowcount == 0:
                raise NotFound("user {} is not found".format(user_id))

            datamanager.commit()

            print "User {0} from region {1} in customer {2} deleted".format(
                user_id, 'DEFAULT', customer_uuid)

        except NotFound as e:
            datamanager.rollback()
            LOG.log_exception("Failed to delete_users, user not found",
                              e.message)
            raise NotFound("Failed to delete users,  %s not found" % e.message)

        except Exception as exp:
            datamanager.rollback()
            raise exp
Esempio n. 10
0
 def validate_model(self, context=None):
     """this function check if the customer model meet the demands
     :param context: i.e. 'create 'update'
     :return: none
     """
     if context == "update":
         for region in self.regions:
             if region.type == "group":
                 raise ErrorStatus(
                     400,
                     "region type is invalid for update, \'group\' can be only in create"
                 )
Esempio n. 11
0
def get_mock_customer_logic():
    global customer_logic_mock
    customer_logic_mock = mock.MagicMock()

    if users.CustomerLogic.return_error == 0:
        res = Models.UserResultWrapper(transaction_id='1', users=[])

        customer_logic_mock.add_default_users.return_value = res
        customer_logic_mock.add_users.return_value = res
        customer_logic_mock.replace_default_users.return_value = res
        customer_logic_mock.replace_users.return_value = res

    elif users.CustomerLogic.return_error == 1:
        customer_logic_mock.add_users.side_effect = SystemError()
        customer_logic_mock.add_default_users.side_effect = SystemError()
        customer_logic_mock.replace_users.side_effect = SystemError()
        customer_logic_mock.replace_default_users.side_effect = SystemError()
        customer_logic_mock.delete_users.side_effect = SystemError()
        customer_logic_mock.delete_default_users.side_effect = SystemError()

    else:
        customer_logic_mock.add_users.side_effect = ErrorStatus(
            status_code=404)
        customer_logic_mock.add_default_users.side_effect = ErrorStatus(
            status_code=404)
        customer_logic_mock.replace_users.side_effect = ErrorStatus(
            status_code=404)
        customer_logic_mock.replace_default_users.side_effect = ErrorStatus(
            status_code=404)
        customer_logic_mock.delete_users.side_effect = ErrorStatus(
            status_code=404)
        customer_logic_mock.delete_default_users.side_effect = ErrorStatus(
            status_code=404)

    return customer_logic_mock
Esempio n. 12
0
    def add_regions(self, customer_uuid, regions, transaction_id):
        datamanager = DataManager()
        customer_record = datamanager.get_record('customer')
        try:
            # TODO DataBase action
            customer_id = datamanager.get_customer_id_by_uuid(customer_uuid)
            if customer_id is None:
                raise ErrorStatus(
                    404,
                    "customer with id {} does not exist".format(customer_uuid))
            self.add_regions_to_db(regions, customer_id, datamanager)

            sql_customer = customer_record.read_customer_by_uuid(customer_uuid)

            sql_customer = self.add_default_users_to_empty_regions(
                sql_customer)
            new_customer_dict = sql_customer.get_proxy_dict()

            for region in new_customer_dict["regions"]:
                new_region = next(
                    (r for r in regions if r.name == region["name"]), None)
                if new_region:
                    region["action"] = "create"
                else:
                    region["action"] = "modify"

            timestamp = utils.get_time_human()
            datamanager.flush(
            )  # i want to get any exception created by this insert
            RdsProxy.send_customer_dict(new_customer_dict, transaction_id,
                                        "POST")
            datamanager.commit()

            base_link = '{0}{1}/'.format(conf.server.host_ip,
                                         pecan.request.path)

            result_regions = [{
                'id': region.name,
                'added': timestamp,
                'links': {
                    'self': base_link + region.name
                }
            } for region in regions]
            region_result_wrapper = RegionResultWrapper(
                transaction_id=transaction_id, regions=result_regions)

            return region_result_wrapper
        except Exception as exp:
            datamanager.rollback()
            raise
Esempio n. 13
0
    def handle_region_group(self):
        regions_to_add = []
        for region in self.regions[:]:  # get copy of it to be able to delete from the origin
            if region.type == "group":
                group_regions = self.get_regions_for_group(region.name)
                if not group_regions:
                    raise ErrorStatus(404,
                                      'Group {} Not found'.format(region.name))
                for group_region in group_regions:
                    regions_to_add.append(
                        Region(name=group_region,
                               type='single',
                               quotas=region.quotas,
                               users=region.users))
                self.regions.remove(region)

        self.regions.extend(set(regions_to_add))  # remove duplicates if exist
Esempio n. 14
0
def get_mock_customer_logic():
    global customer_logic_mock
    customer_logic_mock = mock.MagicMock()

    if enabled.CustomerLogic.return_error == 0:
        res = Models.CustomerResultWrapper(transaction_id='1',
                                           id='1',
                                           links={},
                                           updated=None,
                                           created='1')

        customer_logic_mock.enable.return_value = res

    elif enabled.CustomerLogic.return_error == 1:
        customer_logic_mock.enable.side_effect = SystemError()

    elif enabled.CustomerLogic.return_error == 2:
        customer_logic_mock.enable.side_effect = ErrorStatus(status_code=404)

    return customer_logic_mock
Esempio n. 15
0
    def enable(self, customer_uuid, enabled, transaction_id):
        try:
            datamanager = DataManager()

            customer_record = datamanager.get_record('customer')
            sql_customer = customer_record.read_customer_by_uuid(customer_uuid)

            if not sql_customer:
                raise ErrorStatus(
                    404, 'customer: {0} not found'.format(customer_uuid))

            sql_customer.enabled = 1 if enabled.enabled else 0

            RdsProxy.send_customer(sql_customer, transaction_id, "PUT")

            datamanager.flush()  # get any exception created by this action
            datamanager.commit()

        except Exception as exp:
            datamanager.rollback()
            raise exp
Esempio n. 16
0
    def delete_region(self, customer_id, region_id, transaction_id):
        datamanager = DataManager()
        try:
            customer_region = datamanager.get_record('customer_region')

            sql_customer = datamanager.get_cusomer_by_uuid(customer_id)
            if sql_customer is None:
                raise ErrorStatus(
                    404,
                    "customer with id {} does not exist".format(customer_id))
            customer_dict = sql_customer.get_proxy_dict()

            customer_region.delete_region_for_customer(customer_id, region_id)
            datamanager.flush(
            )  # i want to get any exception created by this insert

            # i want to get any exception created by this insert
            datamanager.flush()

            region = next((r.region
                           for r in sql_customer.customer_customer_regions
                           if r.region.name == region_id), None)
            if region:
                if region.type == 'group':
                    set_utils_conf(conf)
                    regions = get_regions_of_group(region.name)
                else:
                    regions = [region_id]
            for region in customer_dict['regions']:
                if region['name'] in regions:
                    region['action'] = 'delete'

            RdsProxy.send_customer_dict(customer_dict, transaction_id, "PUT")
            datamanager.commit()

            LOG.debug("Region {0} in customer {1} deleted".format(
                region_id, customer_id))
        except Exception as exp:
            datamanager.rollback()
            raise
Esempio n. 17
0
    def update_customer(self, customer, customer_uuid, transaction_id):
        datamanager = DataManager()
        try:
            customer.validate_model('update')
            customer_record = datamanager.get_record('customer')
            cutomer_id = customer_record.get_customer_id_from_uuid(
                customer_uuid)

            sql_customer = customer_record.read_customer_by_uuid(customer_uuid)
            if not sql_customer:
                raise ErrorStatus(
                    404, 'customer {0} was not found'.format(customer_uuid))
            old_customer_dict = sql_customer.get_proxy_dict()
            customer_record.delete_by_primary_key(cutomer_id)
            datamanager.flush()

            sql_customer = self.build_full_customer(customer, customer_uuid,
                                                    datamanager)
            sql_customer = self.add_default_users_to_empty_regions(
                sql_customer)
            new_customer_dict = sql_customer.get_proxy_dict()
            new_customer_dict["regions"] = self.resolve_regions_actions(
                old_customer_dict["regions"], new_customer_dict["regions"])

            customer_result_wrapper = build_response(customer_uuid,
                                                     transaction_id, 'update')
            datamanager.flush(
            )  # i want to get any exception created by this insert
            if not len(new_customer_dict['regions']) == 0:
                RdsProxy.send_customer_dict(new_customer_dict, transaction_id,
                                            "PUT")
            datamanager.commit()

            return customer_result_wrapper

        except Exception as exp:
            LOG.log_exception("CustomerLogic - Failed to CreateCustomer", exp)
            datamanager.rollback()
            raise
Esempio n. 18
0
    def delete_customer_by_uuid(self, customer_id):
        datamanager = DataManager()

        try:
            datamanager.begin_transaction()
            customer_record = datamanager.get_record('customer')

            sql_customer = customer_record.read_customer_by_uuid(customer_id)
            if sql_customer is None:
                # The customer does not exist, so the delete operation is
                # considered successful
                return

            real_regions = sql_customer.get_real_customer_regions()
            if len(real_regions) > 0:
                # Do not delete a customer that still has some regions
                raise ErrorStatus(
                    405, "Cannot delete a customer that has regions. "
                    "Please delete the regions first and then "
                    "delete the customer.")
            else:
                expected_status = 'Success'
                invalid_status = 'N/A'
                # Get status from RDS
                resp = RdsProxy.get_status(sql_customer.uuid)
                if resp.status_code == 200:
                    status_resp = resp.json()
                    if 'status' in status_resp.keys():
                        LOG.debug('RDS returned status: {}'.format(
                            status_resp['status']))
                        status = status_resp['status']
                    else:
                        # Invalid response from RDS
                        LOG.error('Response from RDS did not contain status')
                        status = invalid_status
                elif resp.status_code == 404:
                    # Customer not found in RDS, that means it never had any regions
                    # So it is OK to delete it
                    LOG.debug(
                        'Resource not found in RDS, so it is OK to delete')
                    status = expected_status
                else:
                    # Invalid status code from RDS
                    log_message = 'Invalid response code from RDS: {}'.format(
                        resp.status_code)
                    log_message = log_message.replace('\n',
                                                      '_').replace('\r', '_')
                    LOG.warning(log_message)
                    status = invalid_status

                if status == invalid_status:
                    raise ErrorStatus(500, "Could not get customer status")
                elif status != expected_status:
                    raise ErrorStatus(
                        409, "The customer has not been deleted "
                        "successfully from all of its regions "
                        "(either the deletion failed on one of the "
                        "regions or it is still in progress)")

            # OK to delete
            customer_record.delete_customer_by_uuid(customer_id)

            datamanager.flush(
            )  # i want to get any exception created by this delete
            datamanager.commit()
        except Exception as exp:
            LOG.log_exception("CustomerLogic - Failed to delete customer", exp)
            datamanager.rollback()
            raise
Esempio n. 19
0
    def send_customer_dict(customer_dict, transaction_id,
                           method):  # method is "POST" or "PUT"
        data = {
            "service_template": {
                "resource": {
                    "resource_type": "customer"
                },
                "model": str(json.dumps(customer_dict)),
                "tracking": {
                    "external_id": "",
                    "tracking_id": transaction_id
                }
            }
        }

        data_to_display = {
            "service_template": {
                "resource": {
                    "resource_type": "customer"
                },
                "model": customer_dict,
                "tracking": {
                    "external_id": "",
                    "tracking_id": transaction_id
                }
            }
        }

        pp = pprint.PrettyPrinter(width=30)
        pretty_text = pp.pformat(data_to_display)
        wrapper_json = json.dumps(data)

        headers['X-RANGER-Client'] = request.headers[
            'X-RANGER-Client'] if 'X-RANGER-Client' in request.headers else \
            'NA'
        headers['X-RANGER-Requester'] = request.headers[
            'X-RANGER-Requester'] if 'X-RANGER-Requester' in request.headers else \
            ''

        LOG.debug(
            "Wrapper JSON before sending action: {0} to Rds Proxy\n{1}".format(
                method, pretty_text))
        LOG.info("Sending to RDS Server: " + conf.api.rds_server.base +
                 conf.api.rds_server.resources)

        wrapper_json = json.dumps(data)

        if method == "POST":
            resp = requests.post(conf.api.rds_server.base +
                                 conf.api.rds_server.resources,
                                 data=wrapper_json,
                                 headers=headers,
                                 verify=conf.verify)
        else:
            resp = requests.put(conf.api.rds_server.base +
                                conf.api.rds_server.resources,
                                data=wrapper_json,
                                headers=headers,
                                verify=conf.verify)
        if resp.content:
            LOG.debug("Response Content from rds server: {0}".format(
                resp.content))

        content = resp.content
        if resp.content:
            content = resp.json()

        if resp.content and 200 <= resp.status_code < 300:
            content = resp.json()
            return content

        raise ErrorStatus(resp.status_code, content)