diff --git a/orm/services/resource_distributor/rds/proxies/rds_resource_service_proxy.py b/orm/services/resource_distributor/rds/proxies/rds_resource_service_proxy.py index 66c2d8c0..e1802ec2 100755 --- a/orm/services/resource_distributor/rds/proxies/rds_resource_service_proxy.py +++ b/orm/services/resource_distributor/rds/proxies/rds_resource_service_proxy.py @@ -57,7 +57,18 @@ def delete_customer_region(customer_id, region_id): datamanager = CmsDataManager() try: customer_region = datamanager.get_record('customer_region') - customer_region.delete_region_for_customer(customer_id, region_id) + cust_regions = customer_region.get_regions_for_customer(customer_id) + + for region in cust_regions: + # when "force_delete" option is used on delete customer region + # request, we don't need to execute the delete_region_for_customer + # function as it has already been previously handled by the + # force_delete option + if region.region.name == region_id: + customer_region.delete_region_for_customer(customer_id, + region_id) + break + datamanager.flush() datamanager.commit() except Exception as exp: @@ -91,7 +102,10 @@ def delete_flavor_region(flavor_uuid, region_name): flavor_rec = datamanager.get_record('flavor') sql_flavor = flavor_rec.get_flavor_by_id(flavor_uuid) - if sql_flavor is None: + # An empty 'flavor_regions' indicates that force_delete option was + # used on delete flavor region request. As the flavor is no longer + # associated with any region, we can skip the remove_region function. + if sql_flavor is None or not sql_flavor.flavor_regions: return sql_flavor.remove_region(region_name) @@ -112,7 +126,10 @@ def delete_image_region(image_uuid, region_name): image_rec = datamanager.get_record('image') sql_image = image_rec.get_image_by_id(image_uuid) - if sql_image is None: + # An empty sql_image.regions indicates that force_delete option was + # used on delete image region request. As the image is no longer + # associated with any region, we can skip the remove_region function. + if sql_image is None or not sql_image.regions: return sql_image.remove_region(region_name) diff --git a/orm/services/resource_distributor/rds/services/region_resource_id_status.py b/orm/services/resource_distributor/rds/services/region_resource_id_status.py index cb7c02b4..d6e3d4d2 100755 --- a/orm/services/resource_distributor/rds/services/region_resource_id_status.py +++ b/orm/services/resource_distributor/rds/services/region_resource_id_status.py @@ -32,9 +32,14 @@ def add_update_template_data(data): raise -def delete_resource_status_data(resource_id): - # delete all resource status entries for the given resource_id - pass +def delete_resource_status_data(resource_id, region): + # delete resource status entry for the given resource_id and region + logger.debug("delete resource status data for resource %s and " + "region %s" % (resource_id, region)) + conn = factory.get_region_resource_id_status_connection() + resource_id, region = conn.get_resource_region_data(resource_id, region) + conn = factory.get_region_resource_id_status_connection() + conn.delete_resource_status_entry(resource_id, region) def get_template_data(resource_id, region): @@ -102,15 +107,6 @@ def get_regions_by_status_resource_id(status, resource_id): return result -def delete_resource_template_data(resource_id, region): - logger.debug("delete resource_template_data for resource %s and " - "region %s" % (resource_id, region)) - conn = factory.get_region_resource_id_status_connection() - resource_id, region = conn.get_resource_region_data(resource_id, region) - conn = factory.get_resource_stack_data_connection() - conn.delete_resource_template(resource_id, region) - - def validate_resource_type(resource_type): allowed_resource_type = config['allowed_resource_type'] if resource_type not in allowed_resource_type: diff --git a/orm/services/resource_distributor/rds/sot/base_sot.py b/orm/services/resource_distributor/rds/sot/base_sot.py deleted file mode 100644 index f0e8332d..00000000 --- a/orm/services/resource_distributor/rds/sot/base_sot.py +++ /dev/null @@ -1,18 +0,0 @@ -""" SoT interface definition""" - - -class BaseSoT(object): - - def save_resource_to_sot(self, - tracking_id, - transaction_id, - resource_list, - headers={}): - raise NotImplementedError("Please Implement this method") - - def validate_sot_state(self): - raise NotImplementedError("Please Implement this method") - - -class SoTError(Exception): - pass diff --git a/orm/services/resource_distributor/rds/sot/sot_utils.py b/orm/services/resource_distributor/rds/sot/sot_utils.py deleted file mode 100644 index ad629ee9..00000000 --- a/orm/services/resource_distributor/rds/sot/sot_utils.py +++ /dev/null @@ -1,43 +0,0 @@ -import yaml - - -def merge_yamls(document, section): - document_dict = yaml.safe_load(document) - section_dict = yaml.safe_load(section) - merge_dict(section_dict, document_dict) - new_document = yaml.dump(document_dict) - return new_document - - -# source is being merged into destiantion -def merge_dict(source, destination): - for key, value in list(source.items()): - if isinstance(value, dict): - # get node or create one - node = destination.setdefault(key, {}) - merge_dict(value, node) - else: - destination[key] = value - - return destination - -document = """ - a: 1 - b: - c: 3 - d: 4 - f: - h: h1 -""" - -section = """ - b: - d: 6 - e: 5 - f: - g: g1 - h: - h1: h2 -""" - -print((merge_yamls(document, section))) diff --git a/orm/services/resource_distributor/rds/storage/mysql/region_resource_id_status.py b/orm/services/resource_distributor/rds/storage/mysql/region_resource_id_status.py index dd0fbfeb..1fcb8deb 100755 --- a/orm/services/resource_distributor/rds/storage/mysql/region_resource_id_status.py +++ b/orm/services/resource_distributor/rds/storage/mysql/region_resource_id_status.py @@ -158,6 +158,23 @@ class ResStatusConnection(region_resource_id_status.ResourceStatusBase): except oslo_db.exception.DBDuplicateEntry as e: logger.warning("Duplicate entry: {}".format(str(e))) + def delete_resource_status_entry(self, resource_id, region_name): + # note that when a resource_status entry is deleted, its corresponding + # entry in resource_template_data is deleted as well + try: + session = self._engine_facade.get_session() + with session.begin(): + record = session.query(ResourceStatusRecord).\ + filter_by(resource_id=resource_id, + region=region_name).delete() + if record is None: + logger.exception( + 'Resource status data not found with resource id {} ' + 'and region name {}'.format(resource_id, region_name)) + + except Exception as exp: + raise + def get_records_by_resource_id(self, resource_id): return self.get_records_by_filter_args(resource_id=resource_id) @@ -323,21 +340,6 @@ class ResTemplateConnection(region_resource_id_status.ResourceTemplateBase): except oslo_db.exception.DBDuplicateEntry as e: logger.warning("Duplicate entry: {}".format(str(e))) - def delete_resource_template(self, resource_id, region_name): - try: - session = self._engine_facade.get_session() - with session.begin(): - record = session.query(ResourceTemplateRecord).\ - filter_by(resource_id=resource_id, - region=region_name).delete() - if record is None: - logger.exception( - 'Template data not found with resource id {} and ' - 'region name {}'.format(resource_id, region_name)) - - except Exception as exp: - raise - def get_resource_template_data(self, resource_id, region): logger.debug("Get resource template data by resource {} and " "region '{}' ".format(resource_id, region)) diff --git a/orm/services/resource_distributor/rds/utils/utils.py b/orm/services/resource_distributor/rds/utils/utils.py index 4c8196d9..2a463183 100755 --- a/orm/services/resource_distributor/rds/utils/utils.py +++ b/orm/services/resource_distributor/rds/utils/utils.py @@ -36,7 +36,7 @@ def invoke_delete_region(data): resource_type=data['resource_type'], resource_id=data['resource_id'], region=data['region']) # delete heat template entry - regionResourceIdStatus.delete_resource_template_data( + regionResourceIdStatus.delete_resource_status_data( data['resource_id'], data['region']) return diff --git a/orm/tests/unit/rds/services/test_create_resource.py b/orm/tests/unit/rds/services/test_create_resource.py index 1d6e2338..5e7a46fe 100755 --- a/orm/tests/unit/rds/services/test_create_resource.py +++ b/orm/tests/unit/rds/services/test_create_resource.py @@ -151,8 +151,8 @@ class CreateResource(unittest.TestCase): 'get_regions_by_status_resource_id', return_value=None) @patch.object(ResourceService.uuid_utils, 'get_random_uuid', return_value='uuid-gen-123456') - def test_create_flavor_sot_data_check(self, tranid, result, - yamlbuilder, database): + def test_create_flavor_data_check(self, tranid, result, + yamlbuilder, database): """check list creating.""" input_data = InputData( transaction_id='497ab942-1ac0-11e6-82f3-005056a5129b',