[vFW CNF CDS] Fix issue with multiple tenants creation for k8s region
[demo.git] / heat / vFW_CNF_CDS / automation / create_cloud_regions.py
1 # ============LICENSE_START=======================================================
2 # Copyright (C) 2021 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
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16 # ============LICENSE_END=========================================================
17
18 import logging
19 import os
20 from uuid import uuid4
21
22 from onapsdk.so.so_db_adapter import SoDbAdapter, IdentityService
23
24 from config import Config
25 from k8s_client import K8sClient
26 from onapsdk.exceptions import ResourceNotFound, APIError
27 from onapsdk.aai.business import Customer
28 from onapsdk.aai.cloud_infrastructure import Complex, CloudRegion
29 from onapsdk.msb.k8s import ConnectivityInfo
30
31 logger = logging.getLogger("")
32 logger.setLevel(logging.DEBUG)
33 fh = logging.StreamHandler()
34 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
35 fh.setFormatter(fh_formatter)
36 logger.addHandler(fh)
37
38 MYPATH = os.path.dirname(os.path.realpath(__file__))
39
40
41 def create_complex(region_id):
42     #### Create complex if not exists ####
43     logger.info("******** Complex *******")
44     complex_id = Config.CLOUD_REGIONS[region_id]["complex_id"]
45     try:
46         region_complex = next(Complex.get_all(physical_location_id=complex_id))
47         logger.info("Complex exists")
48     except ResourceNotFound:
49         logger.info("Complex does not exist")
50         region_complex = Complex.create(physical_location_id=complex_id,
51                                         name=complex_id,
52                                         physical_location_type="office",
53                                         street1="DummyStreet 1",
54                                         city="DummyCity",
55                                         postal_code="00-000",
56                                         country="DummyCountry",
57                                         region="DummyRegion")
58         logger.info("Complex created")
59         return region_complex
60     return region_complex
61
62
63 def create_cloud_region(region_id):
64     #### Create cloud region if not exists ####
65     logger.info("******** Cloud Region *******")
66     cloud_owner = Config.CLOUD_REGIONS[region_id]["cloud_owner"]
67     cloud_type = Config.CLOUD_REGIONS[region_id]["cloud_type"]
68     complex_id = Config.CLOUD_REGIONS[region_id]["complex_id"]
69     cloud_region_version = "1.0" if cloud_type == "k8s" else "v2.5"
70     try:
71         region = next(CloudRegion.get_all(cloud_owner=cloud_owner, cloud_region_id=region_id))
72         logger.info("Cloud region exists")
73     except ResourceNotFound:
74         logger.info("Cloud region does not exist")
75         region = CloudRegion.create(cloud_owner=cloud_owner,
76                                     cloud_region_id=region_id,
77                                     cloud_type=cloud_type,
78                                     owner_defined_type="t1",
79                                     cloud_region_version=cloud_region_version,
80                                     complex_name=complex_id,
81                                     cloud_zone="CloudZone",
82                                     sriov_automation="false",
83                                     orchestration_disabled=False,
84                                     in_maint=False)
85         logger.info("Cloud region created")
86         return region
87     return region
88
89
90 def link_region_to_complex(cloud_region, complx):
91     logger.info("******** Cloud region <-> Complex *******")
92     cloud_region.link_to_complex(complex_object=complx)
93
94
95 def add_availability_zone(cloud_region):
96     logger.info("******** Availability zone *******")
97     region_id = cloud_region.cloud_region_id
98     availability_zone_name = Config.CLOUD_REGIONS[region_id]["availability_zone"]
99     cloud_type = Config.CLOUD_REGIONS[region_id]["cloud_type"]
100     try:
101         cloud_region.get_availability_zone_by_name(availability_zone_name)
102         logger.info("Availability zone exists")
103     except ResourceNotFound:
104         logger.info("Availability zone does not exist")
105         cloud_region.add_availability_zone(availability_zone_name=availability_zone_name,
106                                            availability_zone_hypervisor_type=cloud_type)
107         logger.info("Availability zone added to region")
108
109
110 def add_tenant(cloud_region):
111     logger.info("******** Tenant *******")
112     region_id = cloud_region.cloud_region_id
113     is_k8s = is_k8s_region(region_id)
114     tenant_name = Config.CLOUD_REGIONS[region_id]["tenant"]["name"]
115
116     if is_k8s:
117         try:
118             next(_tenant for _tenant in cloud_region.tenants if _tenant.name == tenant_name)
119             logger.info("Tenant exists")
120         except (StopIteration, ResourceNotFound):
121             tenant_id = str(uuid4())
122             logger.info("Tenant does not exist")
123             cloud_region.add_tenant(tenant_id=tenant_id,
124                                     tenant_name=tenant_name)
125             logger.info(f"Tenant {tenant_name} added to region")
126     else:
127         tenant_id = Config.CLOUD_REGIONS[region_id]["tenant"]["id"]
128         try:
129             cloud_region.get_tenant(tenant_id)
130             logger.info("Tenant exists")
131         except ResourceNotFound:
132             logger.info("Tenant does not exist")
133             cloud_region.add_tenant(tenant_id=tenant_id,
134                                     tenant_name=tenant_name)
135             logger.info(f"Tenant {tenant_name} added to region")
136
137
138 def create_customer():
139     #### Create customer if not exists ####
140     logger.info("******** Customer *******")
141     try:
142         Customer.get_by_global_customer_id(Config.GLOBAL_CUSTOMER_ID)
143         logger.info("Customer exists")
144     except ResourceNotFound:
145         logger.info("Customer does not exist")
146         Customer.create(Config.GLOBAL_CUSTOMER_ID, Config.GLOBAL_CUSTOMER_ID, "INFRA")
147         logger.info("Customer created")
148
149
150 def update_connectivity_info(region_id):
151     #### Update or create connectivity info ####
152     logger.info("******** Connectivity Info *******")
153     cluster_kubeconfig_path = Config.CLOUD_REGIONS[region_id]["cluster_kubeconfig_file"]
154     cloud_owner = Config.CLOUD_REGIONS[region_id]["cloud_owner"]
155     with open(os.path.join(MYPATH, cluster_kubeconfig_path), 'rb') as kubeconfig_file:
156         kubeconfig = kubeconfig_file.read()
157     try:
158         connectivity_info = ConnectivityInfo.get_connectivity_info_by_region_id(cloud_region_id=region_id)
159         logger.info("Connectivity Info exists ")
160         logger.info("Delete Connectivity Info ")
161         connectivity_info.delete()
162         ConnectivityInfo.create(cloud_region_id=region_id,
163                                 cloud_owner=cloud_owner,
164                                 kubeconfig=kubeconfig)
165         logger.info("Connectivity Info created ")
166     except (APIError, ResourceNotFound):
167         logger.info("Connectivity Info does not exists ")
168         ConnectivityInfo.create(cloud_region_id=region_id,
169                                 cloud_owner=cloud_owner,
170                                 kubeconfig=kubeconfig)
171         logger.info("Connectivity Info created ")
172
173
174 def add_custom_resource_definitions(region_id):
175     #### Add Custom Resource Definitions ####
176     logger.info("******** Custom Resource Definitions *******")
177     cluster_kubeconfig_path = Config.CLOUD_REGIONS[region_id]["cluster_kubeconfig_file"]
178     k8s_client = K8sClient(kubeconfig_path=cluster_kubeconfig_path)
179     for crd in Config.CLOUD_REGIONS[region_id]["customer_resource_definitions"]:
180         k8s_client.create_custom_object(crd)
181
182
183 def add_region_to_so_db(region):
184     #### Add region to SO db ####
185     logger.info("******** SO Database *******")
186     if is_k8s_region(region.cloud_region_id):
187         identity_service = IdentityService(identity_id="Keystone_K8s",
188                                            url="http://test:5000/v3",
189                                            mso_id="onapsdk_user",
190                                            mso_pass="mso_pass_onapsdk",
191                                            project_domain_name="NULL",
192                                            user_domain_name="NULL",
193                                            identity_server_type="KEYSTONE")
194
195         SoDbAdapter.add_cloud_site(cloud_region_id=region.cloud_region_id,
196                                    complex_id=region.complex_name,
197                                    identity_service=identity_service,
198                                    orchestrator="multicloud")
199     else:
200         identity_url = Config.CLOUD_REGIONS[region.cloud_region_id]["identity_url"]
201         mso_id = Config.CLOUD_REGIONS[region.cloud_region_id]["mso_id"]
202         mso_pass = Config.CLOUD_REGIONS[region.cloud_region_id]["mso_pass"]
203         identity_server_type = Config.CLOUD_REGIONS[region.cloud_region_id]["identity_server_type"]
204         identity_service = IdentityService(identity_id=region.cloud_region_id + "_KEYSTONE",
205                                            url=identity_url,
206                                            mso_id=mso_id,
207                                            mso_pass=mso_pass,
208                                            project_domain_name="Default",
209                                            user_domain_name="Default",
210                                            identity_server_type=identity_server_type)
211
212         SoDbAdapter.add_cloud_site(cloud_region_id=region.cloud_region_id,
213                                    complex_id=region.complex_name,
214                                    identity_service=identity_service,
215                                    orchestrator="NULL")
216
217
218 def is_k8s_region(region_id):
219     is_k8s = False
220     if Config.CLOUD_REGIONS[region_id]["cloud_type"] == "k8s":
221         is_k8s = True
222     return is_k8s
223
224
225 ########################################################################################################################
226 def main():
227     create_customer()
228
229     for cloud_region_id in Config.CLOUD_REGIONS:
230
231         complx = create_complex(cloud_region_id)
232         cloud_region = create_cloud_region(cloud_region_id)
233         link_region_to_complex(cloud_region, complx)
234         add_availability_zone(cloud_region)
235         add_tenant(cloud_region)
236
237         if is_k8s_region(cloud_region_id):
238             update_connectivity_info(cloud_region_id)
239             add_custom_resource_definitions(cloud_region_id)
240
241         add_region_to_so_db(cloud_region)
242
243
244 if __name__ == "__main__":
245     main()