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=========================================================
21 from kubernetes import config, client
22 from kubernetes.stream import stream
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__))
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()
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
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)
52 return password_bytes.decode('ascii')
54 def run_exec_request(self, exec_command):
55 response = stream(self.api_instance.connect_get_namespaced_pod_exec,
57 # container="container-name",
66 def check_region_in_db(self):
70 f"mysql -uroot -p{self.password} catalogdb -e 'SELECT * FROM cloud_sites;'"]
71 response = self.run_exec_request(exec_command)
73 is_region_found = False
74 for line in response.split("\n"):
75 if line.split("\t")[0] == self.CLOUD_REGION_ID:
77 is_region_found = True
78 return is_region_found
79 return is_region_found
81 def add_region_to_so_db(self):
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\");'"]
90 response = self.run_exec_request(exec_command)