From a604f165385c632dac15ae119cccdb6f264b3824 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 14 Oct 2008 11:29:20 +0000
Subject: [PATCH] Add cli function for beparams validation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is used to validate the parameters in both β€œgnt-instance add” and
β€œgnt-backup import”.

Reviewed-by: ultrotter
---
 lib/cli.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/lib/cli.py b/lib/cli.py
index e3abe3a56..73be93073 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -49,6 +49,7 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain",
            "ListTags", "AddTags", "RemoveTags", "TAG_SRC_OPT",
            "FormatError", "SplitNodeOption", "SubmitOrSend",
            "JobSubmittedException", "FormatTimestamp", "ParseTimespec",
+           "ValidateBeParams",
            ]
 
 
@@ -401,6 +402,27 @@ def SplitNodeOption(value):
     return (value, None)
 
 
+def ValidateBeParams(bep):
+  """Parse and check the given beparams.
+
+  The function will update in-place the given dictionary.
+
+  @type bep: dict
+  @param bep: input beparams
+  @raise errors.ParameterError: if the input values are not OK
+  @raise errors.UnitParseError: if the input values are not OK
+
+  """
+  if constants.BE_MEMORY in bep:
+    bep[constants.BE_MEMORY] = utils.ParseUnit(bep[constants.BE_MEMORY])
+
+  if constants.BE_VCPUS in bep:
+    try:
+      bep[constants.BE_VCPUS] = int(bep[constants.BE_VCPUS])
+    except ValueError:
+      raise errors.ParameterError("Invalid number of VCPUs")
+
+
 def AskUser(text, choices=None):
   """Ask the user a question.
 
-- 
GitLab