def get_customer_list_by_criteria(self, region, user, starts_with, contains, metadata): datamanager = DataManager() customer_record = datamanager.get_record('customer') sql_customers = customer_record.get_customers_by_criteria( region=region, user=user, starts_with=starts_with, contains=contains, metadata=metadata) response = CustomerSummaryResponse() for sql_customer in sql_customers: # get aggregate status for each customer customer_status = RdsProxy.get_status(sql_customer.uuid) customer = CustomerSummary.from_db_model(sql_customer) if customer_status.status_code == 200: customer.status = customer_status.json()['status'] response.customers.append(customer) return response
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