From 42ce2e138dc2ef7d9356b0965fdec43f19c47732 Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Tue, 2 Dec 2008 10:53:14 +0000 Subject: [PATCH] Add utils.CheckBEParams This function will be used in LUCreateInstance, LUSetInstanceParams, LUSetClusterParams and InitCluster to check the backend parameters validity and convert the relevant values to integer, without duplicating code. It lives in utils as bootstrap.py is calling it too. Reviewed-by: imsnah --- lib/utils.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/utils.py b/lib/utils.py index 92ef0aa96..17b5d3c65 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -520,6 +520,36 @@ def BridgeExists(bridge): return os.path.isdir("/sys/class/net/%s/bridge" % bridge) +def CheckBEParams(beparams): + """Checks whether the user-supplied be-params are valid, + and converts them from string format where appropriate. + + @type beparams: dict + @param beparams: new params dict + + """ + if beparams: + for item in beparams: + if item not in constants.BES_PARAMETERS: + raise errors.OpPrereqError("Unknown backend parameter %s" % item) + if item in (constants.BE_MEMORY, constants.BE_VCPUS): + val = beparams[item] + if val != constants.VALUE_DEFAULT: + try: + val = int(val) + except ValueError, err: + raise errors.OpPrereqError("Invalid %s size: %s" % (item, str(err))) + beparams[item] = val + if item in (constants.BE_AUTO_BALANCE): + val = beparams[item] + if val == constants.VALUE_TRUE: + beparams[item] = True + elif val == constants.VALUE_FALSE: + beparams[item] = False + else: + raise errors.OpPrereqError("Invalid %s value: %s" % (item, val)) + + def NiceSort(name_list): """Sort a list of strings based on digit and non-digit groupings. -- GitLab