Example #1
0
    def create_customer(self, customer, uuid, transaction_id):
        datamanager = DataManager()
        try:
            customer.handle_region_group()
            sql_customer = self.build_full_customer(customer, uuid,
                                                    datamanager)
            customer_result_wrapper = build_response(uuid, transaction_id,
                                                     'create')

            sql_customer = self.add_default_users_to_empty_regions(
                sql_customer)
            if sql_customer.customer_customer_regions and len(
                    sql_customer.customer_customer_regions) > 1:
                customer_dict = sql_customer.get_proxy_dict()
                for region in customer_dict["regions"]:
                    region["action"] = "create"

                datamanager.flush(
                )  # i want to get any exception created by this insert
                RdsProxy.send_customer_dict(customer_dict, transaction_id,
                                            "POST")
            else:
                LOG.debug(
                    "Customer with no regions - wasn't send to RDS Proxy " +
                    str(customer))

            datamanager.commit()

        except Exception as exp:
            LOG.log_exception("CustomerLogic - Failed to CreateCustomer", exp)
            datamanager.rollback()
            raise

        return customer_result_wrapper
Example #2
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
Example #3
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
Example #4
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
Example #5
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
Example #6
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
Example #7
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
Example #8
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