Improve the error message for command line errors
[ganeti-local] / Ganeti / HTools / Instance.hs
1 {-| Module describing an instance.
2
3 The instance data type holds very few fields, the algorithm
4 intelligence is in the "Node" and "Cluster" modules.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009 Google Inc.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 02110-1301, USA.
26
27 -}
28
29 module Ganeti.HTools.Instance where
30
31 import qualified Ganeti.HTools.Types as T
32 import qualified Ganeti.HTools.Container as Container
33
34 -- * Type declarations
35
36 -- | The instance type
37 data Instance = Instance { name :: String   -- ^ The instance name
38                          , mem :: Int       -- ^ Memory of the instance
39                          , dsk :: Int       -- ^ Disk size of instance
40                          , vcpus :: Int     -- ^ Number of VCPUs
41                          , running :: Bool  -- ^ Whether the instance
42                                             -- is running
43                          , run_st :: String -- ^ Original (text) run status
44                          , pnode :: T.Ndx   -- ^ Original primary node
45                          , snode :: T.Ndx   -- ^ Original secondary node
46                          , idx :: T.Idx     -- ^ Internal index for
47                                             -- book-keeping
48                          } deriving (Show)
49
50 instance T.Element Instance where
51     nameOf  = name
52     idxOf   = idx
53     setName = setName
54     setIdx  = setIdx
55
56 -- | A simple name for the int, instance association list.
57 type AssocList = [(T.Idx, Instance)]
58
59 -- | A simple name for an instance map.
60 type List = Container.Container Instance
61
62 -- * Initialization
63
64 -- | Create an instance.
65 --
66 -- Some parameters are not initialized by function, and must be set
67 -- later (via 'setIdx' for example).
68 create :: String -> Int -> Int -> Int -> String -> T.Ndx -> T.Ndx -> Instance
69 create name_init mem_init dsk_init vcpus_init run_init pn sn =
70     Instance {
71           name = name_init,
72           mem = mem_init,
73           dsk = dsk_init,
74           vcpus = vcpus_init,
75           running = case run_init of
76                       "running" -> True
77                       "ERROR_up" -> True
78                       _ -> False,
79           run_st = run_init,
80           pnode = pn,
81           snode = sn,
82           idx = -1
83         }
84
85 -- | Changes the index.
86 --
87 -- This is used only during the building of the data structures.
88 setIdx :: Instance -- ^ The original instance
89        -> T.Idx    -- ^ New index
90        -> Instance -- ^ The modified instance
91 setIdx t i = t { idx = i }
92
93 -- | Changes the name.
94 --
95 -- This is used only during the building of the data structures.
96 setName :: Instance -- ^ The original instance
97         -> String   -- ^ New name
98         -> Instance -- ^ The modified instance
99 setName t s = t { name = s }
100
101 -- * Update functions
102
103 -- | Changes the primary node of the instance.
104 setPri :: Instance  -- ^ the original instance
105         -> T.Ndx    -- ^ the new primary node
106         -> Instance -- ^ the modified instance
107 setPri t p = t { pnode = p }
108
109 -- | Changes the secondary node of the instance.
110 setSec :: Instance  -- ^ the original instance
111         -> T.Ndx    -- ^ the new secondary node
112         -> Instance -- ^ the modified instance
113 setSec t s = t { snode = s }
114
115 -- | Changes both nodes of the instance.
116 setBoth :: Instance  -- ^ the original instance
117          -> T.Ndx    -- ^ new primary node index
118          -> T.Ndx    -- ^ new secondary node index
119          -> Instance -- ^ the modified instance
120 setBoth t p s = t { pnode = p, snode = s }