From e9dbe96626c23204bac7426bba69b4e2aedfbd8f Mon Sep 17 00:00:00 2001 From: Piotr Stanior Date: Mon, 31 Jan 2022 08:44:09 +0100 Subject: Allow tenant name in service instantiation Change-Id: I26af41c8a1bf35c21f62d9b252fcd5eb9c7bbaee Signed-off-by: Piotr Stanior Issue-ID: INT-2048 Signed-off-by: Piotr Stanior --- .../resources/service_instance_resource.py | 28 ++++++++++++++++- onap_data_provider/schemas/infra_1_1.schema | 4 +++ tests/test_service_instance_resource.py | 36 ++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/onap_data_provider/resources/service_instance_resource.py b/onap_data_provider/resources/service_instance_resource.py index b9af670..b23de40 100644 --- a/onap_data_provider/resources/service_instance_resource.py +++ b/onap_data_provider/resources/service_instance_resource.py @@ -69,7 +69,33 @@ class ServiceInstanceResource(Resource): cloud_owner=self.data["cloud_owner"], cloud_region_id=cloud_region_id, ) - tenant: Tenant = cloud_region.get_tenant(self.data["tenant_id"]) + tenant: Tenant = None + if tenant_name := self.data.get("tenant_name"): + # TODO: https://jira.onap.org/browse/INT-2056 refactor below when ONAP SDK 9.2.3 is released + cr_tenants = [ + x for x in cloud_region.tenants if x.name == tenant_name + ] + if len(cr_tenants) > 1: + msg = "\n".join( + [ + "===================", + f"There are more than one tenant with given name '{tenant_name}':", + "\n".join( + f"{t.name}: {t.tenant_id}" for t in cr_tenants + ), + "Use tenant-id instead of tenant-name to specify which tenant should be used during the instantiation.", + "===================", + ] + ) + logging.error(msg) + raise ValueError( + "Value provided for 'tenant_name' is ambiguous." + ) + if not cr_tenants: + raise ValueError(f"Tenant '{tenant_name}' not found.") + tenant = cr_tenants.pop() + else: + tenant = cloud_region.get_tenant(self.data["tenant_id"]) self.service_subscription.link_to_cloud_region_and_tenant( cloud_region, tenant ) diff --git a/onap_data_provider/schemas/infra_1_1.schema b/onap_data_provider/schemas/infra_1_1.schema index 7175ec5..7514acc 100644 --- a/onap_data_provider/schemas/infra_1_1.schema +++ b/onap_data_provider/schemas/infra_1_1.schema @@ -402,6 +402,10 @@ properties: maximum: 99999 aai_service: type: string + tenant_id: + type: string + tenant_name: + type: string service_subscription_type: type: string instantiation_parameters: diff --git a/tests/test_service_instance_resource.py b/tests/test_service_instance_resource.py index 662aa32..04a18d6 100644 --- a/tests/test_service_instance_resource.py +++ b/tests/test_service_instance_resource.py @@ -172,3 +172,39 @@ def test_test_service_instance_resource_vnf_and_pnf_instantiation(mock_service_s pnf = so_service.pnfs[0] assert pnf.instance_name == "test_pnf_instance" assert pnf.model_name == "test_pnf" + + +@patch( + "onap_data_provider.resources.service_instance_resource.ServiceInstanceResource.exists", + new_callable=PropertyMock, +) +@patch("onap_data_provider.resources.service_instance_resource.Customer") +@patch("onap_data_provider.resources.service_instance_resource.Project") +@patch("onap_data_provider.resources.service_instance_resource.OwningEntity") +@patch("onap_data_provider.resources.service_instance_resource.CloudRegion") +@patch("onap_data_provider.resources.service_instance_resource.ServiceInstantiation") +@patch("onap_data_provider.resources.service_instance_resource.Service") +@patch("onap_data_provider.resources.service_instance_resource.AaiService") +def test_si_resource_create_with_tenant_name( + mock_aai_service, + mock_service, + mock_service_instantionation, + mock_cr, + mock_oe, + mock_project, + mock_customer, + mock_si_resource_exists, +): + data = RESOURCE_DATA_1_1 + data.pop("tenant_id") + data.update({"tenant_name": "test_1"}) + si_resource = ServiceInstanceResource(data) + mock_oe.get_by_owning_entity_name.side_effect = APIError + mock_si_resource_exists.return_value = True + si_resource.create() + mock_service.assert_not_called() + mock_si_resource_exists.return_value = False + try: + si_resource.create() + except ValueError: + assert True -- cgit 1.2.3-korg