Docs: Enable O2 DMS by exposing k8s API endpoint
[pti/o2.git] / o2dms / api / dms_lcm_view.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 import string
16 import random
17 import yaml
18 from datetime import datetime
19
20 # from sqlalchemy import select
21 from o2common.service import unit_of_work
22 # from o2ims.adapter.orm import deploymentmanager
23 from o2common.helper import o2logging
24 from o2common.config import config
25 logger = o2logging.get_logger(__name__)
26
27
28 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):
29     # with uow:
30     # res = uow.session.execute(select(deploymentmanager))
31     # return [dict(r) for r in res]
32     with uow:
33         li = uow.deployment_managers.list()
34     return [r.serialize() for r in li]
35
36
37 def deployment_manager_one(deploymentManagerId: str,
38                            uow: unit_of_work.AbstractUnitOfWork):
39
40     # with uow:
41     #     res = uow.session.execute(select(deploymentmanager).where(
42     #         deploymentmanager.c.deploymentManagerId == deploymentManagerId))
43     #     first = res.first()
44     #     return None if first is None else dict(first)
45     with uow:
46         first = uow.deployment_managers.get(deploymentManagerId)
47         return first.serialize() if first is not None else None
48
49     # profile = profile.lower()
50     # with uow:
51     #     first = uow.deployment_managers.get(deploymentManagerId)
52     #     if first is None:
53     #         return first
54     #     result = first.serialize()
55
56     # profile_data = result.pop("profile", None)
57     # result['profileName'] = 'default'
58
59     # if "sol0018" == profile:
60     #     result['profileName'] = profile
61     #     result['profileData'] = profile_data
62     # elif "file" == profile and result.hasattr("profile"):
63     # p = result.pop("profile", None)
64     # result["profile"] = _gen_kube_config(deploymentManagerId, p)
65
66     # return result
67
68
69 def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict:
70
71     data = config.gen_k8s_config_dict(
72         kubeconfig.pop('cluster_api_endpoint', None),
73         kubeconfig.pop('cluster_ca_cert', None),
74         kubeconfig.pop('admin_user', None),
75         kubeconfig.pop('admin_client_cert', None),
76         kubeconfig.pop('admin_client_key', None),
77     )
78
79     # Generate a random key for tmp kube config file
80     letters = string.ascii_uppercase
81     random_key = ''.join(random.choice(letters) for i in range(10))
82
83     # Get datetime of now as tag of the tmp file
84     current_time = datetime.now().strftime("%Y%m%d%H%M%S")
85     tmp_file_name = random_key + "_" + current_time
86
87     # write down the yaml file of kubectl into tmp folder
88     with open('/tmp/kubeconfig_' + tmp_file_name, 'w') as file:
89         yaml.dump(data, file)
90
91     kubeconfig["kube_config_file"] = config.get_api_url() + \
92         config.get_o2dms_api_base() + "/" + dmId + "/download/" + tmp_file_name
93
94     return kubeconfig