From 84a5b33c98c563f35733ae520e546216a5e4adbf Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Mon, 6 Sep 2010 23:00:34 +0200
Subject: [PATCH] cli: Add option definition for priority

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/cli.py                  | 18 ++++++++++++++++++
 test/ganeti.cli_unittest.py |  8 ++++++++
 2 files changed, 26 insertions(+)

diff --git a/lib/cli.py b/lib/cli.py
index 2d49ec4ff..ad8e9a7fc 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -122,6 +122,7 @@ __all__ = [
   "OS_OPT",
   "OS_SIZE_OPT",
   "PRIMARY_IP_VERSION_OPT",
+  "PRIORITY_OPT",
   "RAPI_CERT_OPT",
   "READD_OPT",
   "REBOOT_TYPE_OPT",
@@ -203,6 +204,18 @@ __all__ = [
 NO_PREFIX = "no_"
 UN_PREFIX = "-"
 
+#: Priorities (sorted)
+_PRIORITY_NAMES = [
+  ("low", constants.OP_PRIO_LOW),
+  ("normal", constants.OP_PRIO_NORMAL),
+  ("high", constants.OP_PRIO_HIGH),
+  ]
+
+#: Priority dictionary for easier lookup
+# TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once
+# we migrate to Python 2.6
+_PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
+
 
 class _Argument:
   def __init__(self, min=0, max=None): # pylint: disable-msg=W0622
@@ -1037,6 +1050,11 @@ PRIMARY_IP_VERSION_OPT = \
                                   constants.IP6_VERSION),
                help="Cluster-wide IP version for primary IP")
 
+PRIORITY_OPT = cli_option("--priority", default=None, dest="priority",
+                          metavar="|".join(name for name, _ in _PRIORITY_NAMES),
+                          choices=_PRIONAME_TO_VALUE.keys(),
+                          help="Priority for opcode(s) processing")
+
 #: Options provided by all commands
 COMMON_OPTS = [DEBUG_OPT]
 
diff --git a/test/ganeti.cli_unittest.py b/test/ganeti.cli_unittest.py
index 77ad4c16e..e2f103253 100755
--- a/test/ganeti.cli_unittest.py
+++ b/test/ganeti.cli_unittest.py
@@ -442,5 +442,13 @@ class TestParseFields(unittest.TestCase):
                      ["def", "ault", "name", "foo"])
 
 
+class TestConstants(unittest.TestCase):
+  def testPriority(self):
+    self.assertEqual(set(cli._PRIONAME_TO_VALUE.values()),
+                     set(constants.OP_PRIO_SUBMIT_VALID))
+    self.assertEqual(list(value for _, value in cli._PRIORITY_NAMES),
+                     sorted(constants.OP_PRIO_SUBMIT_VALID, reverse=True))
+
+
 if __name__ == '__main__':
   testutils.GanetiTestProgram()
-- 
GitLab