blob: 55d98f475d37fef0d75c6d00f4d24faf2d1bd114 [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
rshric3564942024-11-12 18:12:38 +000021import copy
rshri932105f2024-07-05 15:11:55 +000022import logging
yshahd940c652024-10-17 06:11:12 +000023from time import time
garciadeblas72412282024-11-07 12:41:54 +010024import traceback
rshri932105f2024-07-05 15:11:55 +000025from osm_lcm.lcm_utils import LcmBase
26from copy import deepcopy
27from osm_lcm import odu_workflows
28from osm_lcm import vim_sdn
29
30
garciadeblas72412282024-11-07 12:41:54 +010031class GitOpsLcm(LcmBase):
garciadeblasea865ff2024-11-20 12:42:49 +010032 db_collection = "gitops"
33
garciadeblas72412282024-11-07 12:41:54 +010034 def __init__(self, msg, lcm_tasks, config):
35 self.logger = logging.getLogger("lcm.gitops")
36 self.lcm_tasks = lcm_tasks
37 self.odu = odu_workflows.OduWorkflow(msg, self.lcm_tasks, config)
38 self._checkloop_kustomization_timeout = 900
39 self._checkloop_resource_timeout = 900
40 self._workflows = {}
41 super().__init__(msg, self.logger)
42
43 async def check_dummy_operation(self, op_id, op_params, content):
44 self.logger.info(f"Operation {op_id}. Params: {op_params}. Content: {content}")
45 return True, "OK"
46
garciadeblasea865ff2024-11-20 12:42:49 +010047 def initialize_operation(self, item_id, op_id):
48 db_item = self.db.get_one(self.db_collection, {"_id": item_id})
49 operation = next(
50 (op for op in db_item.get("operationHistory", []) if op["op_id"] == op_id),
51 None,
52 )
53 operation["workflowState"] = "PROCESSING"
54 operation["resourceState"] = "NOT_READY"
55 operation["operationState"] = "IN_PROGRESS"
56 operation["gitOperationInfo"] = None
57 db_item["current_operation"] = operation["op_id"]
58 self.db.set_one(self.db_collection, {"_id": item_id}, db_item)
59
garciadeblas7eae6f42024-11-08 10:41:38 +010060 def update_operation_history(
61 self, content, workflow_status=None, resource_status=None
62 ):
63 self.logger.info(
64 f"Update Operation History in DB. Workflow status: {workflow_status}. Resource status: {resource_status}"
65 )
66 self.logger.debug(f"Content: {content}")
67
68 op_id = content["current_operation"]
69 self.logger.debug("OP_id: {}".format(op_id))
70 op_num = 0
71 for operation in content["operationHistory"]:
72 self.logger.debug("Operations: {}".format(operation))
73 if operation["op_id"] == op_id:
74 self.logger.debug("Found operation number: {}".format(op_num))
75 now = time()
76 if workflow_status:
77 content["operationHistory"][op_num]["workflowState"] = "COMPLETED"
78 content["operationHistory"][op_num]["result"] = True
79 else:
80 content["operationHistory"][op_num]["workflowState"] = "ERROR"
81 content["operationHistory"][op_num]["operationState"] = "FAILED"
82 content["operationHistory"][op_num]["result"] = False
83
84 if resource_status:
85 content["operationHistory"][op_num]["resourceState"] = "READY"
86 content["operationHistory"][op_num]["operationState"] = "COMPLETED"
87 content["operationHistory"][op_num]["result"] = True
88 else:
89 content["operationHistory"][op_num]["resourceState"] = "NOT_READY"
90 content["operationHistory"][op_num]["operationState"] = "FAILED"
91 content["operationHistory"][op_num]["result"] = False
92
93 content["operationHistory"][op_num]["endDate"] = now
94 break
95 op_num += 1
96 self.logger.debug("content: {}".format(content))
97
98 return content
99
100 async def common_check_list(self, checkings_list, db_collection, db_item):
garciadeblas72412282024-11-07 12:41:54 +0100101 try:
102 for checking in checkings_list:
103 if checking["enable"]:
104 status, message = await self.odu.readiness_loop(
105 item=checking["item"],
106 name=checking["name"],
107 namespace=checking["namespace"],
108 flag=checking["flag"],
109 timeout=checking["timeout"],
110 )
111 if not status:
112 return status, message
garciadeblas7eae6f42024-11-08 10:41:38 +0100113 else:
114 db_item["resourceState"] = checking["resourceState"]
115 db_item = self.update_operation_history(
116 db_item, "COMPLETED", checking["resourceState"]
117 )
118 self.db.set_one(db_collection, {"_id": db_item["_id"]}, db_item)
garciadeblas72412282024-11-07 12:41:54 +0100119 except Exception as e:
120 self.logger.debug(traceback.format_exc())
121 self.logger.debug(f"Exception: {e}", exc_info=True)
122 return False, f"Unexpected exception: {e}"
123 return True, "OK"
124
125 async def check_resource_status(self, key, op_id, op_params, content):
126 self.logger.info(
127 f"Check resource status. Key: {key}. Operation: {op_id}. Params: {op_params}. Content: {content}"
128 )
129 check_resource_function = self._workflows.get(key, {}).get(
130 "check_resource_function"
131 )
132 self.logger.info("check_resource function : {}".format(check_resource_function))
133 if check_resource_function:
134 return await check_resource_function(op_id, op_params, content)
135 else:
136 return await self.check_dummy_operation(op_id, op_params, content)
137
rshric3564942024-11-12 18:12:38 +0000138 def decrypting_key(self, content):
139 # This deep copy is for to be passed to ODU workflows.
140 cluster_copy = copy.deepcopy(content)
141
142 # decrypting the key
143 self.db.encrypt_decrypt_fields(
144 cluster_copy,
145 "decrypt",
146 ["age_pubkey", "age_privkey"],
147 schema_version="1.11",
148 salt=cluster_copy["_id"],
149 )
150 db_cluster_copy = {
151 "cluster": cluster_copy,
152 }
153 return db_cluster_copy
154
garciadeblas72412282024-11-07 12:41:54 +0100155
156class ClusterLcm(GitOpsLcm):
garciadeblas96b94f52024-07-08 16:18:21 +0200157 db_collection = "clusters"
rshri932105f2024-07-05 15:11:55 +0000158
159 def __init__(self, msg, lcm_tasks, config):
160 """
161 Init, Connect to database, filesystem storage, and messaging
162 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
163 :return: None
164 """
garciadeblas72412282024-11-07 12:41:54 +0100165 super().__init__(msg, lcm_tasks, config)
166 self._workflows = {
167 "create_cluster": {
168 "check_resource_function": self.check_create_cluster,
169 },
garciadeblasb0a42c22024-11-13 16:00:10 +0100170 "register_cluster": {
171 "check_resource_function": self.check_register_cluster,
172 },
173 "update_cluster": {
174 "check_resource_function": self.check_update_cluster,
175 },
garciadeblas72412282024-11-07 12:41:54 +0100176 }
rshri932105f2024-07-05 15:11:55 +0000177 self.regist = vim_sdn.K8sClusterLcm(msg, self.lcm_tasks, config)
178
garciadeblas96b94f52024-07-08 16:18:21 +0200179 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000180 self.logger.info("cluster Create Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200181 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000182
rshric3564942024-11-12 18:12:38 +0000183 db_cluster_copy = self.decrypting_key(db_cluster)
184
185 # vim account details
186 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
187 db_cluster_copy["vim_account"] = db_vim
188
garciadeblasadb81e82024-11-08 01:11:46 +0100189 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000190 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200191 )
rshri932105f2024-07-05 15:11:55 +0000192 self.logger.info("workflow_name is :{}".format(workflow_name))
193
garciadeblas96b94f52024-07-08 16:18:21 +0200194 workflow_status, workflow_msg = await self.odu.check_workflow_status(
195 workflow_name
196 )
rshri932105f2024-07-05 15:11:55 +0000197 self.logger.info(
198 "workflow_status is :{} and workflow_msg is :{}".format(
199 workflow_status, workflow_msg
200 )
201 )
202 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200203 db_cluster["state"] = "CREATED"
204 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000205 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200206 db_cluster["state"] = "FAILED_CREATION"
207 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000208 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200209 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
210 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000211
garciadeblas28bff0f2024-09-16 12:53:07 +0200212 # Clean items used in the workflow, no matter if the workflow succeeded
213 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000214 "create_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200215 )
216 self.logger.info(
217 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
218 )
219
rshri932105f2024-07-05 15:11:55 +0000220 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100221 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000222 "create_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000223 )
224 self.logger.info(
225 "resource_status is :{} and resource_msg is :{}".format(
226 resource_status, resource_msg
227 )
228 )
229 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200230 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000231 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200232 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000233
garciadeblas96b94f52024-07-08 16:18:21 +0200234 db_cluster["operatingState"] = "IDLE"
235 db_cluster = self.update_operation_history(
236 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000237 )
shahithya70a3fc92024-11-12 11:01:05 +0000238 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200239 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
240 self.update_profile_state(db_cluster, workflow_status, resource_status)
rshri932105f2024-07-05 15:11:55 +0000241 return
242
garciadeblas72412282024-11-07 12:41:54 +0100243 async def check_create_cluster(self, op_id, op_params, content):
garciadeblas7eae6f42024-11-08 10:41:38 +0100244 self.logger.info(
245 f"check_create_cluster Operation {op_id}. Params: {op_params}."
246 )
247 # self.logger.debug(f"Content: {content}")
garciadeblas72412282024-11-07 12:41:54 +0100248 db_cluster = content["cluster"]
249 cluster_name = db_cluster["git_name"].lower()
250 cluster_kustomization_name = cluster_name
251 db_vim_account = content["vim_account"]
252 cloud_type = db_vim_account["vim_type"]
253 nodepool_name = ""
254 if cloud_type == "aws":
255 nodepool_name = f"{cluster_name}-nodegroup"
256 cluster_name = f"{cluster_name}-cluster"
257 elif cloud_type == "gcp":
258 nodepool_name = f"nodepool-{cluster_name}"
259 bootstrap = op_params.get("bootstrap", True)
260 if cloud_type in ("azure", "gcp", "aws"):
261 checkings_list = [
262 {
263 "item": "kustomization",
264 "name": cluster_kustomization_name,
265 "namespace": "managed-resources",
266 "flag": "Ready",
267 "timeout": self._checkloop_kustomization_timeout,
268 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100269 "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY",
garciadeblas72412282024-11-07 12:41:54 +0100270 },
271 {
272 "item": f"cluster_{cloud_type}",
273 "name": cluster_name,
274 "namespace": "",
275 "flag": "Synced",
276 "timeout": self._checkloop_resource_timeout,
277 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100278 "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100279 },
280 {
281 "item": f"cluster_{cloud_type}",
282 "name": cluster_name,
283 "namespace": "",
284 "flag": "Ready",
285 "timeout": self._checkloop_resource_timeout,
286 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100287 "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER",
garciadeblas72412282024-11-07 12:41:54 +0100288 },
289 {
290 "item": "kustomization",
291 "name": f"{cluster_kustomization_name}-bstrp-fluxctrl",
292 "namespace": "managed-resources",
293 "flag": "Ready",
294 "timeout": self._checkloop_kustomization_timeout,
295 "enable": bootstrap,
garciadeblas7eae6f42024-11-08 10:41:38 +0100296 "resourceState": "IN_PROGRESS.BOOTSTRAP_OK",
garciadeblas72412282024-11-07 12:41:54 +0100297 },
298 ]
299 else:
300 return False, "Not suitable VIM account to check cluster status"
301 if nodepool_name:
302 nodepool_check = {
303 "item": f"nodepool_{cloud_type}",
304 "name": nodepool_name,
305 "namespace": "",
306 "flag": "Ready",
307 "timeout": self._checkloop_resource_timeout,
308 "enable": True,
garciadeblas7eae6f42024-11-08 10:41:38 +0100309 "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL",
garciadeblas72412282024-11-07 12:41:54 +0100310 }
311 checkings_list.insert(3, nodepool_check)
garciadeblas7eae6f42024-11-08 10:41:38 +0100312 return await self.common_check_list(checkings_list, "clusters", db_cluster)
garciadeblas72412282024-11-07 12:41:54 +0100313
garciadeblasb0a42c22024-11-13 16:00:10 +0100314 async def check_register_cluster(self, op_id, op_params, content):
315 self.logger.info(
316 f"check_register_cluster Operation {op_id}. Params: {op_params}."
317 )
318 # self.logger.debug(f"Content: {content}")
319 db_cluster = content["cluster"]
320 cluster_name = db_cluster["git_name"].lower()
321 cluster_kustomization_name = cluster_name
322 bootstrap = op_params.get("bootstrap", True)
323 checkings_list = [
324 {
325 "item": "kustomization",
326 "name": f"{cluster_kustomization_name}-bstrp-fluxctrl",
327 "namespace": "managed-resources",
328 "flag": "Ready",
329 "timeout": self._checkloop_kustomization_timeout,
330 "enable": bootstrap,
331 "resourceState": "IN_PROGRESS.BOOTSTRAP_OK",
332 },
333 ]
334 return await self.common_check_list(checkings_list, "clusters", db_cluster)
335
336 async def check_update_cluster(self, op_id, op_params, content):
337 self.logger.info(
338 f"check_create_cluster Operation {op_id}. Params: {op_params}."
339 )
340 # self.logger.debug(f"Content: {content}")
341 db_cluster = content["cluster"]
342 cluster_name = db_cluster["git_name"].lower()
343 cluster_kustomization_name = cluster_name
344 db_vim_account = content["vim_account"]
345 cloud_type = db_vim_account["vim_type"]
346 nodepool_name = ""
347 if cloud_type == "aws":
348 nodepool_name = f"{cluster_name}-nodegroup"
349 cluster_name = f"{cluster_name}-cluster"
350 elif cloud_type == "gcp":
351 nodepool_name = f"nodepool-{cluster_name}"
352 if cloud_type in ("azure", "gcp", "aws"):
353 checkings_list = [
354 {
355 "item": "kustomization",
356 "name": cluster_kustomization_name,
357 "namespace": "managed-resources",
358 "flag": "Ready",
359 "timeout": self._checkloop_kustomization_timeout,
360 "enable": True,
361 "resourceState": "IN_PROGRESS.KUSTOMIZATION_READY",
362 },
363 {
364 "item": f"cluster_{cloud_type}",
365 "name": cluster_name,
366 "namespace": "",
367 "flag": "Synced",
368 "timeout": self._checkloop_resource_timeout,
369 "enable": True,
370 "resourceState": "IN_PROGRESS.RESOURCE_SYNCED.CLUSTER",
371 },
372 {
373 "item": f"cluster_{cloud_type}",
374 "name": cluster_name,
375 "namespace": "",
376 "flag": "Ready",
377 "timeout": self._checkloop_resource_timeout,
378 "enable": True,
379 "resourceState": "IN_PROGRESS.RESOURCE_READY.CLUSTER",
380 },
381 ]
382 else:
383 return False, "Not suitable VIM account to check cluster status"
384 if nodepool_name:
385 nodepool_check = {
386 "item": f"nodepool_{cloud_type}",
387 "name": nodepool_name,
388 "namespace": "",
389 "flag": "Ready",
390 "timeout": self._checkloop_resource_timeout,
391 "enable": True,
392 "resourceState": "IN_PROGRESS.RESOURCE_READY.NODEPOOL",
393 }
394 checkings_list.append(nodepool_check)
395 return await self.common_check_list(checkings_list, "clusters", db_cluster)
396
garciadeblas96b94f52024-07-08 16:18:21 +0200397 def update_profile_state(self, db_cluster, workflow_status, resource_status):
rshri932105f2024-07-05 15:11:55 +0000398 profiles = [
399 "infra_controller_profiles",
400 "infra_config_profiles",
401 "app_profiles",
402 "resource_profiles",
403 ]
404 profiles_collection = {
405 "infra_controller_profiles": "k8sinfra_controller",
406 "infra_config_profiles": "k8sinfra_config",
407 "app_profiles": "k8sapp",
408 "resource_profiles": "k8sresource",
409 }
410 for profile_type in profiles:
garciadeblas96b94f52024-07-08 16:18:21 +0200411 profile_id = db_cluster[profile_type]
rshri932105f2024-07-05 15:11:55 +0000412 self.logger.info("profile id is : {}".format(profile_id))
413 db_collection = profiles_collection[profile_type]
414 self.logger.info("the db_collection is :{}".format(db_collection))
415 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
416 self.logger.info("the db_profile is :{}".format(db_profile))
garciadeblas96b94f52024-07-08 16:18:21 +0200417 db_profile["state"] = db_cluster["state"]
418 db_profile["resourceState"] = db_cluster["resourceState"]
419 db_profile["operatingState"] = db_cluster["operatingState"]
rshric3564942024-11-12 18:12:38 +0000420 db_profile["age_pubkey"] = db_cluster["age_pubkey"]
421 db_profile["age_privkey"] = db_profile["age_privkey"]
rshri932105f2024-07-05 15:11:55 +0000422 db_profile = self.update_operation_history(
423 db_profile, workflow_status, resource_status
424 )
425 self.logger.info("the db_profile is :{}".format(db_profile))
426 self.db.set_one(db_collection, {"_id": db_profile["_id"]}, db_profile)
427
garciadeblas96b94f52024-07-08 16:18:21 +0200428 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000429 self.logger.info("cluster delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200430 db_cluster = content["cluster"]
garciadeblas12470812024-11-18 10:33:12 +0100431 if db_cluster["created"] == "false":
432 return await self.deregister(op_id, op_params, content)
rshri932105f2024-07-05 15:11:55 +0000433
garciadeblasadb81e82024-11-08 01:11:46 +0100434 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200435 "delete_cluster", op_id, op_params, content
436 )
rshri932105f2024-07-05 15:11:55 +0000437 self.logger.info("workflow_name is :{}".format(workflow_name))
438
garciadeblas96b94f52024-07-08 16:18:21 +0200439 workflow_status, workflow_msg = await self.odu.check_workflow_status(
440 workflow_name
441 )
rshri932105f2024-07-05 15:11:55 +0000442 self.logger.info(
443 "workflow_status is :{} and workflow_msg is :{}".format(
444 workflow_status, workflow_msg
445 )
446 )
447 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200448 db_cluster["state"] = "DELETED"
449 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000450 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200451 db_cluster["state"] = "FAILED_DELETION"
452 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000453 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200454 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
455 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000456
457 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100458 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200459 "delete_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000460 )
461 self.logger.info(
462 "resource_status is :{} and resource_msg is :{}".format(
463 resource_status, resource_msg
464 )
465 )
466 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200467 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000468 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200469 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000470
garciadeblas96b94f52024-07-08 16:18:21 +0200471 db_cluster["operatingState"] = "IDLE"
472 db_cluster = self.update_operation_history(
473 db_cluster, workflow_status, resource_status
474 )
shahithya70a3fc92024-11-12 11:01:05 +0000475 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200476 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000477
garciadeblas96b94f52024-07-08 16:18:21 +0200478 # To delete it from DB
479 if db_cluster["state"] == "DELETED":
480 self.delete_cluster(db_cluster)
rshri932105f2024-07-05 15:11:55 +0000481 return
482
garciadeblas96b94f52024-07-08 16:18:21 +0200483 def delete_cluster(self, db_cluster):
484 # Actually, item_content is equal to db_cluster
485 # item_content = self.db.get_one("clusters", {"_id": db_cluster["_id"]})
486 # self.logger.debug("item_content is : {}".format(item_content))
rshri932105f2024-07-05 15:11:55 +0000487
rshri932105f2024-07-05 15:11:55 +0000488 # detach profiles
489 update_dict = None
490 profiles_to_detach = [
491 "infra_controller_profiles",
492 "infra_config_profiles",
493 "app_profiles",
494 "resource_profiles",
495 ]
496 profiles_collection = {
497 "infra_controller_profiles": "k8sinfra_controller",
498 "infra_config_profiles": "k8sinfra_config",
499 "app_profiles": "k8sapp",
500 "resource_profiles": "k8sresource",
501 }
502 for profile_type in profiles_to_detach:
garciadeblas96b94f52024-07-08 16:18:21 +0200503 if db_cluster.get(profile_type):
garciadeblasc2552852024-10-22 12:39:32 +0200504 self.logger.debug("the profile_type is :{}".format(profile_type))
garciadeblas96b94f52024-07-08 16:18:21 +0200505 profile_ids = db_cluster[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200506 self.logger.debug("the profile_ids is :{}".format(profile_ids))
rshri932105f2024-07-05 15:11:55 +0000507 profile_ids_copy = deepcopy(profile_ids)
garciadeblasc2552852024-10-22 12:39:32 +0200508 self.logger.debug(
509 "the profile_ids_copy is :{}".format(profile_ids_copy)
510 )
rshri932105f2024-07-05 15:11:55 +0000511 for profile_id in profile_ids_copy:
garciadeblasc2552852024-10-22 12:39:32 +0200512 self.logger.debug("the profile_id is :{}".format(profile_id))
rshri932105f2024-07-05 15:11:55 +0000513 db_collection = profiles_collection[profile_type]
garciadeblasc2552852024-10-22 12:39:32 +0200514 self.logger.debug("the db_collection is :{}".format(db_collection))
rshri932105f2024-07-05 15:11:55 +0000515 db_profile = self.db.get_one(db_collection, {"_id": profile_id})
garciadeblasc2552852024-10-22 12:39:32 +0200516 self.logger.debug("the db_profile is :{}".format(db_profile))
517 self.logger.debug(
garciadeblas96b94f52024-07-08 16:18:21 +0200518 "the item_content name is :{}".format(db_cluster["name"])
rshri932105f2024-07-05 15:11:55 +0000519 )
garciadeblasc2552852024-10-22 12:39:32 +0200520 self.logger.debug(
rshri932105f2024-07-05 15:11:55 +0000521 "the db_profile name is :{}".format(db_profile["name"])
522 )
garciadeblas96b94f52024-07-08 16:18:21 +0200523 if db_cluster["name"] == db_profile["name"]:
garciadeblasc2552852024-10-22 12:39:32 +0200524 self.logger.debug("it is getting into if default")
rshri932105f2024-07-05 15:11:55 +0000525 self.db.del_one(db_collection, {"_id": profile_id})
526 else:
garciadeblasc2552852024-10-22 12:39:32 +0200527 self.logger.debug("it is getting into else non default")
rshri932105f2024-07-05 15:11:55 +0000528 profile_ids.remove(profile_id)
529 update_dict = {profile_type: profile_ids}
garciadeblasc2552852024-10-22 12:39:32 +0200530 self.logger.debug(f"the update dict is :{update_dict}")
rshri932105f2024-07-05 15:11:55 +0000531 self.db.set_one(
garciadeblas96b94f52024-07-08 16:18:21 +0200532 "clusters", {"_id": db_cluster["_id"]}, update_dict
rshri932105f2024-07-05 15:11:55 +0000533 )
garciadeblas96b94f52024-07-08 16:18:21 +0200534 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
garciadeblasc2552852024-10-22 12:39:32 +0200535 self.logger.debug("the id is :{}".format(db_cluster["_id"]))
rshri932105f2024-07-05 15:11:55 +0000536
garciadeblas96b94f52024-07-08 16:18:21 +0200537 async def attach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000538 self.logger.info("profile attach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200539 db_cluster = content["cluster"]
540 db_profile = content["profile"]
541 profile_type = db_profile["profile_type"]
542 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000543 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000544 self.logger.info("profile id is : {}".format(profile_id))
545
garciadeblasadb81e82024-11-08 01:11:46 +0100546 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200547 "attach_profile_to_cluster", op_id, op_params, content
548 )
rshri932105f2024-07-05 15:11:55 +0000549 self.logger.info("workflow_name is :{}".format(workflow_name))
550
garciadeblas96b94f52024-07-08 16:18:21 +0200551 workflow_status, workflow_msg = await self.odu.check_workflow_status(
552 workflow_name
553 )
rshri932105f2024-07-05 15:11:55 +0000554 self.logger.info(
555 "workflow_status is :{} and workflow_msg is :{}".format(
556 workflow_status, workflow_msg
557 )
558 )
559 if workflow_status:
560 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
561 else:
562 db_cluster["resourceState"] = "ERROR"
563 # has to call update_operation_history return content
564 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
565 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
566
567 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100568 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200569 "attach_profile_to_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000570 )
571 self.logger.info(
572 "resource_status is :{} and resource_msg is :{}".format(
573 resource_status, resource_msg
574 )
575 )
576 if resource_status:
577 db_cluster["resourceState"] = "READY"
578 else:
579 db_cluster["resourceState"] = "ERROR"
580
581 db_cluster["operatingState"] = "IDLE"
582 db_cluster = self.update_operation_history(
583 db_cluster, workflow_status, resource_status
584 )
rshri932105f2024-07-05 15:11:55 +0000585 profile_list = db_cluster[profile_type]
586 self.logger.info("profile list is : {}".format(profile_list))
587 if resource_status:
588 self.logger.info("it is getting into resource status true")
589 profile_list.append(profile_id)
590 self.logger.info("profile list is : {}".format(profile_list))
591 db_cluster[profile_type] = profile_list
592 self.logger.info("db cluster is : {}".format(db_cluster))
593 # update_dict = {item: profile_list}
594 # self.logger.info("the update_dict is :{}".format(update_dict))
595 # self.db.set_one(self.topic, filter_q, update_dict)
shahithya70a3fc92024-11-12 11:01:05 +0000596 db_cluster["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +0000597 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
598
599 return
600
garciadeblas96b94f52024-07-08 16:18:21 +0200601 async def detach_profile(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000602 self.logger.info("profile dettach Enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200603 db_cluster = content["cluster"]
604 db_profile = content["profile"]
605 profile_type = db_profile["profile_type"]
606 profile_id = db_profile["_id"]
rshri932105f2024-07-05 15:11:55 +0000607 self.logger.info("profile type is : {}".format(profile_type))
rshri932105f2024-07-05 15:11:55 +0000608 self.logger.info("profile id is : {}".format(profile_id))
609
garciadeblasadb81e82024-11-08 01:11:46 +0100610 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200611 "detach_profile_from_cluster", op_id, op_params, content
612 )
rshri932105f2024-07-05 15:11:55 +0000613 self.logger.info("workflow_name is :{}".format(workflow_name))
614
garciadeblas96b94f52024-07-08 16:18:21 +0200615 workflow_status, workflow_msg = await self.odu.check_workflow_status(
616 workflow_name
617 )
rshri932105f2024-07-05 15:11:55 +0000618 self.logger.info(
619 "workflow_status is :{} and workflow_msg is :{}".format(
620 workflow_status, workflow_msg
621 )
622 )
623 if workflow_status:
624 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
625 else:
626 db_cluster["resourceState"] = "ERROR"
627 # has to call update_operation_history return content
628 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
629 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
630
631 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100632 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200633 "detach_profile_from_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000634 )
635 self.logger.info(
636 "resource_status is :{} and resource_msg is :{}".format(
637 resource_status, resource_msg
638 )
639 )
640 if resource_status:
641 db_cluster["resourceState"] = "READY"
642 else:
643 db_cluster["resourceState"] = "ERROR"
644
645 db_cluster["operatingState"] = "IDLE"
646 db_cluster = self.update_operation_history(
647 db_cluster, workflow_status, resource_status
648 )
rshri932105f2024-07-05 15:11:55 +0000649 profile_list = db_cluster[profile_type]
650 self.logger.info("profile list is : {}".format(profile_list))
651 if resource_status:
652 self.logger.info("it is getting into resource status true")
653 profile_list.remove(profile_id)
654 self.logger.info("profile list is : {}".format(profile_list))
655 db_cluster[profile_type] = profile_list
656 self.logger.info("db cluster is : {}".format(db_cluster))
657 # update_dict = {item: profile_list}
658 # self.logger.info("the update_dict is :{}".format(update_dict))
659 # self.db.set_one(self.topic, filter_q, update_dict)
shahithya70a3fc92024-11-12 11:01:05 +0000660 db_cluster["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +0000661 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
662
663 return
664
garciadeblas96b94f52024-07-08 16:18:21 +0200665 async def register(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000666 self.logger.info("cluster register enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200667 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000668
rshric3564942024-11-12 18:12:38 +0000669 db_cluster_copy = self.decrypting_key(db_cluster)
670
garciadeblasadb81e82024-11-08 01:11:46 +0100671 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000672 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200673 )
rshri932105f2024-07-05 15:11:55 +0000674 self.logger.info("workflow_name is :{}".format(workflow_name))
675
garciadeblas96b94f52024-07-08 16:18:21 +0200676 workflow_status, workflow_msg = await self.odu.check_workflow_status(
677 workflow_name
678 )
rshri932105f2024-07-05 15:11:55 +0000679 self.logger.info(
680 "workflow_status is :{} and workflow_msg is :{}".format(
681 workflow_status, workflow_msg
682 )
683 )
684 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200685 db_cluster["state"] = "CREATED"
686 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000687 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200688 db_cluster["state"] = "FAILED_CREATION"
689 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000690 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200691 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
692 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000693
garciadeblasdde3a312024-09-17 13:25:06 +0200694 # Clean items used in the workflow, no matter if the workflow succeeded
695 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000696 "register_cluster", op_id, op_params, db_cluster_copy
garciadeblasdde3a312024-09-17 13:25:06 +0200697 )
698 self.logger.info(
699 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
700 )
701
rshri932105f2024-07-05 15:11:55 +0000702 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100703 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000704 "register_cluster", op_id, op_params, db_cluster_copy
rshri932105f2024-07-05 15:11:55 +0000705 )
706 self.logger.info(
707 "resource_status is :{} and resource_msg is :{}".format(
708 resource_status, resource_msg
709 )
710 )
711 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200712 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000713 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200714 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000715
garciadeblas96b94f52024-07-08 16:18:21 +0200716 db_cluster["operatingState"] = "IDLE"
717 db_cluster = self.update_operation_history(
718 db_cluster, workflow_status, resource_status
rshri932105f2024-07-05 15:11:55 +0000719 )
shahithya70a3fc92024-11-12 11:01:05 +0000720 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200721 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000722 return
723
garciadeblas96b94f52024-07-08 16:18:21 +0200724 async def deregister(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000725 self.logger.info("cluster deregister enter")
garciadeblas96b94f52024-07-08 16:18:21 +0200726 db_cluster = content["cluster"]
rshri932105f2024-07-05 15:11:55 +0000727
garciadeblas96b94f52024-07-08 16:18:21 +0200728 self.logger.info("db_cluster is : {}".format(db_cluster))
rshri932105f2024-07-05 15:11:55 +0000729
garciadeblasadb81e82024-11-08 01:11:46 +0100730 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200731 "deregister_cluster", op_id, op_params, content
732 )
rshri932105f2024-07-05 15:11:55 +0000733 self.logger.info("workflow_name is :{}".format(workflow_name))
734
garciadeblas96b94f52024-07-08 16:18:21 +0200735 workflow_status, workflow_msg = await self.odu.check_workflow_status(
736 workflow_name
737 )
rshri932105f2024-07-05 15:11:55 +0000738 self.logger.info(
739 "workflow_status is :{} and workflow_msg is :{}".format(
740 workflow_status, workflow_msg
741 )
742 )
743 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200744 db_cluster["state"] = "DELETED"
745 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +0000746 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200747 db_cluster["state"] = "FAILED_DELETION"
748 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000749 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +0200750 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
751 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000752
garciadeblas91bb2c42024-11-12 11:17:12 +0100753 # Clean items used in the workflow or in the cluster, no matter if the workflow succeeded
754 clean_status, clean_msg = await self.odu.clean_items_workflow(
755 "deregister_cluster", op_id, op_params, content
756 )
757 self.logger.info(
758 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
759 )
760
rshri932105f2024-07-05 15:11:55 +0000761 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100762 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200763 "deregister_cluster", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +0000764 )
765 self.logger.info(
766 "resource_status is :{} and resource_msg is :{}".format(
767 resource_status, resource_msg
768 )
769 )
770 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200771 db_cluster["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +0000772 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200773 db_cluster["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +0000774
garciadeblas96b94f52024-07-08 16:18:21 +0200775 db_cluster["operatingState"] = "IDLE"
776 db_cluster = self.update_operation_history(
777 db_cluster, workflow_status, resource_status
778 )
shahithya70a3fc92024-11-12 11:01:05 +0000779 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200780 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
rshri932105f2024-07-05 15:11:55 +0000781
garciadeblas96b94f52024-07-08 16:18:21 +0200782 # To delete it from DB
783 if db_cluster["state"] == "DELETED":
784 self.db.del_one("clusters", {"_id": db_cluster["_id"]})
rshri932105f2024-07-05 15:11:55 +0000785 return
786
yshahd940c652024-10-17 06:11:12 +0000787 async def get_creds(self, op_id, db_cluster):
garciadeblas96b94f52024-07-08 16:18:21 +0200788 self.logger.info("Cluster get creds Enter")
789 result, cluster_creds = await self.odu.get_cluster_credentials(db_cluster)
790 if result:
791 db_cluster["credentials"] = cluster_creds
yshahd940c652024-10-17 06:11:12 +0000792 op_len = 0
793 for operations in db_cluster["operationHistory"]:
794 if operations["op_id"] == op_id:
795 db_cluster["operationHistory"][op_len]["result"] = result
796 db_cluster["operationHistory"][op_len]["endDate"] = time()
797 op_len += 1
shahithya70a3fc92024-11-12 11:01:05 +0000798 db_cluster["current_operation"] = None
yshahd940c652024-10-17 06:11:12 +0000799 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000800 return
801
garciadeblas96b94f52024-07-08 16:18:21 +0200802 async def update(self, op_id, op_params, content):
803 self.logger.info("Cluster update Enter")
804 db_cluster = content["cluster"]
yshah771dea82024-07-05 15:11:49 +0000805
rshric3564942024-11-12 18:12:38 +0000806 db_cluster_copy = self.decrypting_key(db_cluster)
807
808 # vim account details
809 db_vim = self.db.get_one("vim_accounts", {"name": db_cluster["vim_account"]})
810 db_cluster_copy["vim_account"] = db_vim
811
garciadeblasadb81e82024-11-08 01:11:46 +0100812 _, workflow_name = await self.odu.launch_workflow(
rshric3564942024-11-12 18:12:38 +0000813 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200814 )
815 workflow_status, workflow_msg = await self.odu.check_workflow_status(
816 workflow_name
817 )
818 self.logger.info(
819 "Workflow Status: {} Workflow Message: {}".format(
820 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +0000821 )
garciadeblas96b94f52024-07-08 16:18:21 +0200822 )
823
824 if workflow_status:
825 db_cluster["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
826 else:
827 db_cluster["resourceState"] = "ERROR"
828
829 db_cluster = self.update_operation_history(db_cluster, workflow_status, None)
830 # self.logger.info("Db content: {}".format(db_content))
831 # self.db.set_one(self.db_collection, {"_id": _id}, db_cluster)
832 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
833
garciadeblas28bff0f2024-09-16 12:53:07 +0200834 # Clean items used in the workflow, no matter if the workflow succeeded
835 clean_status, clean_msg = await self.odu.clean_items_workflow(
rshric3564942024-11-12 18:12:38 +0000836 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas28bff0f2024-09-16 12:53:07 +0200837 )
838 self.logger.info(
839 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
840 )
garciadeblas96b94f52024-07-08 16:18:21 +0200841 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100842 resource_status, resource_msg = await self.check_resource_status(
rshric3564942024-11-12 18:12:38 +0000843 "update_cluster", op_id, op_params, db_cluster_copy
garciadeblas96b94f52024-07-08 16:18:21 +0200844 )
845 self.logger.info(
846 "Resource Status: {} Resource Message: {}".format(
847 resource_status, resource_msg
848 )
849 )
yshah771dea82024-07-05 15:11:49 +0000850
851 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +0200852 db_cluster["resourceState"] = "READY"
yshah771dea82024-07-05 15:11:49 +0000853 else:
garciadeblas96b94f52024-07-08 16:18:21 +0200854 db_cluster["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +0000855
garciadeblas96b94f52024-07-08 16:18:21 +0200856 db_cluster["operatingState"] = "IDLE"
857 db_cluster = self.update_operation_history(
858 db_cluster, workflow_status, resource_status
859 )
860 # self.logger.info("db_cluster: {}".format(db_cluster))
861 # TODO: verify enxtcondition
862 # For the moment, if the workflow completed successfully, then we update the db accordingly.
863 if workflow_status:
864 if "k8s_version" in op_params:
865 db_cluster["k8s_version"] = op_params["k8s_version"]
866 elif "node_count" in op_params:
867 db_cluster["node_count"] = op_params["node_count"]
868 # self.db.set_one(self.db_collection, {"_id": _id}, db_content)
shahithya70a3fc92024-11-12 11:01:05 +0000869 db_cluster["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +0200870 self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
yshah771dea82024-07-05 15:11:49 +0000871 return
872
873
garciadeblas72412282024-11-07 12:41:54 +0100874class CloudCredentialsLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +0000875 db_collection = "vim_accounts"
876
877 def __init__(self, msg, lcm_tasks, config):
878 """
879 Init, Connect to database, filesystem storage, and messaging
880 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
881 :return: None
882 """
garciadeblas72412282024-11-07 12:41:54 +0100883 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +0000884
garciadeblas96b94f52024-07-08 16:18:21 +0200885 async def add(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000886 self.logger.info("Cloud Credentials create")
garciadeblasadb81e82024-11-08 01:11:46 +0100887 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200888 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000889 )
890
891 workflow_status, workflow_msg = await self.odu.check_workflow_status(
892 workflow_name
893 )
894
895 self.logger.info(
896 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
897 )
898
garciadeblas28bff0f2024-09-16 12:53:07 +0200899 # Clean items used in the workflow, no matter if the workflow succeeded
900 clean_status, clean_msg = await self.odu.clean_items_workflow(
901 "create_cloud_credentials", op_id, op_params, content
902 )
903 self.logger.info(
904 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
905 )
906
yshah771dea82024-07-05 15:11:49 +0000907 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100908 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200909 "create_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000910 )
911 self.logger.info(
912 "Resource Status: {} Resource Message: {}".format(
913 resource_status, resource_msg
914 )
915 )
garciadeblas15b8a302024-09-23 12:40:13 +0200916
917 content["_admin"]["operationalState"] = "ENABLED"
918 for operation in content["_admin"]["operations"]:
919 if operation["lcmOperationType"] == "create":
920 operation["operationState"] = "ENABLED"
921 self.logger.info("Content : {}".format(content))
922 self.db.set_one("vim_accounts", {"_id": content["_id"]}, content)
923
yshah771dea82024-07-05 15:11:49 +0000924 return
925
garciadeblas96b94f52024-07-08 16:18:21 +0200926 async def edit(self, op_id, op_params, content):
garciadeblasadb81e82024-11-08 01:11:46 +0100927 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200928 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000929 )
930 workflow_status, workflow_msg = await self.odu.check_workflow_status(
931 workflow_name
932 )
933 self.logger.info(
934 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
935 )
936
garciadeblas28bff0f2024-09-16 12:53:07 +0200937 # Clean items used in the workflow, no matter if the workflow succeeded
938 clean_status, clean_msg = await self.odu.clean_items_workflow(
939 "update_cloud_credentials", op_id, op_params, content
940 )
941 self.logger.info(
942 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
943 )
944
yshah771dea82024-07-05 15:11:49 +0000945 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100946 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200947 "update_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000948 )
949 self.logger.info(
950 "Resource Status: {} Resource Message: {}".format(
951 resource_status, resource_msg
952 )
953 )
954 return
955
garciadeblas96b94f52024-07-08 16:18:21 +0200956 async def remove(self, op_id, op_params, content):
yshah771dea82024-07-05 15:11:49 +0000957 self.logger.info("Cloud Credentials delete")
garciadeblasadb81e82024-11-08 01:11:46 +0100958 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200959 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000960 )
961 workflow_status, workflow_msg = await self.odu.check_workflow_status(
962 workflow_name
963 )
964 self.logger.info(
965 "Workflow Status: {} Workflow Msg: {}".format(workflow_status, workflow_msg)
966 )
967
968 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +0100969 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +0200970 "delete_cloud_credentials", op_id, op_params, content
yshah771dea82024-07-05 15:11:49 +0000971 )
972 self.logger.info(
973 "Resource Status: {} Resource Message: {}".format(
974 resource_status, resource_msg
975 )
976 )
977 self.db.del_one(self.db_collection, {"_id": content["_id"]})
978 return
979
rshri932105f2024-07-05 15:11:55 +0000980
garciadeblas72412282024-11-07 12:41:54 +0100981class K8sAppLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +0000982 def __init__(self, msg, lcm_tasks, config):
983 """
984 Init, Connect to database, filesystem storage, and messaging
985 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
986 :return: None
987 """
garciadeblas72412282024-11-07 12:41:54 +0100988 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +0000989
garciadeblas96b94f52024-07-08 16:18:21 +0200990 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +0000991 self.logger.info("App Create Enter")
992
garciadeblasadb81e82024-11-08 01:11:46 +0100993 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +0200994 "create_profile", op_id, op_params, content
995 )
rshri932105f2024-07-05 15:11:55 +0000996 self.logger.info("workflow_name is :{}".format(workflow_name))
997
garciadeblas96b94f52024-07-08 16:18:21 +0200998 workflow_status, workflow_msg = await self.odu.check_workflow_status(
999 workflow_name
1000 )
rshri932105f2024-07-05 15:11:55 +00001001 self.logger.info(
1002 "workflow_status is :{} and workflow_msg is :{}".format(
1003 workflow_status, workflow_msg
1004 )
1005 )
1006 if workflow_status:
1007 content["state"] = "CREATED"
1008 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1009 else:
1010 content["state"] = "FAILED_CREATION"
1011 content["resourceState"] = "ERROR"
1012 # has to call update_operation_history return content
1013 content = self.update_operation_history(content, workflow_status, None)
1014 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
1015
1016 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001017 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001018 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001019 )
1020 self.logger.info(
1021 "resource_status is :{} and resource_msg is :{}".format(
1022 resource_status, resource_msg
1023 )
1024 )
1025 if resource_status:
1026 content["resourceState"] = "READY"
1027 else:
1028 content["resourceState"] = "ERROR"
1029
1030 content["operatingState"] = "IDLE"
1031 content = self.update_operation_history(
1032 content, workflow_status, resource_status
1033 )
shahithya70a3fc92024-11-12 11:01:05 +00001034 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001035 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
1036
1037 return
1038
garciadeblas96b94f52024-07-08 16:18:21 +02001039 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001040 self.logger.info("App delete Enter")
rshri932105f2024-07-05 15:11:55 +00001041
garciadeblasadb81e82024-11-08 01:11:46 +01001042 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001043 "delete_profile", op_id, op_params, content
1044 )
rshri932105f2024-07-05 15:11:55 +00001045 self.logger.info("workflow_name is :{}".format(workflow_name))
1046
garciadeblas96b94f52024-07-08 16:18:21 +02001047 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1048 workflow_name
1049 )
rshri932105f2024-07-05 15:11:55 +00001050 self.logger.info(
1051 "workflow_status is :{} and workflow_msg is :{}".format(
1052 workflow_status, workflow_msg
1053 )
1054 )
1055 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001056 content["state"] = "DELETED"
1057 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001058 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001059 content["state"] = "FAILED_DELETION"
1060 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001061 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001062 content = self.update_operation_history(content, workflow_status, None)
rshri932105f2024-07-05 15:11:55 +00001063 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
1064
1065 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001066 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001067 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001068 )
1069 self.logger.info(
1070 "resource_status is :{} and resource_msg is :{}".format(
1071 resource_status, resource_msg
1072 )
1073 )
1074 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001075 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001076 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001077 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001078
garciadeblas96b94f52024-07-08 16:18:21 +02001079 content["operatingState"] = "IDLE"
1080 content = self.update_operation_history(
1081 content, workflow_status, resource_status
1082 )
shahithya70a3fc92024-11-12 11:01:05 +00001083 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001084 self.db.set_one("k8sapp", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001085
garciadeblas96b94f52024-07-08 16:18:21 +02001086 # To delete it from DB
1087 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001088 self.db.del_one("k8sapp", {"_id": content["_id"]})
1089 return
1090
1091
garciadeblas72412282024-11-07 12:41:54 +01001092class K8sResourceLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001093 def __init__(self, msg, lcm_tasks, config):
1094 """
1095 Init, Connect to database, filesystem storage, and messaging
1096 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1097 :return: None
1098 """
garciadeblas72412282024-11-07 12:41:54 +01001099 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001100
garciadeblas96b94f52024-07-08 16:18:21 +02001101 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001102 self.logger.info("Resource Create Enter")
1103
garciadeblasadb81e82024-11-08 01:11:46 +01001104 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001105 "create_profile", op_id, op_params, content
1106 )
rshri932105f2024-07-05 15:11:55 +00001107 self.logger.info("workflow_name is :{}".format(workflow_name))
1108
garciadeblas96b94f52024-07-08 16:18:21 +02001109 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1110 workflow_name
1111 )
rshri932105f2024-07-05 15:11:55 +00001112 self.logger.info(
1113 "workflow_status is :{} and workflow_msg is :{}".format(
1114 workflow_status, workflow_msg
1115 )
1116 )
1117 if workflow_status:
1118 content["state"] = "CREATED"
1119 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1120 else:
1121 content["state"] = "FAILED_CREATION"
1122 content["resourceState"] = "ERROR"
1123 # has to call update_operation_history return content
1124 content = self.update_operation_history(content, workflow_status, None)
1125 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1126
1127 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001128 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001129 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001130 )
1131 self.logger.info(
1132 "resource_status is :{} and resource_msg is :{}".format(
1133 resource_status, resource_msg
1134 )
1135 )
1136 if resource_status:
1137 content["resourceState"] = "READY"
1138 else:
1139 content["resourceState"] = "ERROR"
1140
1141 content["operatingState"] = "IDLE"
1142 content = self.update_operation_history(
1143 content, workflow_status, resource_status
1144 )
shahithya70a3fc92024-11-12 11:01:05 +00001145 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001146 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
1147
1148 return
1149
garciadeblas96b94f52024-07-08 16:18:21 +02001150 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001151 self.logger.info("Resource delete Enter")
garciadeblas96b94f52024-07-08 16:18:21 +02001152 content = self.db.get_one("k8sresource", {"_id": content["_id"]})
rshri932105f2024-07-05 15:11:55 +00001153
garciadeblasadb81e82024-11-08 01:11:46 +01001154 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001155 "delete_profile", op_id, op_params, content
1156 )
rshri932105f2024-07-05 15:11:55 +00001157 self.logger.info("workflow_name is :{}".format(workflow_name))
1158
garciadeblas96b94f52024-07-08 16:18:21 +02001159 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1160 workflow_name
1161 )
rshri932105f2024-07-05 15:11:55 +00001162 self.logger.info(
1163 "workflow_status is :{} and workflow_msg is :{}".format(
1164 workflow_status, workflow_msg
1165 )
1166 )
1167 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001168 content["state"] = "DELETED"
1169 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001170 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001171 content["state"] = "FAILED_DELETION"
1172 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001173 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001174 content = self.update_operation_history(content, workflow_status, None)
1175 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001176
1177 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001178 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001179 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001180 )
1181 self.logger.info(
1182 "resource_status is :{} and resource_msg is :{}".format(
1183 resource_status, resource_msg
1184 )
1185 )
1186 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001187 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001188 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001189 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001190
garciadeblas96b94f52024-07-08 16:18:21 +02001191 content["operatingState"] = "IDLE"
1192 content = self.update_operation_history(
1193 content, workflow_status, resource_status
1194 )
shahithya70a3fc92024-11-12 11:01:05 +00001195 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001196 self.db.set_one("k8sresource", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001197
garciadeblas96b94f52024-07-08 16:18:21 +02001198 # To delete it from DB
1199 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001200 self.db.del_one("k8sresource", {"_id": content["_id"]})
1201 return
1202
1203
garciadeblas72412282024-11-07 12:41:54 +01001204class K8sInfraControllerLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001205 def __init__(self, msg, lcm_tasks, config):
1206 """
1207 Init, Connect to database, filesystem storage, and messaging
1208 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1209 :return: None
1210 """
garciadeblas72412282024-11-07 12:41:54 +01001211 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001212
garciadeblas96b94f52024-07-08 16:18:21 +02001213 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001214 self.logger.info("Infra controller Create Enter")
1215
garciadeblasadb81e82024-11-08 01:11:46 +01001216 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001217 "create_profile", op_id, op_params, content
1218 )
rshri932105f2024-07-05 15:11:55 +00001219 self.logger.info("workflow_name is :{}".format(workflow_name))
1220
garciadeblas96b94f52024-07-08 16:18:21 +02001221 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1222 workflow_name
1223 )
rshri932105f2024-07-05 15:11:55 +00001224 self.logger.info(
1225 "workflow_status is :{} and workflow_msg is :{}".format(
1226 workflow_status, workflow_msg
1227 )
1228 )
1229 if workflow_status:
1230 content["state"] = "CREATED"
1231 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1232 else:
1233 content["state"] = "FAILED_CREATION"
1234 content["resourceState"] = "ERROR"
1235 # has to call update_operation_history return content
1236 content = self.update_operation_history(content, workflow_status, None)
1237 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1238
1239 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001240 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001241 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001242 )
1243 self.logger.info(
1244 "resource_status is :{} and resource_msg is :{}".format(
1245 resource_status, resource_msg
1246 )
1247 )
1248 if resource_status:
1249 content["resourceState"] = "READY"
1250 else:
1251 content["resourceState"] = "ERROR"
1252
1253 content["operatingState"] = "IDLE"
1254 content = self.update_operation_history(
1255 content, workflow_status, resource_status
1256 )
shahithya70a3fc92024-11-12 11:01:05 +00001257 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001258 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
1259
1260 return
1261
garciadeblas96b94f52024-07-08 16:18:21 +02001262 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001263 self.logger.info("Infra controller delete Enter")
rshri932105f2024-07-05 15:11:55 +00001264
garciadeblasadb81e82024-11-08 01:11:46 +01001265 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001266 "delete_profile", op_id, op_params, content
1267 )
rshri932105f2024-07-05 15:11:55 +00001268 self.logger.info("workflow_name is :{}".format(workflow_name))
1269
garciadeblas96b94f52024-07-08 16:18:21 +02001270 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1271 workflow_name
1272 )
rshri932105f2024-07-05 15:11:55 +00001273 self.logger.info(
1274 "workflow_status is :{} and workflow_msg is :{}".format(
1275 workflow_status, workflow_msg
1276 )
1277 )
1278 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001279 content["state"] = "DELETED"
1280 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001281 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001282 content["state"] = "FAILED_DELETION"
1283 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001284 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001285 content = self.update_operation_history(content, workflow_status, None)
1286 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001287
1288 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001289 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001290 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001291 )
1292 self.logger.info(
1293 "resource_status is :{} and resource_msg is :{}".format(
1294 resource_status, resource_msg
1295 )
1296 )
1297 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001298 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001299 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001300 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001301
garciadeblas96b94f52024-07-08 16:18:21 +02001302 content["operatingState"] = "IDLE"
1303 content = self.update_operation_history(
1304 content, workflow_status, resource_status
1305 )
shahithya70a3fc92024-11-12 11:01:05 +00001306 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001307 self.db.set_one("k8sinfra_controller", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001308
garciadeblas96b94f52024-07-08 16:18:21 +02001309 # To delete it from DB
1310 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001311 self.db.del_one("k8sinfra_controller", {"_id": content["_id"]})
1312 return
1313
1314
garciadeblas72412282024-11-07 12:41:54 +01001315class K8sInfraConfigLcm(GitOpsLcm):
rshri932105f2024-07-05 15:11:55 +00001316 def __init__(self, msg, lcm_tasks, config):
1317 """
1318 Init, Connect to database, filesystem storage, and messaging
1319 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1320 :return: None
1321 """
garciadeblas72412282024-11-07 12:41:54 +01001322 super().__init__(msg, lcm_tasks, config)
rshri932105f2024-07-05 15:11:55 +00001323
garciadeblas96b94f52024-07-08 16:18:21 +02001324 async def create(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001325 self.logger.info("Infra config Create Enter")
1326
garciadeblasadb81e82024-11-08 01:11:46 +01001327 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001328 "create_profile", op_id, op_params, content
1329 )
rshri932105f2024-07-05 15:11:55 +00001330 self.logger.info("workflow_name is :{}".format(workflow_name))
1331
garciadeblas96b94f52024-07-08 16:18:21 +02001332 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1333 workflow_name
1334 )
rshri932105f2024-07-05 15:11:55 +00001335 self.logger.info(
1336 "workflow_status is :{} and workflow_msg is :{}".format(
1337 workflow_status, workflow_msg
1338 )
1339 )
1340 if workflow_status:
1341 content["state"] = "CREATED"
1342 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1343 else:
1344 content["state"] = "FAILED_CREATION"
1345 content["resourceState"] = "ERROR"
1346 # has to call update_operation_history return content
1347 content = self.update_operation_history(content, workflow_status, None)
1348 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1349
1350 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001351 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001352 "create_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001353 )
1354 self.logger.info(
1355 "resource_status is :{} and resource_msg is :{}".format(
1356 resource_status, resource_msg
1357 )
1358 )
1359 if resource_status:
1360 content["resourceState"] = "READY"
1361 else:
1362 content["resourceState"] = "ERROR"
1363
1364 content["operatingState"] = "IDLE"
1365 content = self.update_operation_history(
1366 content, workflow_status, resource_status
1367 )
shahithya70a3fc92024-11-12 11:01:05 +00001368 content["current_operation"] = None
rshri932105f2024-07-05 15:11:55 +00001369 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
1370
1371 return
1372
garciadeblas96b94f52024-07-08 16:18:21 +02001373 async def delete(self, op_id, op_params, content):
rshri932105f2024-07-05 15:11:55 +00001374 self.logger.info("Infra config delete Enter")
1375
garciadeblasadb81e82024-11-08 01:11:46 +01001376 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001377 "delete_profile", op_id, op_params, content
1378 )
rshri932105f2024-07-05 15:11:55 +00001379 self.logger.info("workflow_name is :{}".format(workflow_name))
rshri932105f2024-07-05 15:11:55 +00001380
garciadeblas96b94f52024-07-08 16:18:21 +02001381 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1382 workflow_name
1383 )
rshri932105f2024-07-05 15:11:55 +00001384 self.logger.info(
1385 "workflow_status is :{} and workflow_msg is :{}".format(
1386 workflow_status, workflow_msg
1387 )
1388 )
1389 if workflow_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001390 content["state"] = "DELETED"
1391 content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
rshri932105f2024-07-05 15:11:55 +00001392 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001393 content["state"] = "FAILED_DELETION"
1394 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001395 # has to call update_operation_history return content
garciadeblas96b94f52024-07-08 16:18:21 +02001396 content = self.update_operation_history(content, workflow_status, None)
1397 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001398
garciadeblas72412282024-11-07 12:41:54 +01001399 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001400 "delete_profile", op_id, op_params, content
rshri932105f2024-07-05 15:11:55 +00001401 )
1402 self.logger.info(
1403 "resource_status is :{} and resource_msg is :{}".format(
1404 resource_status, resource_msg
1405 )
1406 )
1407 if resource_status:
garciadeblas96b94f52024-07-08 16:18:21 +02001408 content["resourceState"] = "READY"
rshri932105f2024-07-05 15:11:55 +00001409 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001410 content["resourceState"] = "ERROR"
rshri932105f2024-07-05 15:11:55 +00001411
garciadeblas96b94f52024-07-08 16:18:21 +02001412 content["operatingState"] = "IDLE"
1413 content = self.update_operation_history(
1414 content, workflow_status, resource_status
1415 )
shahithya70a3fc92024-11-12 11:01:05 +00001416 content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001417 self.db.set_one("k8sinfra_config", {"_id": content["_id"]}, content)
rshri932105f2024-07-05 15:11:55 +00001418
garciadeblas96b94f52024-07-08 16:18:21 +02001419 # To delete it from DB
1420 if content["state"] == "DELETED":
rshri932105f2024-07-05 15:11:55 +00001421 self.db.del_one("k8sinfra_config", {"_id": content["_id"]})
1422 return
yshah771dea82024-07-05 15:11:49 +00001423
1424
garciadeblas72412282024-11-07 12:41:54 +01001425class OkaLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001426 db_collection = "okas"
1427
1428 def __init__(self, msg, lcm_tasks, config):
1429 """
1430 Init, Connect to database, filesystem storage, and messaging
1431 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1432 :return: None
1433 """
garciadeblas72412282024-11-07 12:41:54 +01001434 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001435
garciadeblas96b94f52024-07-08 16:18:21 +02001436 async def create(self, op_id, op_params, content):
1437 self.logger.info("OKA Create Enter")
1438 db_content = content
yshah771dea82024-07-05 15:11:49 +00001439
garciadeblasadb81e82024-11-08 01:11:46 +01001440 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001441 "create_oka", op_id, op_params, db_content
1442 )
1443 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1444 workflow_name
1445 )
1446 self.logger.info(
1447 "Workflow Status: {} Workflow Message: {}".format(
1448 workflow_status, workflow_msg
1449 )
1450 )
yshah771dea82024-07-05 15:11:49 +00001451
1452 if workflow_status:
1453 db_content["state"] = "CREATED"
1454 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1455 else:
1456 db_content["state"] = "FAILED_CREATION"
1457 db_content["resourceState"] = "ERROR"
1458
1459 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001460 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001461
1462 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001463 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001464 "create_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001465 )
garciadeblas96b94f52024-07-08 16:18:21 +02001466 self.logger.info(
1467 "Resource Status: {} Resource Message: {}".format(
1468 resource_status, resource_msg
1469 )
1470 )
yshah771dea82024-07-05 15:11:49 +00001471
1472 if resource_status:
1473 db_content["resourceState"] = "READY"
1474 else:
1475 db_content["resourceState"] = "ERROR"
1476
1477 # self.logger.info("Db content: {}".format(db_content))
1478 db_content = self.update_operation_history(
1479 db_content, workflow_status, resource_status
1480 )
1481
1482 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001483 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001484 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001485
1486 return
1487
garciadeblas96b94f52024-07-08 16:18:21 +02001488 async def edit(self, op_id, op_params, content):
1489 self.logger.info("OKA Edit Enter")
1490 db_content = content
yshah771dea82024-07-05 15:11:49 +00001491
garciadeblasadb81e82024-11-08 01:11:46 +01001492 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001493 "update_oka", op_id, op_params, content
1494 )
1495 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1496 workflow_name
1497 )
1498 self.logger.info(
1499 "Workflow Status: {} Workflow Message: {}".format(
1500 workflow_status, workflow_msg
1501 )
1502 )
yshah771dea82024-07-05 15:11:49 +00001503
1504 if workflow_status:
1505 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1506 else:
1507 db_content["resourceState"] = "ERROR"
1508
1509 db_content = self.update_operation_history(db_content, workflow_status, None)
1510 # self.logger.info("Db content: {}".format(db_content))
garciadeblas96b94f52024-07-08 16:18:21 +02001511 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001512
1513 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001514 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001515 "update_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001516 )
garciadeblas96b94f52024-07-08 16:18:21 +02001517 self.logger.info(
1518 "Resource Status: {} Resource Message: {}".format(
1519 resource_status, resource_msg
1520 )
1521 )
yshah771dea82024-07-05 15:11:49 +00001522
1523 if resource_status:
1524 db_content["resourceState"] = "READY"
1525 else:
1526 db_content["resourceState"] = "ERROR"
1527
1528 db_content = self.update_operation_history(
1529 db_content, workflow_status, resource_status
1530 )
1531
1532 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001533 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001534 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001535 return
1536
garciadeblas96b94f52024-07-08 16:18:21 +02001537 async def delete(self, op_id, op_params, content):
1538 self.logger.info("OKA delete Enter")
1539 db_content = content
yshah771dea82024-07-05 15:11:49 +00001540
garciadeblasadb81e82024-11-08 01:11:46 +01001541 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001542 "delete_oka", op_id, op_params, content
1543 )
1544 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1545 workflow_name
1546 )
1547 self.logger.info(
1548 "Workflow Status: {} Workflow Message: {}".format(
1549 workflow_status, workflow_msg
1550 )
1551 )
yshah771dea82024-07-05 15:11:49 +00001552
1553 if workflow_status:
1554 db_content["state"] = "DELETED"
1555 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1556 else:
1557 db_content["state"] = "FAILED_DELETION"
1558 db_content["resourceState"] = "ERROR"
1559
1560 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001561 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001562
1563 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001564 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001565 "delete_oka", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001566 )
garciadeblas96b94f52024-07-08 16:18:21 +02001567 self.logger.info(
1568 "Resource Status: {} Resource Message: {}".format(
1569 resource_status, resource_msg
1570 )
1571 )
yshah771dea82024-07-05 15:11:49 +00001572
1573 if resource_status:
1574 db_content["resourceState"] = "READY"
1575 else:
1576 db_content["resourceState"] = "ERROR"
1577
1578 db_content = self.update_operation_history(
1579 db_content, workflow_status, resource_status
1580 )
1581
1582 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001583 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001584 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001585
1586 if db_content["state"] == "DELETED":
garciadeblas96b94f52024-07-08 16:18:21 +02001587 self.db.del_one(self.db_collection, {"_id": db_content["_id"]})
yshah771dea82024-07-05 15:11:49 +00001588 return
1589
1590
garciadeblas72412282024-11-07 12:41:54 +01001591class KsuLcm(GitOpsLcm):
yshah771dea82024-07-05 15:11:49 +00001592 db_collection = "ksus"
1593
1594 def __init__(self, msg, lcm_tasks, config):
1595 """
1596 Init, Connect to database, filesystem storage, and messaging
1597 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
1598 :return: None
1599 """
garciadeblas72412282024-11-07 12:41:54 +01001600 super().__init__(msg, lcm_tasks, config)
yshah771dea82024-07-05 15:11:49 +00001601
garciadeblas96b94f52024-07-08 16:18:21 +02001602 async def create(self, op_id, op_params, content):
1603 self.logger.info("ksu Create Enter")
yshah771dea82024-07-05 15:11:49 +00001604
garciadeblasadb81e82024-11-08 01:11:46 +01001605 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001606 "create_ksus", op_id, op_params, content
1607 )
1608 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1609 workflow_name
1610 )
1611 self.logger.info(
1612 "Workflow Status: {} Workflow Message: {}".format(
1613 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001614 )
garciadeblas96b94f52024-07-08 16:18:21 +02001615 )
yshah771dea82024-07-05 15:11:49 +00001616
garciadeblas96b94f52024-07-08 16:18:21 +02001617 for db_ksu in content:
1618 if workflow_status:
1619 db_ksu["state"] = "CREATED"
1620 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001621 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001622 db_ksu["state"] = "FAILED_CREATION"
1623 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001624
garciadeblas96b94f52024-07-08 16:18:21 +02001625 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1626 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1627
garciadeblasd8429852024-10-17 15:30:30 +02001628 # Clean items used in the workflow, no matter if the workflow succeeded
1629 clean_status, clean_msg = await self.odu.clean_items_workflow(
1630 "create_ksus", op_id, op_params, content
1631 )
1632 self.logger.info(
1633 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1634 )
1635
garciadeblas96b94f52024-07-08 16:18:21 +02001636 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001637 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001638 "create_ksus", op_id, op_params, content
1639 )
1640 self.logger.info(
1641 "Resource Status: {} Resource Message: {}".format(
1642 resource_status, resource_msg
1643 )
yshah771dea82024-07-05 15:11:49 +00001644 )
1645
garciadeblas96b94f52024-07-08 16:18:21 +02001646 for db_ksu in content:
1647 if resource_status:
1648 db_ksu["resourceState"] = "READY"
1649 else:
1650 db_ksu["resourceState"] = "ERROR"
1651
1652 db_ksu = self.update_operation_history(
1653 db_ksu, workflow_status, resource_status
1654 )
1655
1656 for db_ksu in content:
1657 db_ksu["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001658 db_ksu["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001659 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
yshah771dea82024-07-05 15:11:49 +00001660
1661 return
1662
garciadeblas96b94f52024-07-08 16:18:21 +02001663 async def edit(self, op_id, op_params, content):
1664 self.logger.info("ksu edit Enter")
yshah771dea82024-07-05 15:11:49 +00001665
garciadeblasadb81e82024-11-08 01:11:46 +01001666 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001667 "update_ksus", op_id, op_params, content
1668 )
1669 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1670 workflow_name
1671 )
1672 self.logger.info(
1673 "Workflow Status: {} Workflow Message: {}".format(
1674 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001675 )
garciadeblas96b94f52024-07-08 16:18:21 +02001676 )
yshah771dea82024-07-05 15:11:49 +00001677
garciadeblas96b94f52024-07-08 16:18:21 +02001678 for db_ksu in content:
1679 if workflow_status:
1680 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001681 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001682 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001683
garciadeblas96b94f52024-07-08 16:18:21 +02001684 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1685 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1686
garciadeblasd8429852024-10-17 15:30:30 +02001687 # Clean items used in the workflow, no matter if the workflow succeeded
1688 clean_status, clean_msg = await self.odu.clean_items_workflow(
1689 "create_ksus", op_id, op_params, content
1690 )
1691 self.logger.info(
1692 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
1693 )
garciadeblas96b94f52024-07-08 16:18:21 +02001694 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001695 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001696 "update_ksus", op_id, op_params, content
1697 )
1698 self.logger.info(
1699 "Resource Status: {} Resource Message: {}".format(
1700 resource_status, resource_msg
1701 )
yshah771dea82024-07-05 15:11:49 +00001702 )
1703
garciadeblas96b94f52024-07-08 16:18:21 +02001704 for db_ksu in content:
1705 if resource_status:
1706 db_ksu["resourceState"] = "READY"
1707 else:
1708 db_ksu["resourceState"] = "ERROR"
1709
1710 db_ksu = self.update_operation_history(
1711 db_ksu, workflow_status, resource_status
1712 )
1713
1714 for db_ksu, ksu_params in zip(content, op_params):
1715 db_ksu["operatingState"] = "IDLE"
1716 if workflow_status:
1717 db_ksu["name"] = ksu_params["name"]
1718 db_ksu["description"] = ksu_params["description"]
1719 db_ksu["profile"]["profile_type"] = ksu_params["profile"][
1720 "profile_type"
1721 ]
1722 db_ksu["profile"]["_id"] = ksu_params["profile"]["_id"]
1723 db_ksu["oka"] = ksu_params["oka"]
shahithya70a3fc92024-11-12 11:01:05 +00001724 db_ksu["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001725 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1726
yshah771dea82024-07-05 15:11:49 +00001727 return
1728
garciadeblas96b94f52024-07-08 16:18:21 +02001729 async def delete(self, op_id, op_params, content):
1730 self.logger.info("ksu delete Enter")
yshah771dea82024-07-05 15:11:49 +00001731
garciadeblasadb81e82024-11-08 01:11:46 +01001732 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001733 "delete_ksus", op_id, op_params, content
1734 )
1735 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1736 workflow_name
1737 )
1738 self.logger.info(
1739 "Workflow Status: {} Workflow Message: {}".format(
1740 workflow_status, workflow_msg
yshah771dea82024-07-05 15:11:49 +00001741 )
garciadeblas96b94f52024-07-08 16:18:21 +02001742 )
yshah771dea82024-07-05 15:11:49 +00001743
garciadeblas96b94f52024-07-08 16:18:21 +02001744 for db_ksu in content:
1745 if workflow_status:
1746 db_ksu["state"] = "DELETED"
1747 db_ksu["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
yshah771dea82024-07-05 15:11:49 +00001748 else:
garciadeblas96b94f52024-07-08 16:18:21 +02001749 db_ksu["state"] = "FAILED_DELETION"
1750 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001751
garciadeblas96b94f52024-07-08 16:18:21 +02001752 db_ksu = self.update_operation_history(db_ksu, workflow_status, None)
1753 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1754
1755 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001756 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001757 "delete_ksus", op_id, op_params, content
1758 )
1759 self.logger.info(
1760 "Resource Status: {} Resource Message: {}".format(
1761 resource_status, resource_msg
1762 )
yshah771dea82024-07-05 15:11:49 +00001763 )
1764
garciadeblas96b94f52024-07-08 16:18:21 +02001765 for db_ksu in content:
1766 if resource_status:
1767 db_ksu["resourceState"] = "READY"
1768 else:
1769 db_ksu["resourceState"] = "ERROR"
yshah771dea82024-07-05 15:11:49 +00001770
garciadeblas96b94f52024-07-08 16:18:21 +02001771 db_ksu = self.update_operation_history(
1772 db_ksu, workflow_status, resource_status
1773 )
1774
1775 for db_ksu in content:
1776 db_ksu["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001777 db_ksu["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001778 self.db.set_one(self.db_collection, {"_id": db_ksu["_id"]}, db_ksu)
1779
1780 if db_ksu["state"] == "DELETED":
1781 self.db.del_one(self.db_collection, {"_id": db_ksu["_id"]})
yshah771dea82024-07-05 15:11:49 +00001782 return
1783
garciadeblas96b94f52024-07-08 16:18:21 +02001784 async def clone(self, op_id, op_params, db_content):
1785 self.logger.info("ksu clone Enter")
yshah771dea82024-07-05 15:11:49 +00001786
garciadeblasadb81e82024-11-08 01:11:46 +01001787 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001788 "clone_ksus", op_id, op_params, db_content
1789 )
1790 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1791 workflow_name
1792 )
1793 self.logger.info(
1794 "Workflow Status: {} Workflow Message: {}".format(
1795 workflow_status, workflow_msg
1796 )
1797 )
yshah771dea82024-07-05 15:11:49 +00001798
1799 if workflow_status:
1800 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1801 else:
1802 db_content["resourceState"] = "ERROR"
1803
1804 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001805 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001806
1807 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001808 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001809 "clone_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001810 )
garciadeblas96b94f52024-07-08 16:18:21 +02001811 self.logger.info(
1812 "Resource Status: {} Resource Message: {}".format(
1813 resource_status, resource_msg
1814 )
1815 )
yshah771dea82024-07-05 15:11:49 +00001816
1817 if resource_status:
1818 db_content["resourceState"] = "READY"
1819 else:
1820 db_content["resourceState"] = "ERROR"
1821
1822 db_content = self.update_operation_history(
1823 db_content, workflow_status, resource_status
1824 )
1825
1826 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001827 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001828 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001829 return
1830
garciadeblas96b94f52024-07-08 16:18:21 +02001831 async def move(self, op_id, op_params, db_content):
1832 self.logger.info("ksu move Enter")
yshah771dea82024-07-05 15:11:49 +00001833
garciadeblasadb81e82024-11-08 01:11:46 +01001834 _, workflow_name = await self.odu.launch_workflow(
garciadeblas96b94f52024-07-08 16:18:21 +02001835 "move_ksus", op_id, op_params, db_content
1836 )
1837 workflow_status, workflow_msg = await self.odu.check_workflow_status(
1838 workflow_name
1839 )
1840 self.logger.info(
1841 "Workflow Status: {} Workflow Message: {}".format(
1842 workflow_status, workflow_msg
1843 )
1844 )
yshah771dea82024-07-05 15:11:49 +00001845
1846 if workflow_status:
1847 db_content["resourceState"] = "IN_PROGRESS.GIT_SYNCED"
1848 else:
1849 db_content["resourceState"] = "ERROR"
1850
1851 db_content = self.update_operation_history(db_content, workflow_status, None)
garciadeblas96b94f52024-07-08 16:18:21 +02001852 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001853
1854 if workflow_status:
garciadeblas72412282024-11-07 12:41:54 +01001855 resource_status, resource_msg = await self.check_resource_status(
garciadeblas96b94f52024-07-08 16:18:21 +02001856 "move_ksus", op_id, op_params, db_content
yshah771dea82024-07-05 15:11:49 +00001857 )
garciadeblas96b94f52024-07-08 16:18:21 +02001858 self.logger.info(
1859 "Resource Status: {} Resource Message: {}".format(
1860 resource_status, resource_msg
1861 )
1862 )
yshah771dea82024-07-05 15:11:49 +00001863 if resource_status:
1864 db_content["resourceState"] = "READY"
1865 else:
1866 db_content["resourceState"] = "ERROR"
1867
1868 db_content = self.update_operation_history(
1869 db_content, workflow_status, resource_status
1870 )
1871
1872 db_content["operatingState"] = "IDLE"
shahithya70a3fc92024-11-12 11:01:05 +00001873 db_content["current_operation"] = None
garciadeblas96b94f52024-07-08 16:18:21 +02001874 self.db.set_one(self.db_collection, {"_id": db_content["_id"]}, db_content)
yshah771dea82024-07-05 15:11:49 +00001875 return