Feature-9904: Enhancing NG-UI to enable Juju operational view dashboard 95/10495/4
authorksaikiranr <saikiran.k@tataelxsi.co.in>
Wed, 17 Mar 2021 07:20:20 +0000 (12:50 +0530)
committerksaikiranr <saikiran.k@tataelxsi.co.in>
Wed, 14 Apr 2021 04:31:18 +0000 (10:01 +0530)
Added functions to get action list, config list and executed action list for KNF

Change-Id: Ibec764c719da5507168c474cf48fc23efbb9c846
Signed-off-by: jayaramans <selvi.j@tataelxsi.co.in>
Signed-off-by: ksaikiranr <saikiran.k@tataelxsi.co.in>
n2vc/k8s_juju_conn.py
n2vc/utils.py

index 0e9d547..3130216 100644 (file)
@@ -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:
index 16a4733..6e0f2c0 100644 (file)
@@ -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)