Move some more type definitions to Types.hs
[ganeti-local] / Ganeti / HTools / Types.hs
1 {-| Some common types.
2
3 -}
4
5 {-
6
7 Copyright (C) 2009 Google Inc.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 02110-1301, USA.
23
24 -}
25
26 module Ganeti.HTools.Types
27     ( Idx
28     , Ndx
29     , NameAssoc
30     , Score
31     , Placement
32     , IMove(..)
33     , MoveJob
34     , JobSet
35     , Result(..)
36     , Element(..)
37     , FailMode(..)
38     , FailStats
39     , OpResult(..)
40     , connTimeout
41     , queryTimeout
42     ) where
43
44 -- | The instance index type.
45 type Idx = Int
46
47 -- | The node index type.
48 type Ndx = Int
49
50 -- | The type used to hold name-to-idx mappings.
51 type NameAssoc = [(String, Int)]
52
53 -- | A separate name for the cluster score type.
54 type Score = Double
55
56 -- | The description of an instance placement.
57 type Placement = ( Idx   -- ^ The index of the instance being moved
58                  , Ndx   -- ^ New primary node
59                  , Ndx   -- ^ New secondary node
60                  , IMove -- ^ The move being performed
61                  , Score -- ^ The score of the cluster after this move
62                  )
63
64 -- | An instance move definition
65 data IMove = Failover                -- ^ Failover the instance (f)
66            | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
67            | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
68            | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
69            | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
70              deriving (Show)
71
72 -- | Formatted solution output for one move (involved nodes and
73 -- commands
74 type MoveJob = ([Ndx], [String])
75
76 -- | A list of command elements
77 type JobSet = [MoveJob]
78
79 -- | Connection timeout (when using non-file methods).
80 connTimeout :: Int
81 connTimeout = 15
82
83 -- | The default timeout for queries (when using non-file methods).
84 queryTimeout :: Int
85 queryTimeout = 60
86
87 {-|
88
89 This is similar to the JSON library Result type - *very* similar, but
90 we want to use it in multiple places, so we abstract it into a
91 mini-library here
92
93 -}
94 data Result a
95     = Bad String
96     | Ok a
97     deriving (Show)
98
99 instance Monad Result where
100     (>>=) (Bad x) _ = Bad x
101     (>>=) (Ok x) fn = fn x
102     return = Ok
103     fail = Bad
104
105 -- | Reason for an operation's falure
106 data FailMode = FailMem  -- ^ Failed due to not enough RAM
107               | FailDisk -- ^ Failed due to not enough disk
108               | FailCPU  -- ^ Failed due to not enough CPU capacity
109               | FailN1   -- ^ Failed due to not passing N1 checks
110                 deriving (Eq, Enum, Bounded, Show)
111
112 -- | List with failure statistics
113 type FailStats = [(FailMode, Int)]
114
115 -- | Either-like data-type customized for our failure modes
116 data OpResult a = OpFail FailMode -- ^ Failed operation
117                 | OpGood a        -- ^ Success operation
118
119 instance Monad OpResult where
120     (OpGood x) >>= fn = fn x
121     (OpFail y) >>= _ = OpFail y
122     return = OpGood
123
124 -- | A generic class for items that have updateable names and indices.
125 class Element a where
126     -- | Returns the name of the element
127     nameOf  :: a -> String
128     -- | Returns the index of the element
129     idxOf   :: a -> Int
130     -- | Updates the name of the element
131     setName :: a -> String -> a
132     -- | Updates the index of the element
133     setIdx  :: a -> Int -> a