def create_customer(self, customer, uuid, transaction_id): datamanager = DataManager() try: customer.handle_region_group() sql_customer = self.build_full_customer(customer, uuid, datamanager) customer_result_wrapper = build_response(uuid, transaction_id, 'create') sql_customer = self.add_default_users_to_empty_regions( sql_customer) if sql_customer.customer_customer_regions and len( sql_customer.customer_customer_regions) > 1: customer_dict = sql_customer.get_proxy_dict() for region in customer_dict["regions"]: region["action"] = "create" datamanager.flush( ) # i want to get any exception created by this insert RdsProxy.send_customer_dict(customer_dict, transaction_id, "POST") else: LOG.debug( "Customer with no regions - wasn't send to RDS Proxy " + str(customer)) datamanager.commit() except Exception as exp: LOG.log_exception("CustomerLogic - Failed to CreateCustomer", exp) datamanager.rollback() raise return customer_result_wrapper
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_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
def delete_region(self, customer_id, region_id, transaction_id): datamanager = DataManager() try: customer_region = datamanager.get_record('customer_region') sql_customer = datamanager.get_cusomer_by_uuid(customer_id) if sql_customer is None: raise ErrorStatus( 404, "customer with id {} does not exist".format(customer_id)) customer_dict = sql_customer.get_proxy_dict() customer_region.delete_region_for_customer(customer_id, region_id) datamanager.flush( ) # i want to get any exception created by this insert # i want to get any exception created by this insert datamanager.flush() region = next((r.region for r in sql_customer.customer_customer_regions if r.region.name == region_id), None) if region: if region.type == 'group': set_utils_conf(conf) regions = get_regions_of_group(region.name) else: regions = [region_id] for region in customer_dict['regions']: if region['name'] in regions: region['action'] = 'delete' RdsProxy.send_customer_dict(customer_dict, transaction_id, "PUT") datamanager.commit() LOG.debug("Region {0} in customer {1} deleted".format( region_id, customer_id)) except Exception as exp: datamanager.rollback() raise
def update_customer(self, customer, customer_uuid, transaction_id): datamanager = DataManager() try: customer.validate_model('update') customer_record = datamanager.get_record('customer') cutomer_id = customer_record.get_customer_id_from_uuid( customer_uuid) sql_customer = customer_record.read_customer_by_uuid(customer_uuid) if not sql_customer: raise ErrorStatus( 404, 'customer {0} was not found'.format(customer_uuid)) old_customer_dict = sql_customer.get_proxy_dict() customer_record.delete_by_primary_key(cutomer_id) datamanager.flush() sql_customer = self.build_full_customer(customer, customer_uuid, datamanager) sql_customer = self.add_default_users_to_empty_regions( sql_customer) new_customer_dict = sql_customer.get_proxy_dict() new_customer_dict["regions"] = self.resolve_regions_actions( old_customer_dict["regions"], new_customer_dict["regions"]) customer_result_wrapper = build_response(customer_uuid, transaction_id, 'update') datamanager.flush( ) # i want to get any exception created by this insert if not len(new_customer_dict['regions']) == 0: RdsProxy.send_customer_dict(new_customer_dict, transaction_id, "PUT") datamanager.commit() return customer_result_wrapper except Exception as exp: LOG.log_exception("CustomerLogic - Failed to CreateCustomer", exp) datamanager.rollback() raise