Real vFW CNF config-assign and config-deploy
[demo.git] / heat / vFW_CNF_CDS / automation / create_k8s_region.py
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
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 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
29 logger = logging.getLogger("")
30 logger.setLevel(logging.DEBUG)
31 fh = logging.StreamHandler()
32 fh_formatter = logging.Formatter('%(asctime)s %(levelname)s %(lineno)d:%(filename)s(%(process)d) - %(message)s')
33 fh.setFormatter(fh_formatter)
34 logger.addHandler(fh)
35
36 MYPATH = os.path.dirname(os.path.realpath(__file__))
37
38 #### Create complex if not exists ####
39 logger.info("******** Complex *******")
40 try:
41     complex = list(Complex.get_all(physical_location_id=Config.COMPLEX_ID))[0]
42     logger.info("Complex exists")
43 except IndexError:
44     logger.info("Complex does not exists")
45     complex = Complex.create(physical_location_id=Config.COMPLEX_ID,
46                              name=Config.COMPLEX_ID,
47                              physical_location_type="office",
48                              street1="DummyStreet 1",
49                              city="DummyCity",
50                              postal_code="00-000",
51                              country="DummyCountry",
52                              region="DummyRegion")
53     logger.info("Complex created")
54
55 #### Create cloud region if not exists ####
56 logger.info("******** Cloud Region *******")
57 try:
58     cloud_region = list(CloudRegion.get_all(cloud_owner=Config.CLOUD_OWNER, cloud_region_id=Config.CLOUD_REGION))[0]
59     logger.info("Cloud region exists")
60 except IndexError:
61     logger.info("Cloud region does not exists")
62     cloud_region = CloudRegion.create(cloud_owner=Config.CLOUD_OWNER,
63                                       cloud_region_id=Config.CLOUD_REGION,
64                                       cloud_type="k8s",
65                                       owner_defined_type="t1",
66                                       cloud_region_version="1.0",
67                                       complex_name=complex.physical_location_id,
68                                       cloud_zone="CloudZone",
69                                       sriov_automation="false",
70                                       orchestration_disabled=False,
71                                       in_maint=False)
72     logger.info("Cloud region created")
73
74 logger.info("******** Cloud regiongion <-> Complex *******")
75 cloud_region.link_to_complex(complex)
76
77 logger.info("******** Availability zone *******")
78 cloud_region.add_availability_zone(availability_zone_name=Config.AVAILABILITY_ZONE_NAME,
79                                    availability_zone_hypervisor_type=Config.HYPERVISOR_TYPE)
80
81 logger.info("******** Tenant *******")
82 cloud_region.add_tenant(str(uuid4()), Config.TENANT_NAME)
83
84 #### Update or create connectivity info ####
85 logger.info("******** Connectivity Info *******")
86 with open(os.path.join(MYPATH, Config.CLUSTER_KUBECONFIG_PATH), 'rb') as kubeconfig_file:
87     kubeconfig = kubeconfig_file.read()
88 try:
89     connectivity_info = ConnectivityInfo.get_connectivity_info_by_region_id(cloud_region_id=Config.CLOUD_REGION)
90     logger.info("Connectivity Info exists ")
91     logger.info("Delete Connectivity Info ")
92     connectivity_info.delete()
93     connectivity_info = ConnectivityInfo.create(cloud_region_id=Config.CLOUD_REGION,
94                                                 cloud_owner=Config.CLOUD_OWNER,
95                                                 kubeconfig=kubeconfig)
96     logger.info("Connectivity Info created ")
97 except:
98     logger.info("Connectivity Info does not exists ")
99     connectivity_info = ConnectivityInfo.create(cloud_region_id=Config.CLOUD_REGION,
100                                                 cloud_owner=Config.CLOUD_OWNER,
101                                                 kubeconfig=kubeconfig)
102     logger.info("Connectivity Info created ")
103
104 #### Add Custom Resource Definitions ####
105 k8s_client = K8sClient(kubeconfig_path=Config.CLUSTER_KUBECONFIG_PATH)
106 for crd in Config.CUSTOMER_RESOURCE_DEFINITIONS:
107     k8s_client.create_custom_object(crd)
108
109 #### Create customer if not exists ####
110 logger.info("******** Customer *******")
111 try:
112     customer = Customer.get_by_global_customer_id(Config.GLOBAL_CUSTOMER_ID)
113     logger.info("Customer exists")
114 except:
115     logger.info("Customer exists")
116     customer = Customer.create(Config.GLOBAL_CUSTOMER_ID, Config.GLOBAL_CUSTOMER_ID, "INFRA")
117     logger.info("Customer created")
118
119 #### Add region to SO db ####
120 logger.info("******** SO Database *******")
121 result = SoDBUpdate.add_region_to_so_db(cloud_region_id=Config.CLOUD_REGION,
122                                         complex_id=Config.COMPLEX_ID)
123 if result.status_code == 201:
124     logger.info("Region in SO db created successfully")
125 else:
126     logger.error("Creating region in SO db failed")