Exemple #1
0
def update_customer_metadata(customer_uuid, metadata_wrapper, transaction_id):
    sql_metadata_collection = map_metadata(customer_uuid, metadata_wrapper)

    datamanager = DataManager()

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

        if not sql_customer:
            logger.error(
                'customer not found, customer uuid: {0}'.format(customer_uuid))
            raise ValueError(
                'customer not found, customer uuid: {0}'.format(customer_uuid))

        while len(sql_customer.customer_metadata) > 0:
            sql_customer.customer_metadata.remove(
                sql_customer.customer_metadata[0])

        for metadata in sql_metadata_collection:
            sql_customer.customer_metadata.append(metadata)
            logger.debug('updating metadata {0}'.format(
                json.dumps(metadata.get_proxy_dict())))

        if len(sql_customer.customer_customer_regions) > 1:
            RdsProxy.send_customer(sql_customer, transaction_id, "PUT")
        datamanager.commit()

        customer_result_wrapper = build_response(customer_uuid, transaction_id)

        return customer_result_wrapper

    except Exception as exp:
        datamanager.rollback()
        raise exp
Exemple #2
0
    def delete_users(self, customer_uuid, region_id, user_id, transaction_id):
        datamanager = DataManager()
        try:
            user_role_record = datamanager.get_record('user_role')

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

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

            RdsProxy.send_customer(customer, transaction_id, "PUT")
            datamanager.commit()

            print "User {0} from region {1} in customer {2} deleted".format(
                user_id, region_id, 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 exception:
            datamanager.rollback()
            LOG.log_exception("Failed to delete_users", exception)
            raise exception
Exemple #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
Exemple #4
0
def add_customer_metadata(customer_uuid, metadata_wrapper, transaction_id):
    sql_metadata_collection = map_metadata(customer_uuid, metadata_wrapper)

    datamanager = DataManager()

    try:
        customer_record = datamanager.get_record('customer')
        sql_customer = customer_record.read_customer_by_uuid(customer_uuid)
        if not sql_customer:
            logger.error(
                'customer not found, customer uuid: {0}'.format(customer_uuid))
            raise ValueError(
                'customer not found, customer uuid: {0}'.format(customer_uuid))

        for metadata in sql_metadata_collection:
            metadata_match = [
                m for m in sql_customer.customer_metadata
                if m.field_key == metadata.field_key
            ]
            if len(metadata_match) > 0:
                logger.error(
                    'Duplicate metadata key, key already exits: {0}'.format(
                        metadata.field_key))
                raise AttributeError(
                    'Duplicate metadata key, key already exits: {0}'.format(
                        metadata.field_key))

        for metadata in sql_metadata_collection:
            sql_customer.customer_metadata.append(metadata)
            logger.debug('updating metadata {0}'.format(
                json.dumps(metadata.get_proxy_dict())))

        logger.debug('finished appending metadata to customer')
        if len(sql_customer.customer_customer_regions) > 1:
            RdsProxy.send_customer(sql_customer, transaction_id, "PUT")
        datamanager.commit()

        customer_result_wrapper = build_response(customer_uuid, transaction_id)

        return customer_result_wrapper

    except Exception as exp:
        datamanager.rollback()
        raise exp
Exemple #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