From b816d822dcc61a709b22f475085c3f94dceca61d Mon Sep 17 00:00:00 2001 From: ksaikiranr Date: Wed, 17 Mar 2021 12:50:20 +0530 Subject: [PATCH] Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard Added functions to get action list, config list and executed action list for KNF Change-Id: Ibec764c719da5507168c474cf48fc23efbb9c846 Signed-off-by: jayaramans Signed-off-by: ksaikiranr --- n2vc/k8s_juju_conn.py | 51 +++++++++++++++++++++++++++++++++++++++---- n2vc/utils.py | 31 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/n2vc/k8s_juju_conn.py b/n2vc/k8s_juju_conn.py index 0e9d547..3130216 100644 --- a/n2vc/k8s_juju_conn.py +++ b/n2vc/k8s_juju_conn.py @@ -27,6 +27,7 @@ from n2vc.kubectl import Kubectl, CORE_CLIENT, RBAC_CLIENT from .exceptions import MethodNotImplemented from n2vc.utils import base64_to_cacert from n2vc.libjuju import Libjuju +from n2vc.utils import obj_to_dict, obj_to_yaml from kubernetes.client.models import ( V1ClusterRole, @@ -373,6 +374,8 @@ class K8sJujuConnector(K8sConnector): bundle, model_name=kdu_instance, wait=atomic, timeout=timeout ) os.chdir(previous_workdir) + if self.on_update_db: + await self.on_update_db(cluster_uuid, kdu_instance, filter=db_dict["filter"]) return True async def instances_list(self, cluster_uuid: str) -> list: @@ -504,6 +507,8 @@ class K8sJujuConnector(K8sConnector): raise K8sException( "status is not completed: {} output: {}".format(status, output) ) + if self.on_update_db: + await self.on_update_db(cluster_uuid, kdu_instance, filter=db_dict["filter"]) return output @@ -587,6 +592,8 @@ class K8sJujuConnector(K8sConnector): self, cluster_uuid: str, kdu_instance: str, + complete_status: bool = False, + yaml_format: bool = False ) -> dict: """Get the status of the KDU @@ -594,18 +601,54 @@ class K8sJujuConnector(K8sConnector): :param cluster_uuid str: The UUID of the cluster :param kdu_instance str: The unique id of the KDU instance + :param complete_status: To get the complete_status of the KDU + :param yaml_format: To get the status in proper format for NSR record :return: Returns a dictionary containing namespace, state, resources, - and deployment_time. + and deployment_time and returns complete_status if complete_status is True """ status = {} + model_status = await self.libjuju.get_model_status(kdu_instance) - for name in model_status.applications: - application = model_status.applications[name] - status[name] = {"status": application["status"]["status"]} + + if not complete_status: + for name in model_status.applications: + application = model_status.applications[name] + status[name] = {"status": application["status"]["status"]} + else: + if yaml_format: + return obj_to_yaml(model_status) + else: + return obj_to_dict(model_status) return status + async def update_vca_status(self, vcastatus: dict, kdu_instance: str): + """ + Add all configs, actions, executed actions of all applications in a model to vcastatus dict + + :param vcastatus dict: dict containing vcastatus + :param kdu_instance str: The unique id of the KDU instance + + :return: None + """ + try: + for model_name in vcastatus: + # Adding executed actions + vcastatus[model_name]["executedActions"] = \ + await self.libjuju.get_executed_actions(kdu_instance) + + for application in vcastatus[model_name]["applications"]: + # Adding application actions + vcastatus[model_name]["applications"][application]["actions"] = \ + await self.libjuju.get_actions(application, kdu_instance) + # Adding application configs + vcastatus[model_name]["applications"][application]["configs"] = \ + await self.libjuju.get_application_configs(kdu_instance, application) + + except Exception as e: + self.log.debug("Error in updating vca status: {}".format(str(e))) + async def get_services( self, cluster_uuid: str, kdu_instance: str, namespace: str ) -> list: diff --git a/n2vc/utils.py b/n2vc/utils.py index 16a4733..6e0f2c0 100644 --- a/n2vc/utils.py +++ b/n2vc/utils.py @@ -15,6 +15,7 @@ import base64 import re import binascii +import yaml from enum import Enum from juju.machine import Machine from juju.application import Application @@ -120,3 +121,33 @@ DB_DATA = Dict( ) } ) + + +def obj_to_yaml(obj: object) -> str: + """ + Converts object to yaml format + :return: yaml data + """ + # dump to yaml + dump_text = yaml.dump(obj, default_flow_style=False, indent=2) + # split lines + lines = dump_text.splitlines() + # remove !!python/object tags + yaml_text = "" + for line in lines: + index = line.find("!!python/object") + if index >= 0: + line = line[:index] + yaml_text += line + "\n" + return yaml_text + + +def obj_to_dict(obj: object) -> dict: + """ + Converts object to dictionary format + :return: dict data + """ + # convert obj to yaml + yaml_text = obj_to_yaml(obj) + # parse to dict + return yaml.load(yaml_text, Loader=yaml.Loader) -- 2.25.1