blob: 572b90289143b460e7b8d87ddcf81f12f237f2e6 [file] [log] [blame]
rshri932105f2024-07-05 15:11:55 +00001# -*- coding: utf-8 -*-
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
12# implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16__author__ = (
17 "Shrinithi R <shrinithi.r@tataelxsi.co.in>",
18 "Shahithya Y <shahithya.y@tataelxsi.co.in>",
19)
20
21import logging
yshahd940c652024-10-17 06:11:12 +000022from time import time
rshri932105f2024-07-05 15:11:55 +000023from osm_lcm.lcm_utils import LcmBase
24from copy import deepcopy
25from osm_lcm import odu_workflows
26from osm_lcm import vim_sdn
27
28
29class ClusterLcm(LcmBase):
garciadeblas96b94f52024-07-08 16:18:21 +020030 db_collection = "clusters"
rshri932105f2024-07-05 15:11:55 +000031
32 def __init__(self, msg, lcm_tasks, config):
33 """
34 Init, Connect to database, filesystem storage, and messaging
35 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
36 :return: None
37 """
38
garciadeblas40539872024-09-11 14:28:38 +020039 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +000040 self.lcm_tasks = lcm_tasks
41 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
42 self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config)
43
44 super().__init__(msg, self.logger)
45
garciadeblas96b94f52024-07-08 16:18:21 +020046 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +000047 self.logger.info("cluster Create Enter")
garciadeblas96b94f52024-07-08 16:18:21 +020048 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +000049
garciadeblas96b94f52024-07-08 16:18:21 +020050 workflow_name = await self.odu.launch_workflow(
51 "create_cluster", op_id, op_params, content
52 )
rshri932105f2024-07-05 15:11:55 +000053 self.logger.info("workflow_name is :{}".format(workflow_name))
54
garciadeblas96b94f52024-07-08 16:18:21 +020055 workflow_status, workflow_msg = await self.odu.check_workflow_status(
56 workflow_name
57 )
rshri932105f2024-07-05 15:11:55 +000058 self.logger.info(
59 "workflow_status is :{} and workflow_msg is :{}".format(
60 workflow_status, workflow_msg
61 )
62 )
63 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +020064 db_cluster["state"] = "CREATED"
65 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +000066 else:
garciadeblas96b94f52024-07-08 16:18:21 +020067 db_cluster["state"] = "FAILED_CREATION"
68 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +000069 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +020070 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
71 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +000072
garciadeblas28bff0f2024-09-16 12:53:07 +020073 # Clean items used in the workflow, no matter if the workflow succeeded
74 clean_status, clean_msg = await self.odu.clean_items_workflow(
75 "create_cluster", op_id, op_params, content
76 )
77 self.logger.info(
78 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
79 )
80
rshri932105f2024-07-05 15:11:55 +000081 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +020082 resource_status, resource_msg = await self.odu.check_resource_status(
83 "create_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +000084 )
85 self.logger.info(
86 "resource_status is :{} and resource_msg is :{}".format(
87 resource_status, resource_msg
88 )
89 )
90 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +020091 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +000092 else:
garciadeblas96b94f52024-07-08 16:18:21 +020093 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +000094
garciadeblas96b94f52024-07-08 16:18:21 +020095 db_cluster["operatingState"] = "IDLE"
96 db_cluster = self.update_operation_history(
97 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +000098 )
garciadeblas96b94f52024-07-08 16:18:21 +020099 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
100 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000101 return
102
garciadeblas96b94f52024-07-08 16:18:21 +0200103 def update_profile_state(self, db_cluster, workflow_status, resource_status):
rshri932105f2024-07-05 15:11:55 +0000104 profiles = [
105 "infra_controller_profiles",
106 "infra_config_profiles",
107 "app_profiles",
108 "resource_profiles",
109 ]
110 profiles_collection = {
111 "infra_controller_profiles": "k8sinfra_controller",
112 "infra_config_profiles": "k8sinfra_config",
113 "app_profiles": "k8sapp",
114 "resource_profiles": "k8sresource",
115 }
116 for profile_type in profiles:
garciadeblas96b94f52024-07-08 16:18:21 +0200117 profile_id = db_cluster[profile_type]
rshri932105f2024-07-05 15:11:55 +0000118 self.logger.info("profile id is : {}".format(profile_id))
119 db_collection = profiles_collection[profile_type]
120 self.logger.info("the db_collection is :{}".format(db_collection))
121 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
122 self.logger.info("the db_profile is :{}".format(db_profile))
garciadeblas96b94f52024-07-08 16:18:21 +0200123 db_profile["state"] = db_cluster["state"]
124 db_profile["resourceState"] = db_cluster["resourceState"]
125 db_profile["operatingState"] = db_cluster["operatingState"]
rshri932105f2024-07-05 15:11:55 +0000126 db_profile = self.update_operation_history(
127 db_profile, workflow_status, resource_status
128 )
129 self.logger.info("the db_profile is :{}".format(db_profile))
130 self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile)
131
garciadeblas96b94f52024-07-08 16:18:21 +0200132 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000133 self.logger.info("cluster delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200134 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000135
garciadeblas96b94f52024-07-08 16:18:21 +0200136 workflow_name = await self.odu.launch_workflow(
137 "delete_cluster", op_id, op_params, content
138 )
rshri932105f2024-07-05 15:11:55 +0000139 self.logger.info("workflow_name is :{}".format(workflow_name))
140
garciadeblas96b94f52024-07-08 16:18:21 +0200141 workflow_status, workflow_msg = await self.odu.check_workflow_status(
142 workflow_name
143 )
rshri932105f2024-07-05 15:11:55 +0000144 self.logger.info(
145 "workflow_status is :{} and workflow_msg is :{}".format(
146 workflow_status, workflow_msg
147 )
148 )
149 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200150 db_cluster["state"] = "DELETED"
151 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000152 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200153 db_cluster["state"] = "FAILED_DELETION"
154 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000155 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200156 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
157 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000158
159 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200160 resource_status, resource_msg = await self.odu.check_resource_status(
161 "delete_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000162 )
163 self.logger.info(
164 "resource_status is :{} and resource_msg is :{}".format(
165 resource_status, resource_msg
166 )
167 )
168 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200169 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000170 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200171 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000172
garciadeblas96b94f52024-07-08 16:18:21 +0200173 db_cluster["operatingState"] = "IDLE"
174 db_cluster = self.update_operation_history(
175 db_cluster, workflow_status, resource_status
176 )
177 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000178
garciadeblas96b94f52024-07-08 16:18:21 +0200179 # To delete it from DB
180 if db_cluster["state"] == "DELETED":
181 self.delete_cluster(db_cluster)
rshri932105f2024-07-05 15:11:55 +0000182 return
183
garciadeblas96b94f52024-07-08 16:18:21 +0200184 def delete_cluster(self, db_cluster):
185 # Actually, item_content is equal to db_cluster
186 # item_content = self.db.get_one("clusters", {"_id": db_cluster["_id"]})
187 # self.logger.debug("item_content is : {}".format(item_content))
rshri932105f2024-07-05 15:11:55 +0000188
rshri932105f2024-07-05 15:11:55 +0000189 # detach profiles
190 update_dict = None
191 profiles_to_detach = [
192 "infra_controller_profiles",
193 "infra_config_profiles",
194 "app_profiles",
195 "resource_profiles",
196 ]
197 profiles_collection = {
198 "infra_controller_profiles": "k8sinfra_controller",
199 "infra_config_profiles": "k8sinfra_config",
200 "app_profiles": "k8sapp",
201 "resource_profiles": "k8sresource",
202 }
203 for profile_type in profiles_to_detach:
garciadeblas96b94f52024-07-08 16:18:21 +0200204 if db_cluster.get(profile_type):
garciadeblasc2552852024-10-22 12:39:32 +0200205 self.logger.debug("the profile_type is :{}".format(profile_type))
garciadeblas96b94f52024-07-08 16:18:21 +0200206 profile_ids = db_cluster[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200207 self.logger.debug("the profile_ids is :{}".format(profile_ids))
rshri932105f2024-07-05 15:11:55 +0000208 profile_ids_copy = deepcopy(profile_ids)
garciadeblasc2552852024-10-22 12:39:32 +0200209 self.logger.debug(
210 "the profile_ids_copy is :{}".format(profile_ids_copy)
211 )
rshri932105f2024-07-05 15:11:55 +0000212 for profile_id in profile_ids_copy:
garciadeblasc2552852024-10-22 12:39:32 +0200213 self.logger.debug("the profile_id is :{}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000214 db_collection = profiles_collection[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200215 self.logger.debug("the db_collection is :{}".format(db_collection))
rshri932105f2024-07-05 15:11:55 +0000216 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
garciadeblasc2552852024-10-22 12:39:32 +0200217 self.logger.debug("the db_profile is :{}".format(db_profile))
218 self.logger.debug(
garciadeblas96b94f52024-07-08 16:18:21 +0200219 "the item_content name is :{}".format(db_cluster["name"])
rshri932105f2024-07-05 15:11:55 +0000220 )
garciadeblasc2552852024-10-22 12:39:32 +0200221 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000222 "the db_profile name is :{}".format(db_profile["name"])
223 )
garciadeblas96b94f52024-07-08 16:18:21 +0200224 if db_cluster["name"] == db_profile["name"]:
garciadeblasc2552852024-10-22 12:39:32 +0200225 self.logger.debug("it is getting into if default")
rshri932105f2024-07-05 15:11:55 +0000226 self.db.del_one(db_collection, {"_id": profile_id})
227 else:
garciadeblasc2552852024-10-22 12:39:32 +0200228 self.logger.debug("it is getting into else non default")
rshri932105f2024-07-05 15:11:55 +0000229 profile_ids.remove(profile_id)
230 update_dict = {profile_type: profile_ids}
garciadeblasc2552852024-10-22 12:39:32 +0200231 self.logger.debug(f"the update dict is :{update_dict}")
rshri932105f2024-07-05 15:11:55 +0000232 self.db.set_one(
garciadeblas96b94f52024-07-08 16:18:21 +0200233 "clusters", {"_id": db_cluster["_id"]}, update_dict
rshri932105f2024-07-05 15:11:55 +0000234 )
garciadeblas96b94f52024-07-08 16:18:21 +0200235 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
garciadeblasc2552852024-10-22 12:39:32 +0200236 self.logger.debug("the id is :{}".format(db_cluster["_id"]))
rshri932105f2024-07-05 15:11:55 +0000237
garciadeblas96b94f52024-07-08 16:18:21 +0200238 async def attach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000239 self.logger.info("profile attach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200240 db_cluster = content["cluster"]
241 db_profile = content["profile"]
242 profile_type = db_profile["profile_type"]
243 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000244 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000245 self.logger.info("profile id is : {}".format(profile_id))
246
garciadeblas96b94f52024-07-08 16:18:21 +0200247 workflow_name = await self.odu.launch_workflow(
248 "attach_profile_to_cluster", op_id, op_params, content
249 )
rshri932105f2024-07-05 15:11:55 +0000250 self.logger.info("workflow_name is :{}".format(workflow_name))
251
garciadeblas96b94f52024-07-08 16:18:21 +0200252 workflow_status, workflow_msg = await self.odu.check_workflow_status(
253 workflow_name
254 )
rshri932105f2024-07-05 15:11:55 +0000255 self.logger.info(
256 "workflow_status is :{} and workflow_msg is :{}".format(
257 workflow_status, workflow_msg
258 )
259 )
260 if workflow_status:
261 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
262 else:
263 db_cluster["resourceState"] = "ERROR"
264 # has to call update_operation_history return content
265 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
266 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
267
268 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200269 resource_status, resource_msg = await self.odu.check_resource_status(
270 "attach_profile_to_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000271 )
272 self.logger.info(
273 "resource_status is :{} and resource_msg is :{}".format(
274 resource_status, resource_msg
275 )
276 )
277 if resource_status:
278 db_cluster["resourceState"] = "READY"
279 else:
280 db_cluster["resourceState"] = "ERROR"
281
282 db_cluster["operatingState"] = "IDLE"
283 db_cluster = self.update_operation_history(
284 db_cluster, workflow_status, resource_status
285 )
rshri932105f2024-07-05 15:11:55 +0000286 profile_list = db_cluster[profile_type]
287 self.logger.info("profile list is : {}".format(profile_list))
288 if resource_status:
289 self.logger.info("it is getting into resource status true")
290 profile_list.append(profile_id)
291 self.logger.info("profile list is : {}".format(profile_list))
292 db_cluster[profile_type] = profile_list
293 self.logger.info("db cluster is : {}".format(db_cluster))
294 # update_dict = {item: profile_list}
295 # self.logger.info("the update_dict is :{}".format(update_dict))
296 # self.db.set_one(self.topic, filter_q, update_dict)
297 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
298
299 return
300
garciadeblas96b94f52024-07-08 16:18:21 +0200301 async def detach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000302 self.logger.info("profile dettach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200303 db_cluster = content["cluster"]
304 db_profile = content["profile"]
305 profile_type = db_profile["profile_type"]
306 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000307 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000308 self.logger.info("profile id is : {}".format(profile_id))
309
garciadeblas96b94f52024-07-08 16:18:21 +0200310 workflow_name = await self.odu.launch_workflow(
311 "detach_profile_from_cluster", op_id, op_params, content
312 )
rshri932105f2024-07-05 15:11:55 +0000313 self.logger.info("workflow_name is :{}".format(workflow_name))
314
garciadeblas96b94f52024-07-08 16:18:21 +0200315 workflow_status, workflow_msg = await self.odu.check_workflow_status(
316 workflow_name
317 )
rshri932105f2024-07-05 15:11:55 +0000318 self.logger.info(
319 "workflow_status is :{} and workflow_msg is :{}".format(
320 workflow_status, workflow_msg
321 )
322 )
323 if workflow_status:
324 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
325 else:
326 db_cluster["resourceState"] = "ERROR"
327 # has to call update_operation_history return content
328 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
329 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
330
331 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200332 resource_status, resource_msg = await self.odu.check_resource_status(
333 "detach_profile_from_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000334 )
335 self.logger.info(
336 "resource_status is :{} and resource_msg is :{}".format(
337 resource_status, resource_msg
338 )
339 )
340 if resource_status:
341 db_cluster["resourceState"] = "READY"
342 else:
343 db_cluster["resourceState"] = "ERROR"
344
345 db_cluster["operatingState"] = "IDLE"
346 db_cluster = self.update_operation_history(
347 db_cluster, workflow_status, resource_status
348 )
rshri932105f2024-07-05 15:11:55 +0000349 profile_list = db_cluster[profile_type]
350 self.logger.info("profile list is : {}".format(profile_list))
351 if resource_status:
352 self.logger.info("it is getting into resource status true")
353 profile_list.remove(profile_id)
354 self.logger.info("profile list is : {}".format(profile_list))
355 db_cluster[profile_type] = profile_list
356 self.logger.info("db cluster is : {}".format(db_cluster))
357 # update_dict = {item: profile_list}
358 # self.logger.info("the update_dict is :{}".format(update_dict))
359 # self.db.set_one(self.topic, filter_q, update_dict)
360 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
361
362 return
363
garciadeblas96b94f52024-07-08 16:18:21 +0200364 async def register(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000365 self.logger.info("cluster register enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200366 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000367
garciadeblas96b94f52024-07-08 16:18:21 +0200368 workflow_name = await self.odu.launch_workflow(
369 "register_cluster", op_id, op_params, content
370 )
rshri932105f2024-07-05 15:11:55 +0000371 self.logger.info("workflow_name is :{}".format(workflow_name))
372
garciadeblas96b94f52024-07-08 16:18:21 +0200373 workflow_status, workflow_msg = await self.odu.check_workflow_status(
374 workflow_name
375 )
rshri932105f2024-07-05 15:11:55 +0000376 self.logger.info(
377 "workflow_status is :{} and workflow_msg is :{}".format(
378 workflow_status, workflow_msg
379 )
380 )
381 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200382 db_cluster["state"] = "CREATED"
383 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000384 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200385 db_cluster["state"] = "FAILED_CREATION"
386 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000387 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200388 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
389 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000390
garciadeblasdde3a312024-09-17 13:25:06 +0200391 # Clean items used in the workflow, no matter if the workflow succeeded
392 clean_status, clean_msg = await self.odu.clean_items_workflow(
393 "register_cluster", op_id, op_params, content
394 )
395 self.logger.info(
396 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
397 )
398
rshri932105f2024-07-05 15:11:55 +0000399 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200400 resource_status, resource_msg = await self.odu.check_resource_status(
401 "register_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000402 )
403 self.logger.info(
404 "resource_status is :{} and resource_msg is :{}".format(
405 resource_status, resource_msg
406 )
407 )
408 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200409 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000410 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200411 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000412
garciadeblas96b94f52024-07-08 16:18:21 +0200413 db_cluster["operatingState"] = "IDLE"
414 db_cluster = self.update_operation_history(
415 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000416 )
garciadeblas96b94f52024-07-08 16:18:21 +0200417 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
418 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000419 return
420
garciadeblas96b94f52024-07-08 16:18:21 +0200421 async def deregister(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000422 self.logger.info("cluster deregister enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200423 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000424
garciadeblas96b94f52024-07-08 16:18:21 +0200425 self.logger.info("db_cluster is : {}".format(db_cluster))
rshri932105f2024-07-05 15:11:55 +0000426
garciadeblas96b94f52024-07-08 16:18:21 +0200427 workflow_name = await self.odu.launch_workflow(
428 "deregister_cluster", op_id, op_params, content
429 )
rshri932105f2024-07-05 15:11:55 +0000430 self.logger.info("workflow_name is :{}".format(workflow_name))
431
garciadeblas96b94f52024-07-08 16:18:21 +0200432 workflow_status, workflow_msg = await self.odu.check_workflow_status(
433 workflow_name
434 )
rshri932105f2024-07-05 15:11:55 +0000435 self.logger.info(
436 "workflow_status is :{} and workflow_msg is :{}".format(
437 workflow_status, workflow_msg
438 )
439 )
440 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200441 db_cluster["state"] = "DELETED"
442 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000443 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200444 db_cluster["state"] = "FAILED_DELETION"
445 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000446 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200447 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
448 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000449
450 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200451 resource_status, resource_msg = await self.odu.check_resource_status(
452 "deregister_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000453 )
454 self.logger.info(
455 "resource_status is :{} and resource_msg is :{}".format(
456 resource_status, resource_msg
457 )
458 )
459 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200460 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000461 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200462 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000463
garciadeblas96b94f52024-07-08 16:18:21 +0200464 db_cluster["operatingState"] = "IDLE"
465 db_cluster = self.update_operation_history(
466 db_cluster, workflow_status, resource_status
467 )
468 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000469
garciadeblas96b94f52024-07-08 16:18:21 +0200470 # To delete it from DB
471 if db_cluster["state"] == "DELETED":
472 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
rshri932105f2024-07-05 15:11:55 +0000473 return
474
yshahd940c652024-10-17 06:11:12 +0000475 async def get_creds(self, op_id, db_cluster):
garciadeblas96b94f52024-07-08 16:18:21 +0200476 self.logger.info("Cluster get creds Enter")
477 result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster)
478 if result:
479 db_cluster["credentials"] = cluster_creds
yshahd940c652024-10-17 06:11:12 +0000480 op_len = 0
481 for operations in db_cluster["operationHistory"]:
482 if operations["op_id"] == op_id:
483 db_cluster["operationHistory"][op_len]["result"] = result
484 db_cluster["operationHistory"][op_len]["endDate"] = time()
485 op_len += 1
486 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000487 return
488
garciadeblas96b94f52024-07-08 16:18:21 +0200489 async def update(self, op_id, op_params, content):
490 self.logger.info("Cluster update Enter")
491 db_cluster = content["cluster"]
yshah771dea82024-07-05 15:11:49 +0000492
garciadeblas96b94f52024-07-08 16:18:21 +0200493 workflow_name = await self.odu.launch_workflow(
494 "update_cluster", op_id, op_params, content
495 )
496 workflow_status, workflow_msg = await self.odu.check_workflow_status(
497 workflow_name
498 )
499 self.logger.info(
500 "Workflow Status: {} Workflow Message: {}".format(
501 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +0000502 )
garciadeblas96b94f52024-07-08 16:18:21 +0200503 )
504
505 if workflow_status:
506 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
507 else:
508 db_cluster["resourceState"] = "ERROR"
509
510 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
511 # self.logger.info("Db content: {}".format(db_content))
512 # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster)
513 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
514
garciadeblas28bff0f2024-09-16 12:53:07 +0200515 # Clean items used in the workflow, no matter if the workflow succeeded
516 clean_status, clean_msg = await self.odu.clean_items_workflow(
517 "update_cluster", op_id, op_params, content
518 )
519 self.logger.info(
520 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
521 )
garciadeblas96b94f52024-07-08 16:18:21 +0200522 if workflow_status:
523 resource_status, resource_msg = await self.odu.check_resource_status(
524 "update_cluster", op_id, op_params, content
525 )
526 self.logger.info(
527 "Resource Status: {} Resource Message: {}".format(
528 resource_status, resource_msg
529 )
530 )
yshah771dea82024-07-05 15:11:49 +0000531
532 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200533 db_cluster["resourceState"] = "READY"
yshah771dea82024-07-05 15:11:49 +0000534 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200535 db_cluster["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +0000536
garciadeblas96b94f52024-07-08 16:18:21 +0200537 db_cluster["operatingState"] = "IDLE"
538 db_cluster = self.update_operation_history(
539 db_cluster, workflow_status, resource_status
540 )
541 # self.logger.info("db_cluster: {}".format(db_cluster))
542 # TODO: verify enxtcondition
543 # For the moment, if the workflow completed successfully, then we update the db accordingly.
544 if workflow_status:
545 if "k8s_version" in op_params:
546 db_cluster["k8s_version"] = op_params["k8s_version"]
547 elif "node_count" in op_params:
548 db_cluster["node_count"] = op_params["node_count"]
549 # self.db.set_one(self.db_collection, {"_id": _id}, db_content)
550 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000551 return
552
553
554class CloudCredentialsLcm(LcmBase):
555 db_collection = "vim_accounts"
556
557 def __init__(self, msg, lcm_tasks, config):
558 """
559 Init, Connect to database, filesystem storage, and messaging
560 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
561 :return: None
562 """
563
garciadeblas40539872024-09-11 14:28:38 +0200564 self.logger = logging.getLogger("lcm.gitops")
yshah771dea82024-07-05 15:11:49 +0000565 self.lcm_tasks = lcm_tasks
566 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
567
568 super().__init__(msg, self.logger)
569
garciadeblas96b94f52024-07-08 16:18:21 +0200570 async def add(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000571 self.logger.info("Cloud Credentials create")
572 workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200573 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000574 )
575
576 workflow_status, workflow_msg = await self.odu.check_workflow_status(
577 workflow_name
578 )
579
580 self.logger.info(
581 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
582 )
583
garciadeblas28bff0f2024-09-16 12:53:07 +0200584 # Clean items used in the workflow, no matter if the workflow succeeded
585 clean_status, clean_msg = await self.odu.clean_items_workflow(
586 "create_cloud_credentials", op_id, op_params, content
587 )
588 self.logger.info(
589 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
590 )
591
yshah771dea82024-07-05 15:11:49 +0000592 if workflow_status:
593 resource_status, resource_msg = await self.odu.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200594 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000595 )
596 self.logger.info(
597 "Resource Status: {} Resource Message: {}".format(
598 resource_status, resource_msg
599 )
600 )
garciadeblas15b8a302024-09-23 12:40:13 +0200601
602 content["_admin"]["operationalState"] = "ENABLED"
603 for operation in content["_admin"]["operations"]:
604 if operation["lcmOperationType"] == "create":
605 operation["operationState"] = "ENABLED"
606 self.logger.info("Content : {}".format(content))
607 self.db.set_one("vim_accounts", {"_id": content["_id"]}, content)
608
yshah771dea82024-07-05 15:11:49 +0000609 return
610
garciadeblas96b94f52024-07-08 16:18:21 +0200611 async def edit(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000612 workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200613 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000614 )
615 workflow_status, workflow_msg = await self.odu.check_workflow_status(
616 workflow_name
617 )
618 self.logger.info(
619 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
620 )
621
garciadeblas28bff0f2024-09-16 12:53:07 +0200622 # Clean items used in the workflow, no matter if the workflow succeeded
623 clean_status, clean_msg = await self.odu.clean_items_workflow(
624 "update_cloud_credentials", op_id, op_params, content
625 )
626 self.logger.info(
627 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
628 )
629
yshah771dea82024-07-05 15:11:49 +0000630 if workflow_status:
631 resource_status, resource_msg = await self.odu.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200632 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000633 )
634 self.logger.info(
635 "Resource Status: {} Resource Message: {}".format(
636 resource_status, resource_msg
637 )
638 )
639 return
640
garciadeblas96b94f52024-07-08 16:18:21 +0200641 async def remove(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000642 self.logger.info("Cloud Credentials delete")
643 workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200644 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000645 )
646 workflow_status, workflow_msg = await self.odu.check_workflow_status(
647 workflow_name
648 )
649 self.logger.info(
650 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
651 )
652
653 if workflow_status:
654 resource_status, resource_msg = await self.odu.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200655 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000656 )
657 self.logger.info(
658 "Resource Status: {} Resource Message: {}".format(
659 resource_status, resource_msg
660 )
661 )
662 self.db.del_one(self.db_collection, {"_id": content["_id"]})
663 return
664
rshri932105f2024-07-05 15:11:55 +0000665
666class K8sAppLcm(LcmBase):
667 def __init__(self, msg, lcm_tasks, config):
668 """
669 Init, Connect to database, filesystem storage, and messaging
670 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
671 :return: None
672 """
673
garciadeblas40539872024-09-11 14:28:38 +0200674 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +0000675 self.lcm_tasks = lcm_tasks
676 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
677
678 super().__init__(msg, self.logger)
679
garciadeblas96b94f52024-07-08 16:18:21 +0200680 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000681 self.logger.info("App Create Enter")
682
garciadeblas96b94f52024-07-08 16:18:21 +0200683 workflow_name = await self.odu.launch_workflow(
684 "create_profile", op_id, op_params, content
685 )
rshri932105f2024-07-05 15:11:55 +0000686 self.logger.info("workflow_name is :{}".format(workflow_name))
687
garciadeblas96b94f52024-07-08 16:18:21 +0200688 workflow_status, workflow_msg = await self.odu.check_workflow_status(
689 workflow_name
690 )
rshri932105f2024-07-05 15:11:55 +0000691 self.logger.info(
692 "workflow_status is :{} and workflow_msg is :{}".format(
693 workflow_status, workflow_msg
694 )
695 )
696 if workflow_status:
697 content["state"] = "CREATED"
698 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
699 else:
700 content["state"] = "FAILED_CREATION"
701 content["resourceState"] = "ERROR"
702 # has to call update_operation_history return content
703 content = self.update_operation_history(content, workflow_status, None)
704 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
705
706 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200707 resource_status, resource_msg = await self.odu.check_resource_status(
708 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000709 )
710 self.logger.info(
711 "resource_status is :{} and resource_msg is :{}".format(
712 resource_status, resource_msg
713 )
714 )
715 if resource_status:
716 content["resourceState"] = "READY"
717 else:
718 content["resourceState"] = "ERROR"
719
720 content["operatingState"] = "IDLE"
721 content = self.update_operation_history(
722 content, workflow_status, resource_status
723 )
724 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
725
726 return
727
garciadeblas96b94f52024-07-08 16:18:21 +0200728 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000729 self.logger.info("App delete Enter")
rshri932105f2024-07-05 15:11:55 +0000730
garciadeblas96b94f52024-07-08 16:18:21 +0200731 workflow_name = await self.odu.launch_workflow(
732 "delete_profile", op_id, op_params, content
733 )
rshri932105f2024-07-05 15:11:55 +0000734 self.logger.info("workflow_name is :{}".format(workflow_name))
735
garciadeblas96b94f52024-07-08 16:18:21 +0200736 workflow_status, workflow_msg = await self.odu.check_workflow_status(
737 workflow_name
738 )
rshri932105f2024-07-05 15:11:55 +0000739 self.logger.info(
740 "workflow_status is :{} and workflow_msg is :{}".format(
741 workflow_status, workflow_msg
742 )
743 )
744 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200745 content["state"] = "DELETED"
746 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000747 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200748 content["state"] = "FAILED_DELETION"
749 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000750 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200751 content = self.update_operation_history(content, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +0000752 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
753
754 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200755 resource_status, resource_msg = await self.odu.check_resource_status(
756 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000757 )
758 self.logger.info(
759 "resource_status is :{} and resource_msg is :{}".format(
760 resource_status, resource_msg
761 )
762 )
763 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200764 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000765 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200766 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000767
garciadeblas96b94f52024-07-08 16:18:21 +0200768 content["operatingState"] = "IDLE"
769 content = self.update_operation_history(
770 content, workflow_status, resource_status
771 )
772 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000773
garciadeblas96b94f52024-07-08 16:18:21 +0200774 # To delete it from DB
775 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +0000776 self.db.del_one("k8sapp", {"_id": content["_id"]})
777 return
778
779
780class K8sResourceLcm(LcmBase):
781 def __init__(self, msg, lcm_tasks, config):
782 """
783 Init, Connect to database, filesystem storage, and messaging
784 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
785 :return: None
786 """
787
garciadeblas40539872024-09-11 14:28:38 +0200788 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +0000789 self.lcm_tasks = lcm_tasks
790 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
791
792 super().__init__(msg, self.logger)
793
garciadeblas96b94f52024-07-08 16:18:21 +0200794 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000795 self.logger.info("Resource Create Enter")
796
garciadeblas96b94f52024-07-08 16:18:21 +0200797 workflow_name = await self.odu.launch_workflow(
798 "create_profile", op_id, op_params, content
799 )
rshri932105f2024-07-05 15:11:55 +0000800 self.logger.info("workflow_name is :{}".format(workflow_name))
801
garciadeblas96b94f52024-07-08 16:18:21 +0200802 workflow_status, workflow_msg = await self.odu.check_workflow_status(
803 workflow_name
804 )
rshri932105f2024-07-05 15:11:55 +0000805 self.logger.info(
806 "workflow_status is :{} and workflow_msg is :{}".format(
807 workflow_status, workflow_msg
808 )
809 )
810 if workflow_status:
811 content["state"] = "CREATED"
812 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
813 else:
814 content["state"] = "FAILED_CREATION"
815 content["resourceState"] = "ERROR"
816 # has to call update_operation_history return content
817 content = self.update_operation_history(content, workflow_status, None)
818 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
819
820 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200821 resource_status, resource_msg = await self.odu.check_resource_status(
822 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000823 )
824 self.logger.info(
825 "resource_status is :{} and resource_msg is :{}".format(
826 resource_status, resource_msg
827 )
828 )
829 if resource_status:
830 content["resourceState"] = "READY"
831 else:
832 content["resourceState"] = "ERROR"
833
834 content["operatingState"] = "IDLE"
835 content = self.update_operation_history(
836 content, workflow_status, resource_status
837 )
838 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
839
840 return
841
garciadeblas96b94f52024-07-08 16:18:21 +0200842 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000843 self.logger.info("Resource delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200844 content = self.db.get_one("k8sresource", {"_id": content["_id"]})
rshri932105f2024-07-05 15:11:55 +0000845
garciadeblas96b94f52024-07-08 16:18:21 +0200846 workflow_name = await self.odu.launch_workflow(
847 "delete_profile", op_id, op_params, content
848 )
rshri932105f2024-07-05 15:11:55 +0000849 self.logger.info("workflow_name is :{}".format(workflow_name))
850
garciadeblas96b94f52024-07-08 16:18:21 +0200851 workflow_status, workflow_msg = await self.odu.check_workflow_status(
852 workflow_name
853 )
rshri932105f2024-07-05 15:11:55 +0000854 self.logger.info(
855 "workflow_status is :{} and workflow_msg is :{}".format(
856 workflow_status, workflow_msg
857 )
858 )
859 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200860 content["state"] = "DELETED"
861 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000862 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200863 content["state"] = "FAILED_DELETION"
864 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000865 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200866 content = self.update_operation_history(content, workflow_status, None)
867 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000868
869 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200870 resource_status, resource_msg = await self.odu.check_resource_status(
871 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000872 )
873 self.logger.info(
874 "resource_status is :{} and resource_msg is :{}".format(
875 resource_status, resource_msg
876 )
877 )
878 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200879 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000880 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200881 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000882
garciadeblas96b94f52024-07-08 16:18:21 +0200883 content["operatingState"] = "IDLE"
884 content = self.update_operation_history(
885 content, workflow_status, resource_status
886 )
887 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000888
garciadeblas96b94f52024-07-08 16:18:21 +0200889 # To delete it from DB
890 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +0000891 self.db.del_one("k8sresource", {"_id": content["_id"]})
892 return
893
894
895class K8sInfraControllerLcm(LcmBase):
896 def __init__(self, msg, lcm_tasks, config):
897 """
898 Init, Connect to database, filesystem storage, and messaging
899 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
900 :return: None
901 """
902
garciadeblas40539872024-09-11 14:28:38 +0200903 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +0000904 self.lcm_tasks = lcm_tasks
905 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
906
907 super().__init__(msg, self.logger)
908
garciadeblas96b94f52024-07-08 16:18:21 +0200909 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000910 self.logger.info("Infra controller Create Enter")
911
garciadeblas96b94f52024-07-08 16:18:21 +0200912 workflow_name = await self.odu.launch_workflow(
913 "create_profile", op_id, op_params, content
914 )
rshri932105f2024-07-05 15:11:55 +0000915 self.logger.info("workflow_name is :{}".format(workflow_name))
916
garciadeblas96b94f52024-07-08 16:18:21 +0200917 workflow_status, workflow_msg = await self.odu.check_workflow_status(
918 workflow_name
919 )
rshri932105f2024-07-05 15:11:55 +0000920 self.logger.info(
921 "workflow_status is :{} and workflow_msg is :{}".format(
922 workflow_status, workflow_msg
923 )
924 )
925 if workflow_status:
926 content["state"] = "CREATED"
927 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
928 else:
929 content["state"] = "FAILED_CREATION"
930 content["resourceState"] = "ERROR"
931 # has to call update_operation_history return content
932 content = self.update_operation_history(content, workflow_status, None)
933 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
934
935 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200936 resource_status, resource_msg = await self.odu.check_resource_status(
937 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000938 )
939 self.logger.info(
940 "resource_status is :{} and resource_msg is :{}".format(
941 resource_status, resource_msg
942 )
943 )
944 if resource_status:
945 content["resourceState"] = "READY"
946 else:
947 content["resourceState"] = "ERROR"
948
949 content["operatingState"] = "IDLE"
950 content = self.update_operation_history(
951 content, workflow_status, resource_status
952 )
953 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
954
955 return
956
garciadeblas96b94f52024-07-08 16:18:21 +0200957 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000958 self.logger.info("Infra controller delete Enter")
rshri932105f2024-07-05 15:11:55 +0000959
garciadeblas96b94f52024-07-08 16:18:21 +0200960 workflow_name = await self.odu.launch_workflow(
961 "delete_profile", op_id, op_params, content
962 )
rshri932105f2024-07-05 15:11:55 +0000963 self.logger.info("workflow_name is :{}".format(workflow_name))
964
garciadeblas96b94f52024-07-08 16:18:21 +0200965 workflow_status, workflow_msg = await self.odu.check_workflow_status(
966 workflow_name
967 )
rshri932105f2024-07-05 15:11:55 +0000968 self.logger.info(
969 "workflow_status is :{} and workflow_msg is :{}".format(
970 workflow_status, workflow_msg
971 )
972 )
973 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200974 content["state"] = "DELETED"
975 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000976 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200977 content["state"] = "FAILED_DELETION"
978 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000979 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200980 content = self.update_operation_history(content, workflow_status, None)
981 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +0000982
983 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200984 resource_status, resource_msg = await self.odu.check_resource_status(
985 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000986 )
987 self.logger.info(
988 "resource_status is :{} and resource_msg is :{}".format(
989 resource_status, resource_msg
990 )
991 )
992 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200993 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000994 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200995 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000996
garciadeblas96b94f52024-07-08 16:18:21 +0200997 content["operatingState"] = "IDLE"
998 content = self.update_operation_history(
999 content, workflow_status, resource_status
1000 )
1001 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001002
garciadeblas96b94f52024-07-08 16:18:21 +02001003 # To delete it from DB
1004 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001005 self.db.del_one("k8sinfra_controller", {"_id": content["_id"]})
1006 return
1007
1008
1009class K8sInfraConfigLcm(LcmBase):
1010 def __init__(self, msg, lcm_tasks, config):
1011 """
1012 Init, Connect to database, filesystem storage, and messaging
1013 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1014 :return: None
1015 """
1016
garciadeblas40539872024-09-11 14:28:38 +02001017 self.logger = logging.getLogger("lcm.gitops")
rshri932105f2024-07-05 15:11:55 +00001018 self.lcm_tasks = lcm_tasks
1019 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
1020
1021 super().__init__(msg, self.logger)
1022
garciadeblas96b94f52024-07-08 16:18:21 +02001023 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001024 self.logger.info("Infra config Create Enter")
1025
garciadeblas96b94f52024-07-08 16:18:21 +02001026 workflow_name = await self.odu.launch_workflow(
1027 "create_profile", op_id, op_params, content
1028 )
rshri932105f2024-07-05 15:11:55 +00001029 self.logger.info("workflow_name is :{}".format(workflow_name))
1030
garciadeblas96b94f52024-07-08 16:18:21 +02001031 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1032 workflow_name
1033 )
rshri932105f2024-07-05 15:11:55 +00001034 self.logger.info(
1035 "workflow_status is :{} and workflow_msg is :{}".format(
1036 workflow_status, workflow_msg
1037 )
1038 )
1039 if workflow_status:
1040 content["state"] = "CREATED"
1041 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1042 else:
1043 content["state"] = "FAILED_CREATION"
1044 content["resourceState"] = "ERROR"
1045 # has to call update_operation_history return content
1046 content = self.update_operation_history(content, workflow_status, None)
1047 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1048
1049 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001050 resource_status, resource_msg = await self.odu.check_resource_status(
1051 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001052 )
1053 self.logger.info(
1054 "resource_status is :{} and resource_msg is :{}".format(
1055 resource_status, resource_msg
1056 )
1057 )
1058 if resource_status:
1059 content["resourceState"] = "READY"
1060 else:
1061 content["resourceState"] = "ERROR"
1062
1063 content["operatingState"] = "IDLE"
1064 content = self.update_operation_history(
1065 content, workflow_status, resource_status
1066 )
1067 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1068
1069 return
1070
garciadeblas96b94f52024-07-08 16:18:21 +02001071 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001072 self.logger.info("Infra config delete Enter")
1073
garciadeblas96b94f52024-07-08 16:18:21 +02001074 workflow_name = await self.odu.launch_workflow(
1075 "delete_profile", op_id, op_params, content
1076 )
rshri932105f2024-07-05 15:11:55 +00001077 self.logger.info("workflow_name is :{}".format(workflow_name))
rshri932105f2024-07-05 15:11:55 +00001078
garciadeblas96b94f52024-07-08 16:18:21 +02001079 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1080 workflow_name
1081 )
rshri932105f2024-07-05 15:11:55 +00001082 self.logger.info(
1083 "workflow_status is :{} and workflow_msg is :{}".format(
1084 workflow_status, workflow_msg
1085 )
1086 )
1087 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001088 content["state"] = "DELETED"
1089 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001090 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001091 content["state"] = "FAILED_DELETION"
1092 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001093 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001094 content = self.update_operation_history(content, workflow_status, None)
1095 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001096
garciadeblas96b94f52024-07-08 16:18:21 +02001097 resource_status, resource_msg = await self.odu.check_resource_status(
1098 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001099 )
1100 self.logger.info(
1101 "resource_status is :{} and resource_msg is :{}".format(
1102 resource_status, resource_msg
1103 )
1104 )
1105 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001106 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001107 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001108 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001109
garciadeblas96b94f52024-07-08 16:18:21 +02001110 content["operatingState"] = "IDLE"
1111 content = self.update_operation_history(
1112 content, workflow_status, resource_status
1113 )
1114 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001115
garciadeblas96b94f52024-07-08 16:18:21 +02001116 # To delete it from DB
1117 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001118 self.db.del_one("k8sinfra_config", {"_id": content["_id"]})
1119 return
yshah771dea82024-07-05 15:11:49 +00001120
1121
1122class OkaLcm(LcmBase):
1123 db_collection = "okas"
1124
1125 def __init__(self, msg, lcm_tasks, config):
1126 """
1127 Init, Connect to database, filesystem storage, and messaging
1128 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1129 :return: None
1130 """
1131
garciadeblas40539872024-09-11 14:28:38 +02001132 self.logger = logging.getLogger("lcm.gitops")
yshah771dea82024-07-05 15:11:49 +00001133 self.lcm_tasks = lcm_tasks
1134 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
1135
1136 super().__init__(msg, self.logger)
1137
garciadeblas96b94f52024-07-08 16:18:21 +02001138 async def create(self, op_id, op_params, content):
1139 self.logger.info("OKA Create Enter")
1140 db_content = content
yshah771dea82024-07-05 15:11:49 +00001141
garciadeblas96b94f52024-07-08 16:18:21 +02001142 workflow_name = await self.odu.launch_workflow(
1143 "create_oka", op_id, op_params, db_content
1144 )
1145 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1146 workflow_name
1147 )
1148 self.logger.info(
1149 "Workflow Status: {} Workflow Message: {}".format(
1150 workflow_status, workflow_msg
1151 )
1152 )
yshah771dea82024-07-05 15:11:49 +00001153
1154 if workflow_status:
1155 db_content["state"] = "CREATED"
1156 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1157 else:
1158 db_content["state"] = "FAILED_CREATION"
1159 db_content["resourceState"] = "ERROR"
1160
1161 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001162 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001163
1164 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001165 resource_status, resource_msg = await self.odu.check_resource_status(
1166 "create_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001167 )
garciadeblas96b94f52024-07-08 16:18:21 +02001168 self.logger.info(
1169 "Resource Status: {} Resource Message: {}".format(
1170 resource_status, resource_msg
1171 )
1172 )
yshah771dea82024-07-05 15:11:49 +00001173
1174 if resource_status:
1175 db_content["resourceState"] = "READY"
1176 else:
1177 db_content["resourceState"] = "ERROR"
1178
1179 # self.logger.info("Db content: {}".format(db_content))
1180 db_content = self.update_operation_history(
1181 db_content, workflow_status, resource_status
1182 )
1183
1184 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001185 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001186
1187 return
1188
garciadeblas96b94f52024-07-08 16:18:21 +02001189 async def edit(self, op_id, op_params, content):
1190 self.logger.info("OKA Edit Enter")
1191 db_content = content
yshah771dea82024-07-05 15:11:49 +00001192
garciadeblas96b94f52024-07-08 16:18:21 +02001193 workflow_name = await self.odu.launch_workflow(
1194 "update_oka", op_id, op_params, content
1195 )
1196 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1197 workflow_name
1198 )
1199 self.logger.info(
1200 "Workflow Status: {} Workflow Message: {}".format(
1201 workflow_status, workflow_msg
1202 )
1203 )
yshah771dea82024-07-05 15:11:49 +00001204
1205 if workflow_status:
1206 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1207 else:
1208 db_content["resourceState"] = "ERROR"
1209
1210 db_content = self.update_operation_history(db_content, workflow_status, None)
1211 # self.logger.info("Db content: {}".format(db_content))
garciadeblas96b94f52024-07-08 16:18:21 +02001212 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001213
1214 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001215 resource_status, resource_msg = await self.odu.check_resource_status(
1216 "update_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001217 )
garciadeblas96b94f52024-07-08 16:18:21 +02001218 self.logger.info(
1219 "Resource Status: {} Resource Message: {}".format(
1220 resource_status, resource_msg
1221 )
1222 )
yshah771dea82024-07-05 15:11:49 +00001223
1224 if resource_status:
1225 db_content["resourceState"] = "READY"
1226 else:
1227 db_content["resourceState"] = "ERROR"
1228
1229 db_content = self.update_operation_history(
1230 db_content, workflow_status, resource_status
1231 )
1232
1233 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001234 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001235 return
1236
garciadeblas96b94f52024-07-08 16:18:21 +02001237 async def delete(self, op_id, op_params, content):
1238 self.logger.info("OKA delete Enter")
1239 db_content = content
yshah771dea82024-07-05 15:11:49 +00001240
garciadeblas96b94f52024-07-08 16:18:21 +02001241 workflow_name = await self.odu.launch_workflow(
1242 "delete_oka", op_id, op_params, content
1243 )
1244 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1245 workflow_name
1246 )
1247 self.logger.info(
1248 "Workflow Status: {} Workflow Message: {}".format(
1249 workflow_status, workflow_msg
1250 )
1251 )
yshah771dea82024-07-05 15:11:49 +00001252
1253 if workflow_status:
1254 db_content["state"] = "DELETED"
1255 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1256 else:
1257 db_content["state"] = "FAILED_DELETION"
1258 db_content["resourceState"] = "ERROR"
1259
1260 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001261 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001262
1263 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001264 resource_status, resource_msg = await self.odu.check_resource_status(
1265 "delete_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001266 )
garciadeblas96b94f52024-07-08 16:18:21 +02001267 self.logger.info(
1268 "Resource Status: {} Resource Message: {}".format(
1269 resource_status, resource_msg
1270 )
1271 )
yshah771dea82024-07-05 15:11:49 +00001272
1273 if resource_status:
1274 db_content["resourceState"] = "READY"
1275 else:
1276 db_content["resourceState"] = "ERROR"
1277
1278 db_content = self.update_operation_history(
1279 db_content, workflow_status, resource_status
1280 )
1281
1282 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001283 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001284
1285 if db_content["state"] == "DELETED":
garciadeblas96b94f52024-07-08 16:18:21 +02001286 self.db.del_one(self.db_collection, {"_id": db_content["_id"]})
yshah771dea82024-07-05 15:11:49 +00001287 return
1288
1289
1290class KsuLcm(LcmBase):
1291 db_collection = "ksus"
1292
1293 def __init__(self, msg, lcm_tasks, config):
1294 """
1295 Init, Connect to database, filesystem storage, and messaging
1296 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1297 :return: None
1298 """
1299
garciadeblas40539872024-09-11 14:28:38 +02001300 self.logger = logging.getLogger("lcm.gitops")
yshah771dea82024-07-05 15:11:49 +00001301 self.lcm_tasks = lcm_tasks
1302 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
1303
1304 super().__init__(msg, self.logger)
1305
garciadeblas96b94f52024-07-08 16:18:21 +02001306 async def create(self, op_id, op_params, content):
1307 self.logger.info("ksu Create Enter")
yshah771dea82024-07-05 15:11:49 +00001308
garciadeblas96b94f52024-07-08 16:18:21 +02001309 workflow_name = await self.odu.launch_workflow(
1310 "create_ksus", op_id, op_params, content
1311 )
1312 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1313 workflow_name
1314 )
1315 self.logger.info(
1316 "Workflow Status: {} Workflow Message: {}".format(
1317 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001318 )
garciadeblas96b94f52024-07-08 16:18:21 +02001319 )
yshah771dea82024-07-05 15:11:49 +00001320
garciadeblas96b94f52024-07-08 16:18:21 +02001321 for db_ksu in content:
1322 if workflow_status:
1323 db_ksu["state"] = "CREATED"
1324 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001325 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001326 db_ksu["state"] = "FAILED_CREATION"
1327 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001328
garciadeblas96b94f52024-07-08 16:18:21 +02001329 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1330 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1331
garciadeblasd8429852024-10-17 15:30:30 +02001332 # Clean items used in the workflow, no matter if the workflow succeeded
1333 clean_status, clean_msg = await self.odu.clean_items_workflow(
1334 "create_ksus", op_id, op_params, content
1335 )
1336 self.logger.info(
1337 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1338 )
1339
garciadeblas96b94f52024-07-08 16:18:21 +02001340 if workflow_status:
1341 resource_status, resource_msg = await self.odu.check_resource_status(
1342 "create_ksus", op_id, op_params, content
1343 )
1344 self.logger.info(
1345 "Resource Status: {} Resource Message: {}".format(
1346 resource_status, resource_msg
1347 )
yshah771dea82024-07-05 15:11:49 +00001348 )
1349
garciadeblas96b94f52024-07-08 16:18:21 +02001350 for db_ksu in content:
1351 if resource_status:
1352 db_ksu["resourceState"] = "READY"
1353 else:
1354 db_ksu["resourceState"] = "ERROR"
1355
1356 db_ksu = self.update_operation_history(
1357 db_ksu, workflow_status, resource_status
1358 )
1359
1360 for db_ksu in content:
1361 db_ksu["operatingState"] = "IDLE"
1362 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
yshah771dea82024-07-05 15:11:49 +00001363
1364 return
1365
garciadeblas96b94f52024-07-08 16:18:21 +02001366 async def edit(self, op_id, op_params, content):
1367 self.logger.info("ksu edit Enter")
yshah771dea82024-07-05 15:11:49 +00001368
garciadeblas96b94f52024-07-08 16:18:21 +02001369 workflow_name = await self.odu.launch_workflow(
1370 "update_ksus", op_id, op_params, content
1371 )
1372 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1373 workflow_name
1374 )
1375 self.logger.info(
1376 "Workflow Status: {} Workflow Message: {}".format(
1377 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001378 )
garciadeblas96b94f52024-07-08 16:18:21 +02001379 )
yshah771dea82024-07-05 15:11:49 +00001380
garciadeblas96b94f52024-07-08 16:18:21 +02001381 for db_ksu in content:
1382 if workflow_status:
1383 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001384 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001385 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001386
garciadeblas96b94f52024-07-08 16:18:21 +02001387 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1388 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1389
garciadeblasd8429852024-10-17 15:30:30 +02001390 # Clean items used in the workflow, no matter if the workflow succeeded
1391 clean_status, clean_msg = await self.odu.clean_items_workflow(
1392 "create_ksus", op_id, op_params, content
1393 )
1394 self.logger.info(
1395 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1396 )
garciadeblas96b94f52024-07-08 16:18:21 +02001397 if workflow_status:
1398 resource_status, resource_msg = await self.odu.check_resource_status(
1399 "update_ksus", op_id, op_params, content
1400 )
1401 self.logger.info(
1402 "Resource Status: {} Resource Message: {}".format(
1403 resource_status, resource_msg
1404 )
yshah771dea82024-07-05 15:11:49 +00001405 )
1406
garciadeblas96b94f52024-07-08 16:18:21 +02001407 for db_ksu in content:
1408 if resource_status:
1409 db_ksu["resourceState"] = "READY"
1410 else:
1411 db_ksu["resourceState"] = "ERROR"
1412
1413 db_ksu = self.update_operation_history(
1414 db_ksu, workflow_status, resource_status
1415 )
1416
1417 for db_ksu, ksu_params in zip(content, op_params):
1418 db_ksu["operatingState"] = "IDLE"
1419 if workflow_status:
1420 db_ksu["name"] = ksu_params["name"]
1421 db_ksu["description"] = ksu_params["description"]
1422 db_ksu["profile"]["profile_type"] = ksu_params["profile"][
1423 "profile_type"
1424 ]
1425 db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"]
1426 db_ksu["oka"] = ksu_params["oka"]
1427 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1428
yshah771dea82024-07-05 15:11:49 +00001429 return
1430
garciadeblas96b94f52024-07-08 16:18:21 +02001431 async def delete(self, op_id, op_params, content):
1432 self.logger.info("ksu delete Enter")
yshah771dea82024-07-05 15:11:49 +00001433
garciadeblas96b94f52024-07-08 16:18:21 +02001434 workflow_name = await self.odu.launch_workflow(
1435 "delete_ksus", op_id, op_params, content
1436 )
1437 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1438 workflow_name
1439 )
1440 self.logger.info(
1441 "Workflow Status: {} Workflow Message: {}".format(
1442 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001443 )
garciadeblas96b94f52024-07-08 16:18:21 +02001444 )
yshah771dea82024-07-05 15:11:49 +00001445
garciadeblas96b94f52024-07-08 16:18:21 +02001446 for db_ksu in content:
1447 if workflow_status:
1448 db_ksu["state"] = "DELETED"
1449 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001450 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001451 db_ksu["state"] = "FAILED_DELETION"
1452 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001453
garciadeblas96b94f52024-07-08 16:18:21 +02001454 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1455 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1456
1457 if workflow_status:
1458 resource_status, resource_msg = await self.odu.check_resource_status(
1459 "delete_ksus", op_id, op_params, content
1460 )
1461 self.logger.info(
1462 "Resource Status: {} Resource Message: {}".format(
1463 resource_status, resource_msg
1464 )
yshah771dea82024-07-05 15:11:49 +00001465 )
1466
garciadeblas96b94f52024-07-08 16:18:21 +02001467 for db_ksu in content:
1468 if resource_status:
1469 db_ksu["resourceState"] = "READY"
1470 else:
1471 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001472
garciadeblas96b94f52024-07-08 16:18:21 +02001473 db_ksu = self.update_operation_history(
1474 db_ksu, workflow_status, resource_status
1475 )
1476
1477 for db_ksu in content:
1478 db_ksu["operatingState"] = "IDLE"
1479 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1480
1481 if db_ksu["state"] == "DELETED":
1482 self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]})
yshah771dea82024-07-05 15:11:49 +00001483 return
1484
garciadeblas96b94f52024-07-08 16:18:21 +02001485 async def clone(self, op_id, op_params, db_content):
1486 self.logger.info("ksu clone Enter")
yshah771dea82024-07-05 15:11:49 +00001487
garciadeblas96b94f52024-07-08 16:18:21 +02001488 workflow_name = await self.odu.launch_workflow(
1489 "clone_ksus", op_id, op_params, db_content
1490 )
1491 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1492 workflow_name
1493 )
1494 self.logger.info(
1495 "Workflow Status: {} Workflow Message: {}".format(
1496 workflow_status, workflow_msg
1497 )
1498 )
yshah771dea82024-07-05 15:11:49 +00001499
1500 if workflow_status:
1501 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1502 else:
1503 db_content["resourceState"] = "ERROR"
1504
1505 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001506 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001507
1508 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001509 resource_status, resource_msg = await self.odu.check_resource_status(
1510 "clone_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001511 )
garciadeblas96b94f52024-07-08 16:18:21 +02001512 self.logger.info(
1513 "Resource Status: {} Resource Message: {}".format(
1514 resource_status, resource_msg
1515 )
1516 )
yshah771dea82024-07-05 15:11:49 +00001517
1518 if resource_status:
1519 db_content["resourceState"] = "READY"
1520 else:
1521 db_content["resourceState"] = "ERROR"
1522
1523 db_content = self.update_operation_history(
1524 db_content, workflow_status, resource_status
1525 )
1526
1527 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001528 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001529 return
1530
garciadeblas96b94f52024-07-08 16:18:21 +02001531 async def move(self, op_id, op_params, db_content):
1532 self.logger.info("ksu move Enter")
yshah771dea82024-07-05 15:11:49 +00001533
garciadeblas96b94f52024-07-08 16:18:21 +02001534 workflow_name = await self.odu.launch_workflow(
1535 "move_ksus", op_id, op_params, db_content
1536 )
1537 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1538 workflow_name
1539 )
1540 self.logger.info(
1541 "Workflow Status: {} Workflow Message: {}".format(
1542 workflow_status, workflow_msg
1543 )
1544 )
yshah771dea82024-07-05 15:11:49 +00001545
1546 if workflow_status:
1547 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1548 else:
1549 db_content["resourceState"] = "ERROR"
1550
1551 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001552 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001553
1554 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001555 resource_status, resource_msg = await self.odu.check_resource_status(
1556 "move_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001557 )
garciadeblas96b94f52024-07-08 16:18:21 +02001558 self.logger.info(
1559 "Resource Status: {} Resource Message: {}".format(
1560 resource_status, resource_msg
1561 )
1562 )
yshah771dea82024-07-05 15:11:49 +00001563 if resource_status:
1564 db_content["resourceState"] = "READY"
1565 else:
1566 db_content["resourceState"] = "ERROR"
1567
1568 db_content = self.update_operation_history(
1569 db_content, workflow_status, resource_status
1570 )
1571
1572 db_content["operatingState"] = "IDLE"
garciadeblas96b94f52024-07-08 16:18:21 +02001573 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001574 return