From d67e0a94f717fbbd98ae635a3ea69ef04aa6097b Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 14 May 2012 16:47:14 +0200
Subject: [PATCH] Allow units in ipolicy disk/mem CLI changes

This makes "gnt-cluster modify --specs-mem-size max=16g" work. The
downside (due to how we iterate) is that error messages are not very
clear:

  $ gnt-cluster modify --specs-mem-size max=16a
  Failure: prerequisites not met for this operation:
  error type: wrong_input, error details:
  Invalid disk ({}) or memory ({'max': '16a'}) size in policy: Unknown unit: a

Also updates the help of these commands, which was very confusing
before (at least for me).

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/cli.py | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/lib/cli.py b/lib/cli.py
index 1a3926aa7..b79422262 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -810,25 +810,31 @@ DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams",
 
 SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size",
                                  type="keyval", default={},
-                                 help="Memory count specs: min, max, std"
-                                 " (in MB)")
+                                 help="Memory size specs: list of key=value,"
+                                " where key is one of min, max, std"
+                                 " (in MB or using a unit)")
 
 SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count",
                                  type="keyval", default={},
-                                 help="CPU count specs: min, max, std")
+                                 help="CPU count specs: list of key=value,"
+                                 " where key is one of min, max, std")
 
 SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count",
                                   dest="ispecs_disk_count",
                                   type="keyval", default={},
-                                  help="Disk count specs: min, max, std")
+                                  help="Disk count specs: list of key=value,"
+                                  " where key is one of min, max, std")
 
 SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size",
                                  type="keyval", default={},
-                                 help="Disk size specs: min, max, std (in MB)")
+                                 help="Disk size specs: list of key=value,"
+                                " where key is one of min, max, std"
+                                 " (in MB or using a unit)")
 
 SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count",
                                  type="keyval", default={},
-                                 help="NIC count specs: min, max, std")
+                                 help="NIC count specs: list of key=value,"
+                                 " where key is one of min, max, std")
 
 IPOLICY_DISK_TEMPLATES = cli_option("--ipolicy-disk-templates",
                                  dest="ipolicy_disk_templates",
@@ -3427,6 +3433,19 @@ def CreateIPolicyFromOpts(ispecs_mem_size=None,
 
 
   """
+  try:
+    if ispecs_mem_size:
+      for k in ispecs_mem_size:
+        ispecs_mem_size[k] = utils.ParseUnit(ispecs_mem_size[k])
+    if ispecs_disk_size:
+      for k in ispecs_disk_size:
+        ispecs_disk_size[k] = utils.ParseUnit(ispecs_disk_size[k])
+  except (TypeError, ValueError, errors.UnitParseError), err:
+    raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
+                               " in policy: %s" %
+                               (ispecs_disk_size, ispecs_mem_size, err),
+                               errors.ECODE_INVAL)
+
   # prepare ipolicy dict
   ipolicy_transposed = {
     constants.ISPEC_MEM_SIZE: ispecs_mem_size,
-- 
GitLab