Sort instance list in gnt-group list -opinst_list
[ganeti-local] / src / Ganeti / Query / Group.hs
1 {-| Implementation of the Ganeti Query2 node group queries.
2
3  -}
4
5 {-
6
7 Copyright (C) 2012, 2013 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.Query.Group
27   ( Runtime
28   , fieldsMap
29   , collectLiveData
30   ) where
31
32 import qualified Data.Map as Map
33
34 import Ganeti.Config
35 import Ganeti.Objects
36 import Ganeti.Query.Language
37 import Ganeti.Query.Common
38 import Ganeti.Query.Types
39 import Ganeti.Utils (niceSort)
40
41 -- | There is no runtime.
42 data Runtime = Runtime
43
44 groupFields :: FieldList NodeGroup Runtime
45 groupFields =
46   [ (FieldDefinition "alloc_policy" "AllocPolicy" QFTText
47        "Allocation policy for group",
48      FieldSimple (rsNormal . groupAllocPolicy), QffNormal)
49   , (FieldDefinition "custom_diskparams" "CustomDiskParameters" QFTOther
50        "Custom disk parameters",
51      FieldSimple (rsNormal . groupDiskparams), QffNormal)
52   , (FieldDefinition "custom_ipolicy" "CustomInstancePolicy" QFTOther
53        "Custom instance policy limitations",
54      FieldSimple (rsNormal . groupIpolicy), QffNormal)
55   , (FieldDefinition "custom_ndparams" "CustomNDParams" QFTOther
56        "Custom node parameters",
57      FieldSimple (rsNormal . groupNdparams), QffNormal)
58   , (FieldDefinition "diskparams" "DiskParameters" QFTOther
59        "Disk parameters (merged)",
60      FieldConfig (\cfg -> rsNormal . getGroupDiskParams cfg), QffNormal)
61   , (FieldDefinition "ipolicy" "InstancePolicy" QFTOther
62        "Instance policy limitations (merged)",
63      FieldConfig (\cfg ng -> rsNormal (getGroupIpolicy cfg ng)), QffNormal)
64   , (FieldDefinition "name" "Group" QFTText "Group name",
65      FieldSimple (rsNormal . groupName), QffNormal)
66   , (FieldDefinition "ndparams" "NDParams" QFTOther "Node parameters",
67      FieldConfig (\cfg ng -> rsNormal (getGroupNdParams cfg ng)), QffNormal)
68   , (FieldDefinition "node_cnt" "Nodes" QFTNumber "Number of nodes",
69      FieldConfig (\cfg -> rsNormal . length . getGroupNodes cfg . groupUuid),
70      QffNormal)
71   , (FieldDefinition "node_list" "NodeList" QFTOther "List of nodes",
72      FieldConfig (\cfg -> rsNormal . map nodeName .
73                           getGroupNodes cfg . groupUuid), QffNormal)
74   , (FieldDefinition "pinst_cnt" "Instances" QFTNumber
75        "Number of primary instances",
76      FieldConfig
77        (\cfg -> rsNormal . length . fst . getGroupInstances cfg . groupUuid),
78      QffNormal)
79   , (FieldDefinition "pinst_list" "InstanceList" QFTOther
80        "List of primary instances",
81      -- FIXME: the niceSort here is not tested
82      FieldConfig (\cfg -> rsNormal . niceSort . map instName . fst .
83                           getGroupInstances cfg . groupUuid), QffNormal)
84   ] ++
85   map buildNdParamField allNDParamFields ++
86   timeStampFields ++
87   uuidFields "Group" ++
88   serialFields "Group" ++
89   tagsFields
90
91 -- | The group fields map.
92 fieldsMap :: FieldMap NodeGroup Runtime
93 fieldsMap =
94   Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) groupFields
95
96 -- | Dummy function for collecting live data (which groups don't have).
97 collectLiveData :: Bool -> ConfigData -> [NodeGroup]
98                 -> IO [(NodeGroup, Runtime)]
99 collectLiveData _ _ = return . map (\n -> (n, Runtime))