From 04e8c7658c0ed31a334cf64fcfd4aa5f1962b39d Mon Sep 17 00:00:00 2001 From: Michal Jagiello Date: Mon, 27 Jun 2022 12:59:33 +0000 Subject: [Data provider] Add relationships between some A&AI resources Create relationship between: Tenant <-> Line of business Tenant <-> Owning entity Cloud region <-> Project Issue-ID: INT-2126 Signed-off-by: Michal Jagiello Change-Id: I3523c02b28b5fe972c0fbba112c8ffa532feadb8 --- .../resources/cloud_region_resource.py | 39 ++++++++++++++-------- onap_data_provider/resources/tenant_resource.py | 29 ++++++++++++++++ 2 files changed, 54 insertions(+), 14 deletions(-) (limited to 'onap_data_provider/resources') diff --git a/onap_data_provider/resources/cloud_region_resource.py b/onap_data_provider/resources/cloud_region_resource.py index 7bcc3b4..9f66b48 100644 --- a/onap_data_provider/resources/cloud_region_resource.py +++ b/onap_data_provider/resources/cloud_region_resource.py @@ -21,6 +21,8 @@ from onap_data_provider.resources.esr_system_info_resource import ( import logging from typing import Any, Dict +from onapsdk.aai.aai_element import Relationship # type: ignore +from onapsdk.aai.business import Project # type: ignore from onapsdk.aai.cloud_infrastructure import CloudRegion, Complex # type: ignore from onapsdk.msb.k8s.connectivity_info import ConnectivityInfo # type: ignore from onapsdk.so.so_db_adapter import SoDbAdapter, IdentityService # type: ignore @@ -122,6 +124,20 @@ class CloudRegionResource(Resource): IdentityService("DEFAULT_KEYSTONE"), ) + # Link with project + for project_data in self.data.get("projects", []): + try: + project: Project = Project.get_by_name(project_data["project"]["name"]) + except ResourceNotFound: + project = Project.create(project_data["project"]["name"]) + project.add_relationship( + Relationship( + related_to="cloud-region", + related_link=self.cloud_region.url, + relationship_data=[] + ) + ) + @property def exists(self) -> bool: """Determine if resource already exists or not. @@ -156,21 +172,16 @@ class CloudRegionResource(Resource): return self._cloud_region def _link_to_complex(self, complex_physical_id: str) -> None: - try: # TODO: change it when https://gitlab.com/Orange-OpenSource/lfn/onap/python-onapsdk/-/issues/120 is fixed - if self.cloud_region.complex: - logging.info( - "Cloud region has relationship with complex: %s. New relationship can't be created", - self.cloud_region.complex.physical_location_id, - ) - return - except ResourceNotFound: - logging.debug("Cloud region has no complex linked with") - try: - complex: Complex = next( - Complex.get_all(physical_location_id=complex_physical_id) + if self.cloud_region.complex: + logging.info( + "Cloud region has relationship with complex: %s. New relationship can't be created", + self.cloud_region.complex.physical_location_id, ) - self.cloud_region.link_to_complex(complex) - except StopIteration: + return + try: + cmplx: Complex = Complex.get_by_physical_location_id(complex_physical_id) + self.cloud_region.link_to_complex(cmplx) + except ResourceNotFound: logging.error( "Complex %s does not exist, please create it before cloud region creation", complex_physical_id, diff --git a/onap_data_provider/resources/tenant_resource.py b/onap_data_provider/resources/tenant_resource.py index 13d003f..b4bca68 100644 --- a/onap_data_provider/resources/tenant_resource.py +++ b/onap_data_provider/resources/tenant_resource.py @@ -17,7 +17,9 @@ import logging from typing import Any, Dict, Optional +from onapsdk.aai.aai_element import Relationship # type: ignore from onapsdk.aai.cloud_infrastructure import CloudRegion, Tenant # type: ignore +from onapsdk.aai.business import LineOfBusiness, OwningEntity # type: ignore from .resource import Resource from onapsdk.exceptions import ResourceNotFound # type: ignore @@ -54,6 +56,33 @@ class TenantResource(Resource): tenant_context=self.data.get("tenant-context"), ) + for lines_of_business_data in self.data.get("lines-of-business", []): + try: + line_of_business: LineOfBusiness = LineOfBusiness.get_by_name(lines_of_business_data["line-of-business"]["name"]) + except ResourceNotFound: + line_of_business = LineOfBusiness.create(lines_of_business_data["line-of-business"]["name"]) + line_of_business.add_relationship( + Relationship( + related_to="tenant", + related_link=self.tenant.url, + relationship_data=[] + ) + ) + + for owning_entities_data in self.data.get("owning-entities", []): + try: + owning_entity: OwningEntity = OwningEntity.get_by_owning_entity_name(owning_entities_data["owning-entity"]["name"]) + except ResourceNotFound: + owning_entity = OwningEntity.create(owning_entities_data["owning-entity"]["name"], + owning_entities_data["owning-entity"]["id"]) + owning_entity.add_relationship( + Relationship( + related_to="tenant", + related_link=self.tenant.url, + relationship_data=[] + ) + ) + @property def exists(self) -> bool: """Determine if resource already exists or not. -- cgit 1.2.3-korg