a829fbda463cd39dd49a4ad44bfdca9f824ec156
[demo.git] / heat / vFW_CNF_CDS / automation / so_db_adapter.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 base64
19 import os
20
21 from kubernetes import config, client
22 from kubernetes.stream import stream
23
24
25 class SoDBAdapter:
26
27     def __init__(self, cloud_region_id, complex_id, onap_kubeconfig_path):
28         self.CLOUD_REGION_ID = cloud_region_id
29         self.COMPLEX_ID = complex_id
30         self.ONAP_KUBECONFIG_PATH = onap_kubeconfig_path
31         self.MYPATH = os.path.dirname(os.path.realpath(__file__))
32
33         config.load_kube_config(config_file=os.path.join(self.MYPATH, self.ONAP_KUBECONFIG_PATH))
34         self.api_instance = client.CoreV1Api()
35         self.pod_name = self.get_mariadb_pod_name()
36         self.password = self.get_mariadb_root_username_password()
37
38     def get_mariadb_pod_name(self):
39         pods = self.api_instance.list_namespaced_pod(namespace="onap")
40         for pod in pods.items:
41             if pod.metadata.name.find("mariadb-galera-0") != -1:
42                 return pod.metadata.name
43
44     def get_mariadb_root_username_password(self):
45         secrets = self.api_instance.list_namespaced_secret(namespace="onap")
46         for secret in secrets.items:
47             if secret.metadata.name.find("mariadb-galera-db-root-password") != -1:
48                 base64_password = secret.data["password"]
49                 base64_bytes = base64_password.encode('ascii')
50                 password_bytes = base64.b64decode(base64_bytes)
51
52                 return password_bytes.decode('ascii')
53
54     def run_exec_request(self, exec_command):
55         response = stream(self.api_instance.connect_get_namespaced_pod_exec,
56                           name=self.pod_name,
57                           # container="container-name",
58                           namespace="onap",
59                           command=exec_command,
60                           stdin=False,
61                           tty=False,
62                           stderr=True,
63                           stdout=True)
64         return response
65
66     def check_region_in_db(self):
67         exec_command = [
68             "/bin/sh",
69             "-c",
70             f"mysql -uroot -p{self.password} catalogdb -e 'SELECT * FROM cloud_sites;'"]
71         response = self.run_exec_request(exec_command)
72
73         is_region_found = False
74         for line in response.split("\n"):
75             if line.split("\t")[0] == self.CLOUD_REGION_ID:
76                 print(line)
77                 is_region_found = True
78                 return is_region_found
79         return is_region_found
80
81     def add_region_to_so_db(self):
82         exec_command = [
83             "/bin/sh",
84             "-c",
85             f"mysql -uroot -p{self.password} catalogdb -e "
86             f"'insert into cloud_sites(ID, REGION_ID, IDENTITY_SERVICE_ID, CLOUD_VERSION, CLLI, ORCHESTRATOR ) "
87             f"values (\"{self.CLOUD_REGION_ID}\", \"{self.CLOUD_REGION_ID}\", \"DEFAULT_KEYSTONE\", \"2.5\", "
88             f"\"{self.COMPLEX_ID}\", \"multicloud\");'"]
89
90         response = self.run_exec_request(exec_command)
91
92         return response