Add 'Read' instances for most objects
[ganeti-local] / Ganeti / HTools / Group.hs
1 {-| Module describing a node group.
2
3 -}
4
5 {-
6
7 Copyright (C) 2010 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.Group
27     ( Group(..)
28     , List
29     , AssocList
30     -- * Constructor
31     , create
32     , setIdx
33     ) where
34
35 import qualified Ganeti.HTools.Container as Container
36
37 import qualified Ganeti.HTools.Types as T
38
39 -- * Type declarations
40
41 -- | The node group type.
42 data Group = Group
43     { name        :: String        -- ^ The node name
44     , uuid        :: T.GroupID     -- ^ The UUID of the group
45     , idx         :: T.Gdx         -- ^ Internal index for book-keeping
46     , allocPolicy :: T.AllocPolicy -- ^ The allocation policy for this group
47     } deriving (Show, Read, Eq)
48
49 -- Note: we use the name as the alias, and the UUID as the official
50 -- name
51 instance T.Element Group where
52     nameOf     = uuid
53     idxOf      = idx
54     setAlias   = setName
55     setIdx     = setIdx
56     allNames n = [name n, uuid n]
57
58 -- | A simple name for the int, node association list.
59 type AssocList = [(T.Gdx, Group)]
60
61 -- | A simple name for a node map.
62 type List = Container.Container Group
63
64 -- * Initialization functions
65
66 -- | Create a new group.
67 create :: String -> T.GroupID -> T.AllocPolicy -> Group
68 create name_init id_init apol_init =
69     Group { name        = name_init
70           , uuid        = id_init
71           , allocPolicy = apol_init
72           , idx         = -1
73           }
74
75 -- This is used only during the building of the data structures.
76 setIdx :: Group -> T.Gdx -> Group
77 setIdx t i = t {idx = i}
78
79 -- | Changes the alias.
80 --
81 -- This is used only during the building of the data structures.
82 setName :: Group -> String -> Group
83 setName t s = t { name = s }