94d86019f8f50798cdcc8dd6550d1c7db3e8bdf3
[pti/o2.git] / o2ims / views / ocloud_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 uuid
16 import yaml
17 import random
18 import string
19 from datetime import datetime
20
21 from o2common.service import unit_of_work
22 from o2ims.views.ocloud_dto import SubscriptionDTO
23 from o2ims.domain.subscription_obj import Subscription
24
25 from o2common.helper import o2logging
26 from o2common.config import config
27 logger = o2logging.get_logger(__name__)
28
29
30 def oclouds(uow: unit_of_work.AbstractUnitOfWork):
31     with uow:
32         li = uow.oclouds.list()
33     return [r.serialize() for r in li]
34
35
36 def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork):
37     with uow:
38         first = uow.oclouds.get(ocloudid)
39         return first.serialize() if first is not None else None
40
41
42 def resource_types(uow: unit_of_work.AbstractUnitOfWork):
43     with uow:
44         li = uow.resource_types.list()
45     return [r.serialize() for r in li]
46
47
48 def resource_type_one(resourceTypeId: str,
49                       uow: unit_of_work.AbstractUnitOfWork):
50     with uow:
51         first = uow.resource_types.get(resourceTypeId)
52         return first.serialize() if first is not None else None
53
54
55 def resource_pools(uow: unit_of_work.AbstractUnitOfWork):
56     with uow:
57         li = uow.resource_pools.list()
58     return [r.serialize() for r in li]
59
60
61 def resource_pool_one(resourcePoolId: str,
62                       uow: unit_of_work.AbstractUnitOfWork):
63     with uow:
64         first = uow.resource_pools.get(resourcePoolId)
65         return first.serialize() if first is not None else None
66
67
68 def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork,
69               **kwargs):
70
71     filter_kwargs = {}  # filter key should be the same with database name
72     if 'resourceTypeName' in kwargs:
73         resource_type_name = kwargs['resourceTypeName']
74         with uow:
75             # res_types = uow.resource_types.list()
76             # restype_ids = [
77             #     restype.resourceTypeId for restype in res_types
78             #     if resourceTypeName == restype.name]
79             # restype_id = '' if len(restype_ids) == 0 else restype_ids[0]
80             res_type = uow.resource_types.get_by_name(resource_type_name)
81             restype_id = '' if res_type is None else res_type.resourceTypeId
82         filter_kwargs['resourceTypeId'] = restype_id
83
84         #     li = uow.resources.list(resourcePoolId)
85         # return [r.serialize() for r in li if r.resourceTypeId == restype_id]
86     if 'parentId' in kwargs:
87         filter_kwargs['parentId'] = kwargs['parentId']
88
89     with uow:
90         li = uow.resources.list(resourcePoolId, **filter_kwargs)
91     return [r.serialize() for r in li]
92
93
94 def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):
95     with uow:
96         first = uow.resources.get(resourceId)
97         return first.serialize() if first is not None else None
98
99
100 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):
101     with uow:
102         li = uow.deployment_managers.list()
103     return [r.serialize() for r in li]
104
105
106 def deployment_manager_one(deploymentManagerId: str,
107                            uow: unit_of_work.AbstractUnitOfWork,
108                            profile: str = 'default'):
109     profile = profile.lower()
110     with uow:
111         first = uow.deployment_managers.get(deploymentManagerId)
112         if first is None:
113             return first
114         result = first.serialize()
115         if result is None:
116             return None
117
118     profile_data = result.pop("profile", None)
119     result['profileName'] = 'default'
120
121     if "sol018" == profile:
122         result['profileName'] = profile
123         result['deploymentManagementServiceEndpoint'] = \
124             profile_data['cluster_api_endpoint']
125         result['profileData'] = profile_data
126     # elif "file" == profile and result.hasattr("profile"):
127         # p = result.pop("profile", None)
128         # result["profile"] = _gen_kube_config(deploymentManagerId, p)
129
130     return result
131
132
133 def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict:
134
135     data = config.gen_k8s_config_dict(
136         kubeconfig.pop('cluster_api_endpoint', None),
137         kubeconfig.pop('cluster_ca_cert', None),
138         kubeconfig.pop('admin_user', None),
139         kubeconfig.pop('admin_client_cert', None),
140         kubeconfig.pop('admin_client_key', None),
141     )
142
143     # Generate a random key for tmp kube config file
144     letters = string.ascii_uppercase
145     random_key = ''.join(random.choice(letters) for i in range(10))
146
147     # Get datetime of now as tag of the tmp file
148     current_time = datetime.now().strftime("%Y%m%d%H%M%S")
149     tmp_file_name = random_key + "_" + current_time
150
151     # write down the yaml file of kubectl into tmp folder
152     with open('/tmp/kubeconfig_' + tmp_file_name, 'w') as file:
153         yaml.dump(data, file)
154
155     kubeconfig["kube_config_file"] = config.get_api_url() + \
156         config.get_o2dms_api_base() + "/" + dmId + "/download/" + tmp_file_name
157
158     return kubeconfig
159
160
161 def subscriptions(uow: unit_of_work.AbstractUnitOfWork):
162     with uow:
163         li = uow.subscriptions.list()
164     return [r.serialize() for r in li]
165
166
167 def subscription_one(subscriptionId: str,
168                      uow: unit_of_work.AbstractUnitOfWork):
169     with uow:
170         first = uow.subscriptions.get(subscriptionId)
171         return first.serialize() if first is not None else None
172
173
174 def subscription_create(subscriptionDto: SubscriptionDTO.subscription,
175                         uow: unit_of_work.AbstractUnitOfWork):
176
177     sub_uuid = str(uuid.uuid4())
178     subscription = Subscription(
179         sub_uuid, subscriptionDto['callback'],
180         subscriptionDto['consumerSubscriptionId'],
181         subscriptionDto['filter'])
182     with uow:
183         uow.subscriptions.add(subscription)
184         uow.commit()
185     return {"subscriptionId": sub_uuid}
186
187
188 def subscription_delete(subscriptionId: str,
189                         uow: unit_of_work.AbstractUnitOfWork):
190     with uow:
191         uow.subscriptions.delete(subscriptionId)
192         uow.commit()
193     return True