From 845c79d8915138ae2d72ecaa3730ee410264282d Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Wed, 29 Sep 2010 17:30:05 +0200 Subject: [PATCH] cli: Move parsing of --net option to separate function This function will also be used in tools/move-instance. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/cli.py | 36 +++++++++++++++++++++++++----------- test/ganeti.cli_unittest.py | 21 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/lib/cli.py b/lib/cli.py index 2d4286339..a92f7931c 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -1801,6 +1801,30 @@ def GenericMain(commands, override=None, aliases=None): return result +def ParseNicOption(optvalue): + """Parses the value of the --net option(s). + + """ + try: + nic_max = max(int(nidx[0]) + 1 for nidx in optvalue) + except (TypeError, ValueError), err: + raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err)) + + nics = [{}] * nic_max + for nidx, ndict in optvalue: + nidx = int(nidx) + + if not isinstance(ndict, dict): + raise errors.OpPrereqError("Invalid nic/%d value: expected dict," + " got %s" % (nidx, ndict)) + + utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES) + + nics[nidx] = ndict + + return nics + + def GenericInstanceCreate(mode, opts, args): """Add an instance to the cluster via either creation or import. @@ -1822,17 +1846,7 @@ def GenericInstanceCreate(mode, opts, args): hypervisor, hvparams = opts.hypervisor if opts.nics: - try: - nic_max = max(int(nidx[0]) + 1 for nidx in opts.nics) - except ValueError, err: - raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err)) - nics = [{}] * nic_max - for nidx, ndict in opts.nics: - nidx = int(nidx) - if not isinstance(ndict, dict): - msg = "Invalid nic/%d value: expected dict, got %s" % (nidx, ndict) - raise errors.OpPrereqError(msg) - nics[nidx] = ndict + nics = ParseNicOption(opts.nics) elif opts.no_nics: # no nics nics = [] diff --git a/test/ganeti.cli_unittest.py b/test/ganeti.cli_unittest.py index 77ad4c16e..0f352ac22 100755 --- a/test/ganeti.cli_unittest.py +++ b/test/ganeti.cli_unittest.py @@ -442,5 +442,26 @@ class TestParseFields(unittest.TestCase): ["def", "ault", "name", "foo"]) +class TestParseNicOption(unittest.TestCase): + def test(self): + self.assertEqual(cli.ParseNicOption([("0", { "link": "eth0", })]), + [{ "link": "eth0", }]) + self.assertEqual(cli.ParseNicOption([("5", { "ip": "192.0.2.7", })]), + [{}, {}, {}, {}, {}, { "ip": "192.0.2.7", }]) + + def testErrors(self): + for i in [None, "", "abc", "zero", "Hello World", "\0", []]: + self.assertRaises(errors.OpPrereqError, cli.ParseNicOption, + [(i, { "link": "eth0", })]) + self.assertRaises(errors.OpPrereqError, cli.ParseNicOption, + [("0", i)]) + + self.assertRaises(errors.TypeEnforcementError, cli.ParseNicOption, + [(0, { True: False, })]) + + self.assertRaises(errors.TypeEnforcementError, cli.ParseNicOption, + [(3, { "mode": [], })]) + + if __name__ == '__main__': testutils.GanetiTestProgram() -- GitLab