Skip to content
Snippets Groups Projects
Commit b2ba4669 authored by Iustin Pop's avatar Iustin Pop
Browse files

Implement a JSON instance for AllocPolicy


This will allow reading this attribute via the Rapi/Luxi backends.

Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarBalazs Lecz <leczb@google.com>
parent 1a3cc8ad
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,8 @@ module Ganeti.HTools.Types
, Weight
, GroupID
, AllocPolicy(..)
, apolFromString
, apolToString
, RSpec(..)
, DynUtil(..)
, zeroUtil
......@@ -60,6 +62,7 @@ module Ganeti.HTools.Types
) where
import qualified Data.Map as M
import qualified Text.JSON as JSON
-- | The instance index type.
type Idx = Int
......@@ -88,6 +91,28 @@ data AllocPolicy = AllocPreferred
| AllocUnallocable
deriving (Show, Eq)
-- | Convert a string to an alloc policy
apolFromString :: (Monad m) => String -> m AllocPolicy
apolFromString s =
case s of
"preferred" -> return AllocPreferred
"last_resort" -> return AllocLastResort
"unallocable" -> return AllocUnallocable
o -> fail $ "Invalid alloc policy mode: " ++ o
-- | Convert an alloc policy to the Ganeti string equivalent
apolToString :: AllocPolicy -> String
apolToString AllocPreferred = "preferred"
apolToString AllocLastResort = "last_resort"
apolToString AllocUnallocable = "unallocable"
instance JSON.JSON AllocPolicy where
showJSON = JSON.showJSON . apolToString
readJSON s = case JSON.readJSON s of
JSON.Ok s' -> apolFromString s'
JSON.Error e -> JSON.Error $
"Can't parse alloc_policy: " ++ e
-- | The resource spec type.
data RSpec = RSpec
{ rspecCpu :: Int -- ^ Requested VCPUs
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment