diff --git a/lib/cli.py b/lib/cli.py index 20190cc816e4bb027229479c563a8efd6710b341..b33c98c8028f88d0f688d95ddb2518a5e372b2a3 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -195,6 +195,7 @@ __all__ = [ "cli_option", "SplitNodeOption", "CalculateOSNames", + "ParseFields", ] NO_PREFIX = "no_" @@ -1200,6 +1201,24 @@ def CalculateOSNames(os_name, os_variants): return [os_name] +def ParseFields(selected, default): + """Parses the values of "--field"-like options. + + @type selected: string or None + @param selected: User-selected options + @type default: list + @param default: Default fields + + """ + if selected is None: + return default + + if selected.startswith("+"): + return default + selected[1:].split(",") + + return selected.split(",") + + UsesRPC = rpc.RunWithRPC diff --git a/scripts/gnt-instance b/scripts/gnt-instance index e64ef9349f64282e8852e6775ec94b2060c07061..6d9e0f9cb16b3c26e29234d95cea60b3d681bf97 100755 --- a/scripts/gnt-instance +++ b/scripts/gnt-instance @@ -244,12 +244,7 @@ def ListInstances(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = _LIST_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) output = GetClient().QueryInstances(args, selected_fields, opts.do_locking) diff --git a/scripts/gnt-job b/scripts/gnt-job index 3088a1427b45d90398071dc4c357e11ce084cef2..df2ced97dbe554698a616e77b1ab723238a36a5d 100755 --- a/scripts/gnt-job +++ b/scripts/gnt-job @@ -61,12 +61,7 @@ def ListJobs(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = _LIST_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) output = GetClient().QueryJobs(args, selected_fields) if not opts.no_headers: diff --git a/scripts/gnt-node b/scripts/gnt-node index 1cb7f7d980e454fa3ddede7a390b2f5048458050..c17e2c64cff63bffcd12905e9c30804761d9c41f 100755 --- a/scripts/gnt-node +++ b/scripts/gnt-node @@ -46,6 +46,10 @@ _LIST_DEF_FIELDS = [ ] +#: Default field list for L{ListVolumes} +_LIST_VOL_DEF_FIELDS = ["node", "phys", "vg", "name", "size", "instance"] + + #: default list of field for L{ListStorage} _LIST_STOR_DEF_FIELDS = [ constants.SF_NODE, @@ -188,12 +192,7 @@ def ListNodes(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = _LIST_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS) output = GetClient().QueryNodes(args, selected_fields, opts.do_locking) @@ -466,11 +465,7 @@ def ListVolumes(opts, args): @return: the desired exit code """ - if opts.output is None: - selected_fields = ["node", "phys", "vg", - "name", "size", "instance"] - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_VOL_DEF_FIELDS) op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields) output = SubmitOpCode(op, opts=opts) @@ -514,12 +509,7 @@ def ListStorage(opts, args): storage_type = ConvertStorageType(opts.user_storage_type) - if opts.output is None: - selected_fields = _LIST_STOR_DEF_FIELDS - elif opts.output.startswith("+"): - selected_fields = _LIST_STOR_DEF_FIELDS + opts.output[1:].split(",") - else: - selected_fields = opts.output.split(",") + selected_fields = ParseFields(opts.output, _LIST_STOR_DEF_FIELDS) op = opcodes.OpQueryNodeStorage(nodes=args, storage_type=storage_type, diff --git a/test/ganeti.cli_unittest.py b/test/ganeti.cli_unittest.py index a1ae1471a69e4f9467894aac60cbae0e433a8547..77ad4c16e5322d03d7f2ee031004d63aaa19b730 100755 --- a/test/ganeti.cli_unittest.py +++ b/test/ganeti.cli_unittest.py @@ -429,5 +429,18 @@ class TestFormatLogMessage(unittest.TestCase): self.assert_(cli.FormatLogMessage("some other type", (1, 2, 3))) +class TestParseFields(unittest.TestCase): + def test(self): + self.assertEqual(cli.ParseFields(None, []), []) + self.assertEqual(cli.ParseFields("name,foo,hello", []), + ["name", "foo", "hello"]) + self.assertEqual(cli.ParseFields(None, ["def", "ault", "fields", "here"]), + ["def", "ault", "fields", "here"]) + self.assertEqual(cli.ParseFields("name,foo", ["def", "ault"]), + ["name", "foo"]) + self.assertEqual(cli.ParseFields("+name,foo", ["def", "ault"]), + ["def", "ault", "name", "foo"]) + + if __name__ == '__main__': testutils.GanetiTestProgram()