From acd19189b30440eabc8b981d696b56b2641a611e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Nussbaumer?= <rn@google.com>
Date: Wed, 12 Jan 2011 13:57:58 +0100
Subject: [PATCH] Move FormatParameterDict from gnt_instance.py to cli.py
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This function is useful for other cli-tools who need to display
a dict based parameter field.

Signed-off-by: RenΓ© Nussbaumer <rn@google.com>
Reviewed-by: Adeodato Simo <dato@google.com>
---
 lib/cli.py                 | 19 +++++++++++++++++++
 lib/client/gnt_instance.py | 21 ++++-----------------
 2 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/lib/cli.py b/lib/cli.py
index e2e1e313f..0eb51d0d0 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -178,6 +178,7 @@ __all__ = [
   "ToStderr", "ToStdout",
   "FormatError",
   "FormatQueryResult",
+  "FormatParameterDict",
   "GenerateTable",
   "AskUser",
   "FormatTimestamp",
@@ -2941,3 +2942,21 @@ class JobExecutor(object):
         else:
           ToStderr("Failure for %s: %s", name, result)
       return [row[1:3] for row in self.jobs]
+
+
+def FormatParameterDict(buf, param_dict, actual, level=1):
+  """Formats a parameter dictionary.
+
+  @type buf: L{StringIO}
+  @param buf: the buffer into which to write
+  @type param_dict: dict
+  @param param_dict: the own parameters
+  @type actual: dict
+  @param actual: the current parameter set (including defaults)
+  @param level: Level of indent
+
+  """
+  indent = "  " * level
+  for key in sorted(actual):
+    val = param_dict.get(key, "default (%s)" % actual[key])
+    buf.write("%s- %s: %s\n" % (indent, key, val))
diff --git a/lib/client/gnt_instance.py b/lib/client/gnt_instance.py
index d26efe776..2f258a704 100644
--- a/lib/client/gnt_instance.py
+++ b/lib/client/gnt_instance.py
@@ -1120,21 +1120,6 @@ def _FormatList(buf, data, indent_level):
       _FormatList(buf, elem, indent_level+1)
 
 
-def _FormatParameterDict(buf, per_inst, actual):
-  """Formats a parameter dictionary.
-
-  @type buf: L{StringIO}
-  @param buf: the buffer into which to write
-  @type per_inst: dict
-  @param per_inst: the instance's own parameters
-  @type actual: dict
-  @param actual: the current parameter set (including defaults)
-
-  """
-  for key in sorted(actual):
-    val = per_inst.get(key, "default (%s)" % actual[key])
-    buf.write("    - %s: %s\n" % (key, val))
-
 def ShowInstanceConfig(opts, args):
   """Compute instance run-time status.
 
@@ -1183,7 +1168,8 @@ def ShowInstanceConfig(opts, args):
     buf.write("    - primary: %s\n" % instance["pnode"])
     buf.write("    - secondaries: %s\n" % utils.CommaJoin(instance["snodes"]))
     buf.write("  Operating system: %s\n" % instance["os"])
-    _FormatParameterDict(buf, instance["os_instance"], instance["os_actual"])
+    FormatParameterDict(buf, instance["os_instance"], instance["os_actual"],
+                        level=2)
     if instance.has_key("network_port"):
       buf.write("  Allocated network port: %s\n" %
                 compat.TryToRoman(instance["network_port"],
@@ -1210,7 +1196,8 @@ def ShowInstanceConfig(opts, args):
                                       vnc_bind_address)
       buf.write("    - console connection: vnc to %s\n" % vnc_console_port)
 
-    _FormatParameterDict(buf, instance["hv_instance"], instance["hv_actual"])
+    FormatParameterDict(buf, instance["hv_instance"], instance["hv_actual"],
+                        level=2)
     buf.write("  Hardware:\n")
     buf.write("    - VCPUs: %s\n" %
               compat.TryToRoman(instance["be_actual"][constants.BE_VCPUS],
-- 
GitLab