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
def add_users(self, customer_uuid, region_name, users, transaction_id, p_datamanager=None): datamanager = None try: if p_datamanager is None: datamanager = DataManager() datamanager.begin_transaction() else: datamanager = p_datamanager region_id = datamanager.get_region_id_by_name(region_name) 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)) if region_id is None: raise ErrorStatus(404, "region {} not found".format(region_name)) self.add_users_to_db(datamanager, customer_id, region_id, 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 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: if 'Duplicate' in exception.message: raise ErrorStatus(409, exception.message) datamanager.rollback() LOG.log_exception("Failed to add_users", exception) raise exception