Revision 2e5eb96a htools/Ganeti/HTools/Types.hs

b/htools/Ganeti/HTools/Types.hs
4 4

  
5 5
{-
6 6

  
7
Copyright (C) 2009, 2010 Google Inc.
7
Copyright (C) 2009, 2010, 2011 Google Inc.
8 8

  
9 9
This program is free software; you can redistribute it and/or modify
10 10
it under the terms of the GNU General Public License as published by
......
64 64
import qualified Data.Map as M
65 65
import qualified Text.JSON as JSON
66 66

  
67
import qualified Ganeti.Constants as C
68

  
67 69
-- | The instance index type.
68 70
type Idx = Int
69 71

  
......
103 105
-- | Convert a string to an alloc policy
104 106
apolFromString :: (Monad m) => String -> m AllocPolicy
105 107
apolFromString s =
106
    case s of
107
      "preferred"   -> return AllocPreferred
108
      "last_resort" -> return AllocLastResort
109
      "unallocable" -> return AllocUnallocable
110
      o -> fail $ "Invalid alloc policy mode: " ++ o
108
    case () of
109
      _ | s == C.allocPolicyPreferred -> return AllocPreferred
110
        | s == C.allocPolicyLastResort -> return AllocLastResort
111
        | s == C.allocPolicyUnallocable -> return AllocUnallocable
112
        | otherwise -> fail $ "Invalid alloc policy mode: " ++ s
111 113

  
112 114
-- | Convert an alloc policy to the Ganeti string equivalent
113 115
apolToString :: AllocPolicy -> String
114
apolToString AllocPreferred   = "preferred"
115
apolToString AllocLastResort  = "last_resort"
116
apolToString AllocUnallocable = "unallocable"
116
apolToString AllocPreferred   = C.allocPolicyPreferred
117
apolToString AllocLastResort  = C.allocPolicyLastResort
118
apolToString AllocUnallocable = C.allocPolicyUnallocable
117 119

  
118 120
instance JSON.JSON AllocPolicy where
119 121
    showJSON = JSON.showJSON . apolToString

Also available in: Unified diff