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 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
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
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