Revision e9aaa3c6 htools/Ganeti/HTools/Types.hs

b/htools/Ganeti/HTools/Types.hs
1
{-# LANGUAGE TemplateHaskell #-}
2

  
1 3
{-| Some common types.
2 4

  
3 5
-}
......
72 74
import qualified Text.JSON as JSON
73 75

  
74 76
import qualified Ganeti.Constants as C
77
import qualified Ganeti.THH as THH
75 78

  
76 79
-- | The instance index type.
77 80
type Idx = Int
......
100 103
-- Ord instance will order them in the order they are defined, so when
101 104
-- changing this data type be careful about the interaction with the
102 105
-- desired sorting order.
103
data AllocPolicy
104
    = AllocPreferred   -- ^ This is the normal status, the group
105
                       -- should be used normally during allocations
106
    | AllocLastResort  -- ^ This group should be used only as
107
                       -- last-resort, after the preferred groups
108
    | AllocUnallocable -- ^ This group must not be used for new
109
                       -- allocations
110
      deriving (Show, Read, Eq, Ord, Enum, Bounded)
111

  
112
-- | Convert a string to an alloc policy.
113
allocPolicyFromString :: (Monad m) => String -> m AllocPolicy
114
allocPolicyFromString s =
115
    case () of
116
      _ | s == C.allocPolicyPreferred -> return AllocPreferred
117
        | s == C.allocPolicyLastResort -> return AllocLastResort
118
        | s == C.allocPolicyUnallocable -> return AllocUnallocable
119
        | otherwise -> fail $ "Invalid alloc policy mode: " ++ s
120

  
121
-- | Convert an alloc policy to the Ganeti string equivalent.
122
allocPolicyToString :: AllocPolicy -> String
123
allocPolicyToString AllocPreferred   = C.allocPolicyPreferred
124
allocPolicyToString AllocLastResort  = C.allocPolicyLastResort
125
allocPolicyToString AllocUnallocable = C.allocPolicyUnallocable
126

  
127
instance JSON.JSON AllocPolicy where
128
    showJSON = JSON.showJSON . allocPolicyToString
129
    readJSON s = case JSON.readJSON s of
130
                   JSON.Ok s' -> allocPolicyFromString s'
131
                   JSON.Error e -> JSON.Error $
132
                                   "Can't parse alloc_policy: " ++ e
106
$(THH.declareSADT "AllocPolicy"
107
         [ ("AllocPreferred",   'C.allocPolicyPreferred)
108
         , ("AllocLastResort",  'C.allocPolicyLastResort)
109
         , ("AllocUnallocable", 'C.allocPolicyUnallocable)
110
         ])
111
$(THH.makeJSONInstance ''AllocPolicy)
133 112

  
134 113
-- | The resource spec type.
135 114
data RSpec = RSpec
......
182 161
             deriving (Show, Read)
183 162

  
184 163
-- | Instance disk template type.
185
data DiskTemplate = DTDiskless
186
                  | DTFile
187
                  | DTSharedFile
188
                  | DTPlain
189
                  | DTBlock
190
                  | DTDrbd8
191
                    deriving (Show, Read, Eq, Enum, Bounded)
192

  
193
-- | Converts a DiskTemplate to String.
194
diskTemplateToString :: DiskTemplate -> String
195
diskTemplateToString DTDiskless   = C.dtDiskless
196
diskTemplateToString DTFile       = C.dtFile
197
diskTemplateToString DTSharedFile = C.dtSharedFile
198
diskTemplateToString DTPlain      = C.dtPlain
199
diskTemplateToString DTBlock      = C.dtBlock
200
diskTemplateToString DTDrbd8      = C.dtDrbd8
201

  
202
-- | Converts a DiskTemplate from String.
203
diskTemplateFromString :: (Monad m) => String -> m DiskTemplate
204
diskTemplateFromString s =
205
    case () of
206
      _ | s == C.dtDiskless   -> return DTDiskless
207
        | s == C.dtFile       -> return DTFile
208
        | s == C.dtSharedFile -> return DTSharedFile
209
        | s == C.dtPlain      -> return DTPlain
210
        | s == C.dtBlock      -> return DTBlock
211
        | s == C.dtDrbd8      -> return DTDrbd8
212
        | otherwise           -> fail $ "Invalid disk template: " ++ s
213

  
214
instance JSON.JSON DiskTemplate where
215
    showJSON = JSON.showJSON . diskTemplateToString
216
    readJSON s = case JSON.readJSON s of
217
                   JSON.Ok s' -> diskTemplateFromString s'
218
                   JSON.Error e -> JSON.Error $
219
                                   "Can't parse disk_template as string: " ++ e
164
$(THH.declareSADT "DiskTemplate"
165
     [ ("DTDiskless",   'C.dtDiskless)
166
     , ("DTFile",       'C.dtFile)
167
     , ("DTSharedFile", 'C.dtSharedFile)
168
     , ("DTPlain",      'C.dtPlain)
169
     , ("DTBlock",      'C.dtBlock)
170
     , ("DTDrbd8",      'C.dtDrbd8)
171
     ])
172
$(THH.makeJSONInstance ''DiskTemplate)
220 173

  
221 174
-- | Formatted solution output for one move (involved nodes and
222 175
-- commands.
......
347 300
    setIdx  :: a -> Int -> a
348 301

  
349 302
-- | The iallocator node-evacuate evac_mode type.
350
data EvacMode = ChangePrimary
351
              | ChangeSecondary
352
              | ChangeAll
353
                deriving (Show, Read)
354

  
355
instance JSON.JSON EvacMode where
356
    showJSON mode = case mode of
357
                      ChangeAll       -> JSON.showJSON C.iallocatorNevacAll
358
                      ChangePrimary   -> JSON.showJSON C.iallocatorNevacPri
359
                      ChangeSecondary -> JSON.showJSON C.iallocatorNevacSec
360
    readJSON v =
361
        case JSON.readJSON v of
362
          JSON.Ok s | s == C.iallocatorNevacAll -> return ChangeAll
363
                    | s == C.iallocatorNevacPri -> return ChangePrimary
364
                    | s == C.iallocatorNevacSec -> return ChangeSecondary
365
                    | otherwise -> fail $ "Invalid evacuate mode " ++ s
366
          JSON.Error e -> JSON.Error $
367
                          "Can't parse evacuate mode as string: " ++ e
303
$(THH.declareSADT "EvacMode"
304
     [ ("ChangePrimary",   'C.iallocatorNevacPri)
305
     , ("ChangeSecondary", 'C.iallocatorNevacSec)
306
     , ("ChangeAll",       'C.iallocatorNevacAll)
307
     ])
308
$(THH.makeJSONInstance ''EvacMode)

Also available in: Unified diff