From 8a23d2d3c49c868be8083180c984d7baeab4509f Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 13 Sep 2007 11:49:29 +0000
Subject: [PATCH] Move string formatting out of LUQueryInstances

Currently, LUQueryInstances will provide strings for its results. This makes it
hard for other consumers than "gnt-instance list" to use the OpQueryInstances
opcode for whatever they wish to.

The change moves the formatting in five of the six cases where this happens to
the actual gnt-instance.ListInstances procedure and removes the unconditional
convert to string in LUQueryInstances.Exec()

Reviewed-by: imsnah
---
 lib/cmdlib.py        | 19 ++++++-------------
 scripts/gnt-instance | 26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 13 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 8381922e1..aae5e6369 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -2138,25 +2138,19 @@ class LUQueryInstances(NoHooksLU):
         elif field == "pnode":
           val = instance.primary_node
         elif field == "snodes":
-          val = ",".join(instance.secondary_nodes) or "-"
+          val = list(instance.secondary_nodes)
         elif field == "admin_state":
-          if instance.status == "down":
-            val = "no"
-          else:
-            val = "yes"
+          val = (instance.status != "down")
         elif field == "oper_state":
           if instance.primary_node in bad_nodes:
-            val = "(node down)"
+            val = None
           else:
-            if live_data.get(instance.name):
-              val = "running"
-            else:
-              val = "stopped"
+            val = bool(live_data.get(instance.name))
         elif field == "admin_ram":
           val = instance.memory
         elif field == "oper_ram":
           if instance.primary_node in bad_nodes:
-            val = "(node down)"
+            val = None
           elif instance.name in live_data:
             val = live_data[instance.name].get("memory", "?")
           else:
@@ -2172,12 +2166,11 @@ class LUQueryInstances(NoHooksLU):
         elif field == "sda_size" or field == "sdb_size":
           disk = instance.FindDisk(field[:3])
           if disk is None:
-            val = "N/A"
+            val = None
           else:
             val = disk.size
         else:
           raise errors.ParameterError(field)
-        val = str(val)
         iout.append(val)
       output.append(iout)
 
diff --git a/scripts/gnt-instance b/scripts/gnt-instance
index f10ae647f..bfbf59f7b 100755
--- a/scripts/gnt-instance
+++ b/scripts/gnt-instance
@@ -62,6 +62,32 @@ def ListInstances(opts, args):
 
   numfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size"]
 
+  # change raw values to nicer strings
+  for row in output:
+    for idx, field in enumerate(selected_fields):
+      val = row[idx]
+      if field == "snodes":
+        val = ",".join(val) or "-"
+      elif field == "admin_state":
+        if val:
+          val = "yes"
+        else:
+          val = "no"
+      elif field == "oper_state":
+        if val is None:
+          val = "(node down)"
+        elif val: # True
+          val = "running"
+        else:
+          val = "stopped"
+      elif field == "oper_ram":
+        if val is None:
+          val = "(node down)"
+      elif field == "sda_size" or field == "sdb_size":
+        if val is None:
+          val = "N/A"
+      row[idx] = str(val)
+
   data = GenerateTable(separator=opts.separator, headers=headers,
                        fields=selected_fields, unitfields=unitfields,
                        numfields=numfields, data=output)
-- 
GitLab