Update cluster-list and show commands to talk both new and old APIs 73/14673/3
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 24 Oct 2024 09:14:34 +0000 (11:14 +0200)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Wed, 30 Oct 2024 09:05:13 +0000 (10:05 +0100)
Change-Id: Icf2f60ae45ae93e8976b475636c86d9fda761506
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/cli_commands/cluster.py
osmclient/cli_commands/common.py
osmclient/sol005/cluster.py

index 64dc7c6..e486eee 100755 (executable)
@@ -102,9 +102,33 @@ def cluster_delete(ctx, name, force):
 def cluster_list(ctx, filter, output):
     """list all K8s clusters"""
     logger.debug("")
-    extras = {"Created": "created"}
+    extra_items = [
+        {
+            "header": "Created by OSM",
+            "item": "created",
+        },
+        {
+            "header": "Version",
+            "item": "k8s_version",
+        },
+        {
+            "header": "VIM account",
+            "item": "vim_account",
+        },
+        {
+            "header": "Bootstrap",
+            "item": "bootstrap",
+        },
+        {
+            "header": "Description",
+            "item": "description",
+        },
+    ]
     common.generic_list(
-        callback=ctx.obj.cluster.list, filter=filter, format=output, extras=extras
+        callback=ctx.obj.cluster.list_both,
+        filter=filter,
+        format=output,
+        extras=extra_items,
     )
 
 
@@ -121,7 +145,7 @@ def cluster_show(ctx, name, output):
     NAME: name or ID of the K8s cluster
     """
     logger.debug("")
-    common.generic_show(callback=ctx.obj.cluster.get, name=name, format=output)
+    common.generic_show(callback=ctx.obj.cluster.get_both, name=name, format=output)
 
 
 @click.command(name="cluster-update", short_help="updates a K8s cluster")
index 0a3d8c7..2e3c8c4 100644 (file)
@@ -93,13 +93,13 @@ def generic_show(callback, name, format="table"):
 
 def generic_list(callback, filter, format="table", extras=None):
     if not extras:
-        extras = {}
+        extras = []
     logger.debug("")
     if filter:
         filter = "&".join(filter)
     resp = callback(filter)
-    headers = ["Name", "Id", "State", "Operating State", "Resource State"]
-    headers.extend(extras.keys())
+    headers = ["Name", "Id", "Git State", "Resource State", "Operating State"]
+    headers.extend([item["header"] for item in extras])
     rows = []
     if format == "table" or format == "csv":
         for item in resp:
@@ -107,10 +107,15 @@ def generic_list(callback, filter, format="table", extras=None):
                 item["name"],
                 item["_id"],
                 item["state"],
-                item["operatingState"],
                 item["resourceState"],
+                item["operatingState"],
             ]
-            for v in extras.values():
-                row_item.append(item.get(v, "-"))
+            for extra_item in extras:
+                item1 = item.get(extra_item["item"], "-")
+                if item1 == "true":
+                    item1 = "YES"
+                elif item1 == "false":
+                    item1 = "NO"
+                row_item.append(item1)
             rows.append(row_item)
     print_output.print_output(format, headers, rows, resp)
index 21bfeba..2517456 100644 (file)
@@ -92,3 +92,50 @@ class Cluster(GenericOSMAPIObject):
         self._logger.debug("")
         endpoint = f"{self._apiBase}/register"
         self.create(name, cluster, None, endpoint=endpoint)
+
+    def list_both(self, filter=None):
+        """List all clusters from both new and old APIs"""
+        self._logger.debug("")
+        cluster_list1 = self.list(filter)
+        cluster_list2 = self._client.k8scluster.list(filter)
+        list1_names = {item["name"] for item in cluster_list1}
+        for item in cluster_list2:
+            if item["name"] not in list1_names:
+                # Complete the information for clusters from old API
+                item["state"] = "N/A"
+                old_state = item.get("_admin", {}).get("operationalState", "Unknown")
+                item["bootstrap"] = "NO"
+                item["operatingState"] = "N/A"
+                item["resourceState"] = old_state
+                item["created"] = "NO"
+                cluster_list1.append(item)
+        # Complete cluster info with vim_account name and vim_type
+        vim_list = self._client.vim.list()
+        for item in cluster_list1:
+            vim_id = item["vim_account"]
+            vim_name, vim_type = next(
+                (
+                    (vim_item["name"], vim_item["vim_type"])
+                    for vim_item in vim_list
+                    if vim_item["_id"] == vim_id or vim_item["name"]
+                ),
+                None,
+            )
+            item["vim_account"] = f"{vim_name} ({vim_type})"
+        return cluster_list1
+
+    def get_both(self, name):
+        """
+        Gets and shows an individual cluster
+        from both new and old API
+        """
+        self._logger.debug("")
+        try:
+            cluster = self.get(name)
+            return cluster
+        except NotFound:
+            try:
+                cluster2 = self._client.k8scluster.get(name)
+                return cluster2
+            except NotFound:
+                raise NotFound(f"{self._logObjectName} {name} not found")