Commit a4ebd726 authored by Michael Hanselmann's avatar Michael Hanselmann

Use one function to parse “--fields” option values

Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 4fb780d1
...@@ -195,6 +195,7 @@ __all__ = [ ...@@ -195,6 +195,7 @@ __all__ = [
"cli_option", "cli_option",
"SplitNodeOption", "SplitNodeOption",
"CalculateOSNames", "CalculateOSNames",
"ParseFields",
] ]
NO_PREFIX = "no_" NO_PREFIX = "no_"
...@@ -1200,6 +1201,24 @@ def CalculateOSNames(os_name, os_variants): ...@@ -1200,6 +1201,24 @@ def CalculateOSNames(os_name, os_variants):
return [os_name] 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 UsesRPC = rpc.RunWithRPC
......
...@@ -244,12 +244,7 @@ def ListInstances(opts, args): ...@@ -244,12 +244,7 @@ def ListInstances(opts, args):
@return: the desired exit code @return: the desired exit code
""" """
if opts.output is None: selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
selected_fields = _LIST_DEF_FIELDS
elif opts.output.startswith("+"):
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
else:
selected_fields = opts.output.split(",")
output = GetClient().QueryInstances(args, selected_fields, opts.do_locking) output = GetClient().QueryInstances(args, selected_fields, opts.do_locking)
......
...@@ -61,12 +61,7 @@ def ListJobs(opts, args): ...@@ -61,12 +61,7 @@ def ListJobs(opts, args):
@return: the desired exit code @return: the desired exit code
""" """
if opts.output is None: selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
selected_fields = _LIST_DEF_FIELDS
elif opts.output.startswith("+"):
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
else:
selected_fields = opts.output.split(",")
output = GetClient().QueryJobs(args, selected_fields) output = GetClient().QueryJobs(args, selected_fields)
if not opts.no_headers: if not opts.no_headers:
......
...@@ -46,6 +46,10 @@ _LIST_DEF_FIELDS = [ ...@@ -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} #: default list of field for L{ListStorage}
_LIST_STOR_DEF_FIELDS = [ _LIST_STOR_DEF_FIELDS = [
constants.SF_NODE, constants.SF_NODE,
...@@ -188,12 +192,7 @@ def ListNodes(opts, args): ...@@ -188,12 +192,7 @@ def ListNodes(opts, args):
@return: the desired exit code @return: the desired exit code
""" """
if opts.output is None: selected_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
selected_fields = _LIST_DEF_FIELDS
elif opts.output.startswith("+"):
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
else:
selected_fields = opts.output.split(",")
output = GetClient().QueryNodes(args, selected_fields, opts.do_locking) output = GetClient().QueryNodes(args, selected_fields, opts.do_locking)
...@@ -466,11 +465,7 @@ def ListVolumes(opts, args): ...@@ -466,11 +465,7 @@ def ListVolumes(opts, args):
@return: the desired exit code @return: the desired exit code
""" """
if opts.output is None: selected_fields = ParseFields(opts.output, _LIST_VOL_DEF_FIELDS)
selected_fields = ["node", "phys", "vg",
"name", "size", "instance"]
else:
selected_fields = opts.output.split(",")
op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields) op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
output = SubmitOpCode(op, opts=opts) output = SubmitOpCode(op, opts=opts)
...@@ -514,12 +509,7 @@ def ListStorage(opts, args): ...@@ -514,12 +509,7 @@ def ListStorage(opts, args):
storage_type = ConvertStorageType(opts.user_storage_type) storage_type = ConvertStorageType(opts.user_storage_type)
if opts.output is None: selected_fields = ParseFields(opts.output, _LIST_STOR_DEF_FIELDS)
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(",")
op = opcodes.OpQueryNodeStorage(nodes=args, op = opcodes.OpQueryNodeStorage(nodes=args,
storage_type=storage_type, storage_type=storage_type,
......
...@@ -429,5 +429,18 @@ class TestFormatLogMessage(unittest.TestCase): ...@@ -429,5 +429,18 @@ class TestFormatLogMessage(unittest.TestCase):
self.assert_(cli.FormatLogMessage("some other type", (1, 2, 3))) 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__': if __name__ == '__main__':
testutils.GanetiTestProgram() testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment