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