1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2020 Orange
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # ============LICENSE_END=========================================================
20 from uuid import uuid4
22 from config import Config
23 from k8s_client import K8sClient
24 from so_db_adapter import SoDBUpdate
25 from onapsdk.aai.business import Customer
26 from onapsdk.aai.cloud_infrastructure import Complex, CloudRegion
27 from onapsdk.msb.k8s import ConnectivityInfo
28 from onapsdk.exceptions import ResourceNotFound
30 logger = logging.getLogger("")
31 logger.setLevel(logging.DEBUG)
32 fh = logging.StreamHandler()
33 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
34 fh.setFormatter(fh_formatter)
37 MYPATH = os.path.dirname(os.path.realpath(__file__))
39 #### Create complex if not exists ####
40 logger.info("******** Complex *******")
42 complex = list(Complex.get_all(physical_location_id=Config.COMPLEX_ID))[0]
43 logger.info("Complex exists")
44 except (IndexError, ResourceNotFound):
45 logger.info("Complex does not exists")
46 complex = Complex.create(physical_location_id=Config.COMPLEX_ID,
47 name=Config.COMPLEX_ID,
48 physical_location_type="office",
49 street1="DummyStreet 1",
52 country="DummyCountry",
54 logger.info("Complex created")
56 #### Create cloud region if not exists ####
57 logger.info("******** Cloud Region *******")
59 cloud_region = list(CloudRegion.get_all(cloud_owner=Config.CLOUD_OWNER, cloud_region_id=Config.CLOUD_REGION))[0]
60 logger.info("Cloud region exists")
61 except (IndexError, ResourceNotFound):
62 logger.info("Cloud region does not exists")
63 cloud_region = CloudRegion.create(cloud_owner=Config.CLOUD_OWNER,
64 cloud_region_id=Config.CLOUD_REGION,
66 owner_defined_type="t1",
67 cloud_region_version="1.0",
68 complex_name=complex.physical_location_id,
69 cloud_zone="CloudZone",
70 sriov_automation="false",
71 orchestration_disabled=False,
73 logger.info("Cloud region created")
75 logger.info("******** Cloud regiongion <-> Complex *******")
76 cloud_region.link_to_complex(complex)
78 logger.info("******** Availability zone *******")
80 az = cloud_region.get_availability_zone_by_name(Config.AVAILABILITY_ZONE_NAME)
81 logger.info("Availability zone exists")
82 except ResourceNotFound:
83 logger.info("Availability zone does not exist")
84 cloud_region.add_availability_zone(availability_zone_name=Config.AVAILABILITY_ZONE_NAME,
85 availability_zone_hypervisor_type=Config.HYPERVISOR_TYPE)
86 logger.info("Availability zone added to region")
88 logger.info("******** Tenant *******")
90 next(_tenant for _tenant in cloud_region.tenants if _tenant.name == Config.TENANT_NAME)
91 logger.info("Tenant exists")
92 except (StopIteration, ResourceNotFound):
93 tenant_id = str(uuid4())
94 logger.info("Tenant does not exist")
95 cloud_region.add_tenant(tenant_id=tenant_id,
96 tenant_name=Config.TENANT_NAME)
97 logger.info(f"Tenant {Config.TENANT_NAME} added to region")
99 #### Update or create connectivity info ####
100 logger.info("******** Connectivity Info *******")
101 with open(os.path.join(MYPATH, Config.CLUSTER_KUBECONFIG_PATH), 'rb') as kubeconfig_file:
102 kubeconfig = kubeconfig_file.read()
104 connectivity_info = ConnectivityInfo.get_connectivity_info_by_region_id(cloud_region_id=Config.CLOUD_REGION)
105 logger.info("Connectivity Info exists ")
106 logger.info("Delete Connectivity Info ")
107 connectivity_info.delete()
108 connectivity_info = ConnectivityInfo.create(cloud_region_id=Config.CLOUD_REGION,
109 cloud_owner=Config.CLOUD_OWNER,
110 kubeconfig=kubeconfig)
111 logger.info("Connectivity Info created ")
113 logger.info("Connectivity Info does not exists ")
114 connectivity_info = ConnectivityInfo.create(cloud_region_id=Config.CLOUD_REGION,
115 cloud_owner=Config.CLOUD_OWNER,
116 kubeconfig=kubeconfig)
117 logger.info("Connectivity Info created ")
119 #### Add Custom Resource Definitions ####
120 k8s_client = K8sClient(kubeconfig_path=Config.CLUSTER_KUBECONFIG_PATH)
121 for crd in Config.CUSTOMER_RESOURCE_DEFINITIONS:
122 k8s_client.create_custom_object(crd)
124 #### Create customer if not exists ####
125 logger.info("******** Customer *******")
127 customer = Customer.get_by_global_customer_id(Config.GLOBAL_CUSTOMER_ID)
128 logger.info("Customer exists")
130 logger.info("Customer exists")
131 customer = Customer.create(Config.GLOBAL_CUSTOMER_ID, Config.GLOBAL_CUSTOMER_ID, "INFRA")
132 logger.info("Customer created")
134 #### Add region to SO db ####
135 logger.info("******** SO Database *******")
136 result = SoDBUpdate.add_region_to_so_db(cloud_region_id=Config.CLOUD_REGION,
137 complex_id=Config.COMPLEX_ID)
138 if result.status_code == 201:
139 logger.info("Region in SO db created successfully")
141 logger.error("Creating region in SO db failed")