Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Common.hs @ c2fe6008

History | View | Annotate | Download (6.5 kB)

1
{-| Implementation of the Ganeti Query2 common objects.
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.Common
27
  ( NoDataRuntime(..)
28
  , rsNoData
29
  , rsUnavail
30
  , rsNormal
31
  , rsMaybeNoData
32
  , rsMaybeUnavail
33
  , rsUnknown
34
  , missingRuntime
35
  , rpcErrorToStatus
36
  , timeStampFields
37
  , uuidFields
38
  , serialFields
39
  , tagsFields
40
  , dictFieldGetter
41
  , buildNdParamField
42
  , getDefaultHypervisorSpec
43
  , getHvParamsFromCluster
44
  ) where
45

    
46
import qualified Data.Map as Map
47
import Data.Maybe (fromMaybe)
48
import Text.JSON (JSON, showJSON)
49

    
50
import qualified Ganeti.Constants as C
51
import Ganeti.Config
52
import Ganeti.JSON
53
import Ganeti.Objects
54
import Ganeti.Rpc
55
import Ganeti.Query.Language
56
import Ganeti.Query.Types
57
import Ganeti.Types
58

    
59
-- | The runtime used by queries which retrieve no live data.
60
data NoDataRuntime = NoDataRuntime
61

    
62
-- * Generic functions
63

    
64
-- | Conversion from 'VType' to 'FieldType'.
65
vTypeToQFT :: VType -> FieldType
66
vTypeToQFT VTypeString      = QFTOther
67
vTypeToQFT VTypeMaybeString = QFTOther
68
vTypeToQFT VTypeBool        = QFTBool
69
vTypeToQFT VTypeSize        = QFTUnit
70
vTypeToQFT VTypeInt         = QFTNumber
71

    
72
-- * Result helpers
73

    
74
-- | Helper for a result with no data.
75
rsNoData :: ResultEntry
76
rsNoData = ResultEntry RSNoData Nothing
77

    
78
-- | Helper for result for an entity which supports no such field.
79
rsUnavail :: ResultEntry
80
rsUnavail = ResultEntry RSUnavail Nothing
81

    
82
-- | Helper to declare a normal result.
83
rsNormal :: (JSON a) => a -> ResultEntry
84
rsNormal a = ResultEntry RSNormal $ Just (showJSON a)
85

    
86
-- | Helper to declare a result from a 'Maybe' (the item might be
87
-- missing, in which case we return no data). Note that there's some
88
-- ambiguity here: in some cases, we mean 'RSNoData', but in other
89
-- 'RSUnavail'; this is easy to solve in simple cases, but not in
90
-- nested dicts. If you want to return 'RSUnavail' in case of 'Nothing'
91
-- use the function 'rsMaybeUnavail'.
92
rsMaybeNoData :: (JSON a) => Maybe a -> ResultEntry
93
rsMaybeNoData = maybe rsNoData rsNormal
94

    
95
-- | Helper to declare a result from a 'Maybe'. This version returns
96
-- a 'RSUnavail' in case of 'Nothing'. It should be used for optional
97
-- fields that are not set. For cases where 'Nothing' means that there
98
-- was an error, consider using 'rsMaybe' instead.
99
rsMaybeUnavail :: (JSON a) => Maybe a -> ResultEntry
100
rsMaybeUnavail = maybe rsUnavail rsNormal
101

    
102
-- | Helper for unknown field result.
103
rsUnknown :: ResultEntry
104
rsUnknown = ResultEntry RSUnknown Nothing
105

    
106
-- | Helper for a missing runtime parameter.
107
missingRuntime :: FieldGetter a b
108
missingRuntime = FieldRuntime (\_ _ -> ResultEntry RSNoData Nothing)
109

    
110
-- * Error conversion
111

    
112
-- | Convert RpcError to ResultStatus
113
rpcErrorToStatus :: RpcError -> ResultStatus
114
rpcErrorToStatus OfflineNodeError = RSOffline
115
rpcErrorToStatus _ = RSNoData
116

    
117
-- * Common fields
118

    
119
-- | The list of timestamp fields.
120
timeStampFields :: (TimeStampObject a) => FieldList a b
121
timeStampFields =
122
  [ (FieldDefinition "ctime" "CTime" QFTTimestamp "Creation timestamp",
123
     FieldSimple (rsNormal . cTimeOf), QffNormal)
124
  , (FieldDefinition "mtime" "MTime" QFTTimestamp "Modification timestamp",
125
     FieldSimple (rsNormal . mTimeOf), QffNormal)
126
  ]
127

    
128
-- | The list of UUID fields.
129
uuidFields :: (UuidObject a) => String -> FieldList a b
130
uuidFields name =
131
  [ (FieldDefinition "uuid" "UUID" QFTText  (name ++ " UUID"),
132
     FieldSimple (rsNormal . uuidOf), QffNormal) ]
133

    
134
-- | The list of serial number fields.
135
serialFields :: (SerialNoObject a) => String -> FieldList a b
136
serialFields name =
137
  [ (FieldDefinition "serial_no" "SerialNo" QFTNumber
138
     (name ++ " object serial number, incremented on each modification"),
139
     FieldSimple (rsNormal . serialOf), QffNormal) ]
140

    
141
-- | The list of tag fields.
142
tagsFields :: (TagsObject a) => FieldList a b
143
tagsFields =
144
  [ (FieldDefinition "tags" "Tags" QFTOther "Tags",
145
     FieldSimple (rsNormal . tagsOf), QffNormal) ]
146

    
147
-- * Generic parameter functions
148

    
149
-- | Returns a field from a (possibly missing) 'DictObject'. This is
150
-- used by parameter dictionaries, usually. Note that we have two
151
-- levels of maybe: the top level dict might be missing, or one key in
152
-- the dictionary might be.
153
dictFieldGetter :: (DictObject a) => String -> Maybe a -> ResultEntry
154
dictFieldGetter k = maybe rsNoData (rsMaybeNoData . lookup k . toDict)
155

    
156
-- | Ndparams optimised lookup map.
157
ndParamTypes :: Map.Map String FieldType
158
ndParamTypes = Map.map vTypeToQFT C.ndsParameterTypes
159

    
160
-- | Ndparams title map.
161
ndParamTitles :: Map.Map String FieldTitle
162
ndParamTitles = C.ndsParameterTitles
163

    
164
-- | Ndparam getter builder: given a field, it returns a FieldConfig
165
-- getter, that is a function that takes the config and the object and
166
-- returns the Ndparam field specified when the getter was built.
167
ndParamGetter :: (NdParamObject a) =>
168
                 String -- ^ The field we're building the getter for
169
              -> ConfigData -> a -> ResultEntry
170
ndParamGetter field config =
171
  dictFieldGetter field . getNdParamsOf config
172

    
173
-- | Builds the ndparam fields for an object.
174
buildNdParamField :: (NdParamObject a) => String -> FieldData a b
175
buildNdParamField field =
176
  let full_name = "ndp/" ++ field
177
      title = fromMaybe field $ field `Map.lookup` ndParamTitles
178
      qft = fromMaybe QFTOther $ field `Map.lookup` ndParamTypes
179
      desc = "The \"" ++ field ++ "\" node parameter"
180
  in (FieldDefinition full_name title qft desc,
181
      FieldConfig (ndParamGetter field), QffNormal)
182

    
183
-- | Looks up the default hypervisor and its hvparams
184
getDefaultHypervisorSpec :: ConfigData -> (Hypervisor, HvParams)
185
getDefaultHypervisorSpec cfg = (hv, getHvParamsFromCluster cfg hv)
186
  where hv = getDefaultHypervisor cfg
187

    
188
-- | Looks up the cluster's hvparams of the given hypervisor
189
getHvParamsFromCluster :: ConfigData -> Hypervisor -> HvParams
190
getHvParamsFromCluster cfg hv =
191
  fromMaybe (GenericContainer Map.empty) .
192
    Map.lookup (hypervisorToRaw hv) .
193
      fromContainer . clusterHvparams $ configCluster cfg