Turn on, and fix, more warnings
[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     , Result(..)
31     , Element(..)
32     , FailMode(..)
33     , FailStats
34     , OpResult(..)
35     , connTimeout
36     , queryTimeout
37     ) where
38
39 -- | The instance index type.
40 type Idx = Int
41
42 -- | The node index type.
43 type Ndx = Int
44
45 -- | The type used to hold name-to-idx mappings.
46 type NameAssoc = [(String, Int)]
47
48 -- | Connection timeout (when using non-file methods).
49 connTimeout :: Int
50 connTimeout = 15
51
52 -- | The default timeout for queries (when using non-file methods).
53 queryTimeout :: Int
54 queryTimeout = 60
55
56 {-|
57
58 This is similar to the JSON library Result type - *very* similar, but
59 we want to use it in multiple places, so we abstract it into a
60 mini-library here
61
62 -}
63 data Result a
64     = Bad String
65     | Ok a
66     deriving (Show)
67
68 instance Monad Result where
69     (>>=) (Bad x) _ = Bad x
70     (>>=) (Ok x) fn = fn x
71     return = Ok
72     fail = Bad
73
74 -- | Reason for an operation's falure
75 data FailMode = FailMem  -- ^ Failed due to not enough RAM
76               | FailDisk -- ^ Failed due to not enough disk
77               | FailCPU  -- ^ Failed due to not enough CPU capacity
78               | FailN1   -- ^ Failed due to not passing N1 checks
79                 deriving (Eq, Enum, Bounded, Show)
80
81 -- | List with failure statistics
82 type FailStats = [(FailMode, Int)]
83
84 -- | Either-like data-type customized for our failure modes
85 data OpResult a = OpFail FailMode -- ^ Failed operation
86                 | OpGood a        -- ^ Success operation
87
88 instance Monad OpResult where
89     (OpGood x) >>= fn = fn x
90     (OpFail y) >>= _ = OpFail y
91     return = OpGood
92
93 -- | A generic class for items that have updateable names and indices.
94 class Element a where
95     -- | Returns the name of the element
96     nameOf  :: a -> String
97     -- | Returns the index of the element
98     idxOf   :: a -> Int
99     -- | Updates the name of the element
100     setName :: a -> String -> a
101     -- | Updates the index of the element
102     setIdx  :: a -> Int -> a