Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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
  ) where
43

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

    
48
import qualified Ganeti.Constants as C
49
import Ganeti.Config
50
import Ganeti.Objects
51
import Ganeti.Rpc
52
import Ganeti.Query.Language
53
import Ganeti.Query.Types
54
import Ganeti.Types
55

    
56
-- | The runtime used by queries which retrieve no live data.
57
data NoDataRuntime = NoDataRuntime
58

    
59
-- * Generic functions
60

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

    
69
-- * Result helpers
70

    
71
-- | Helper for a result with no data.
72
rsNoData :: ResultEntry
73
rsNoData = ResultEntry RSNoData Nothing
74

    
75
-- | Helper for result for an entity which supports no such field.
76
rsUnavail :: ResultEntry
77
rsUnavail = ResultEntry RSUnavail Nothing
78

    
79
-- | Helper to declare a normal result.
80
rsNormal :: (JSON a) => a -> ResultEntry
81
rsNormal a = ResultEntry RSNormal $ Just (showJSON a)
82

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

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

    
99
-- | Helper for unknown field result.
100
rsUnknown :: ResultEntry
101
rsUnknown = ResultEntry RSUnknown Nothing
102

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

    
107
-- * Error conversion
108

    
109
-- | Convert RpcError to ResultStatus
110
rpcErrorToStatus :: RpcError -> ResultStatus
111
rpcErrorToStatus OfflineNodeError = RSOffline
112
rpcErrorToStatus _ = RSNoData
113

    
114
-- * Common fields
115

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

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

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

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

    
144
-- * Generic parameter functions
145

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

    
153
-- | Ndparams optimised lookup map.
154
ndParamTypes :: Map.Map String FieldType
155
ndParamTypes = Map.map vTypeToQFT C.ndsParameterTypes
156

    
157
-- | Ndparams title map.
158
ndParamTitles :: Map.Map String FieldTitle
159
ndParamTitles = C.ndsParameterTitles
160

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

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