Add functions to parse CLI-level format of priorities
authorIustin Pop <iustin@google.com>
Fri, 8 Feb 2013 12:14:26 +0000 (13:14 +0100)
committerIustin Pop <iustin@google.com>
Tue, 12 Feb 2013 13:47:53 +0000 (14:47 +0100)
The current serialisation format for submit priorities is
integer-based, same as the opcode json serialisation. But for CLI
level, we need to support a string-based format, so we add functions
to parse and format this representation.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>

src/Ganeti/Types.hs
test/hs/Test/Ganeti/Types.hs

index e6c9378..9f02173 100644 (file)
@@ -11,7 +11,7 @@ representation should go into 'Ganeti.HTools.Types'.
 
 {-
 
-Copyright (C) 2012 Google Inc.
+Copyright (C) 2012, 2013 Google Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -86,6 +86,8 @@ module Ganeti.Types
   , JobDependency(..)
   , OpSubmitPriority(..)
   , opSubmitPriorityToRaw
+  , parseSubmitPriority
+  , fmtSubmitPriority
   , OpStatus(..)
   , opStatusToRaw
   , opStatusFromRaw
@@ -447,6 +449,19 @@ $(THH.declareIADT "OpSubmitPriority"
   ])
 $(THH.makeJSONInstance ''OpSubmitPriority)
 
+-- | Parse submit priorities from a string.
+parseSubmitPriority :: (Monad m) => String -> m OpSubmitPriority
+parseSubmitPriority "low"    = return OpPrioLow
+parseSubmitPriority "normal" = return OpPrioNormal
+parseSubmitPriority "high"   = return OpPrioHigh
+parseSubmitPriority str      = fail $ "Unknown priority '" ++ str ++ "'"
+
+-- | Format a submit priority as string.
+fmtSubmitPriority :: OpSubmitPriority -> String
+fmtSubmitPriority OpPrioLow    = "low"
+fmtSubmitPriority OpPrioNormal = "normal"
+fmtSubmitPriority OpPrioHigh   = "high"
+
 -- | Our ADT for the OpCode status at runtime (while in a job).
 $(THH.declareSADT "OpStatus"
   [ ("OP_STATUS_QUEUED",    'C.opStatusQueued)
index 6f84782..6246e6e 100644 (file)
@@ -7,7 +7,7 @@
 
 {-
 
-Copyright (C) 2012 Google Inc.
+Copyright (C) 2012, 2013 Google Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -370,6 +370,11 @@ prop_JobDependency_serialisation = testSerialisation
 prop_OpSubmitPriority_serialisation :: OpSubmitPriority -> Property
 prop_OpSubmitPriority_serialisation = testSerialisation
 
+-- | Tests string formatting for 'OpSubmitPriority'.
+prop_OpSubmitPriority_string :: OpSubmitPriority -> Property
+prop_OpSubmitPriority_string prio =
+  parseSubmitPriority (fmtSubmitPriority prio) ==? Just prio
+
 -- | Test 'ELogType' serialisation.
 prop_ELogType_serialisation :: ELogType -> Property
 prop_ELogType_serialisation = testSerialisation
@@ -416,5 +421,6 @@ testSuite "Types"
   , 'case_JobId_BadTypes
   , 'prop_JobDependency_serialisation
   , 'prop_OpSubmitPriority_serialisation
+  , 'prop_OpSubmitPriority_string
   , 'prop_ELogType_serialisation
   ]