Improve TemplateHaskell code to support empty objects
[ganeti-local] / src / Ganeti / Types.hs
index e6c9378..1753ce6 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,10 +86,13 @@ module Ganeti.Types
   , JobDependency(..)
   , OpSubmitPriority(..)
   , opSubmitPriorityToRaw
+  , parseSubmitPriority
+  , fmtSubmitPriority
   , OpStatus(..)
   , opStatusToRaw
   , opStatusFromRaw
   , ELogType(..)
+  , InstReasonSrc(..)
   ) where
 
 import Control.Monad (liftM)
@@ -357,7 +360,7 @@ $(THH.declareSADT "IAllocatorMode"
   ])
 $(THH.makeJSONInstance ''IAllocatorMode)
 
--- | Netork mode.
+-- | Network mode.
 $(THH.declareSADT "NICMode"
   [ ("NMBridged", 'C.nicModeBridged)
   , ("NMRouted",  'C.nicModeRouted)
@@ -447,6 +450,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)
@@ -466,3 +482,12 @@ $(THH.declareSADT "ELogType"
   , ("ELogJqueueTest",   'C.elogJqueueTest)
   ])
 $(THH.makeJSONInstance ''ELogType)
+
+-- | Type for the source of the state change of instances.
+$(THH.declareSADT "InstReasonSrc"
+  [ ("IRSCli", 'C.instanceReasonSourceCli)
+  , ("IRSRapi",  'C.instanceReasonSourceRapi)
+  ])
+$(THH.makeJSONInstance ''InstReasonSrc)
+
+