root / htools / Ganeti / Query / Common.hs @ 4393e075
History | View | Annotate | Download (5.4 kB)
1 |
{-| Implementation of the Ganeti Query2 common objects. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2012 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 |
( rsNoData |
28 |
, rsNormal |
29 |
, rsMaybe |
30 |
, rsUnknown |
31 |
, missingRuntime |
32 |
, rpcErrorToStatus |
33 |
, timeStampFields |
34 |
, uuidFields |
35 |
, serialFields |
36 |
, tagsFields |
37 |
, dictFieldGetter |
38 |
, buildQFTLookup |
39 |
, buildNdParamField |
40 |
) where |
41 |
|
42 |
import qualified Data.Map as Map |
43 |
import Data.Maybe (fromMaybe) |
44 |
import Text.JSON (JSON, showJSON) |
45 |
|
46 |
import qualified Ganeti.Constants as C |
47 |
import Ganeti.Config |
48 |
import Ganeti.Objects |
49 |
import Ganeti.Rpc |
50 |
import Ganeti.Query.Language |
51 |
import Ganeti.Query.Types |
52 |
|
53 |
-- * Generic functions |
54 |
|
55 |
-- | Conversion from 'VType' to 'FieldType'. |
56 |
vTypeToQFT :: VType -> FieldType |
57 |
vTypeToQFT VTypeString = QFTOther |
58 |
vTypeToQFT VTypeMaybeString = QFTOther |
59 |
vTypeToQFT VTypeBool = QFTBool |
60 |
vTypeToQFT VTypeSize = QFTUnit |
61 |
vTypeToQFT VTypeInt = QFTNumber |
62 |
|
63 |
-- * Result helpers |
64 |
|
65 |
-- | Helper for a result with no data. |
66 |
rsNoData :: ResultEntry |
67 |
rsNoData = ResultEntry RSNoData Nothing |
68 |
|
69 |
-- | Helper to declare a normal result. |
70 |
rsNormal :: (JSON a) => a -> ResultEntry |
71 |
rsNormal a = ResultEntry RSNormal $ Just (showJSON a) |
72 |
|
73 |
-- | Helper to declare a result from a 'Maybe' (the item might be |
74 |
-- missing, in which case we return no data). Note that there's some |
75 |
-- ambiguity here: in some cases, we mean 'RSNoData', but in other |
76 |
-- 'RSUnavail'; this is easy to solve in simple cases, but not in |
77 |
-- nested dicts. |
78 |
rsMaybe :: (JSON a) => Maybe a -> ResultEntry |
79 |
rsMaybe = maybe rsNoData rsNormal |
80 |
|
81 |
-- | Helper for unknown field result. |
82 |
rsUnknown :: ResultEntry |
83 |
rsUnknown = ResultEntry RSUnknown Nothing |
84 |
|
85 |
-- | Helper for a missing runtime parameter. |
86 |
missingRuntime :: FieldGetter a b |
87 |
missingRuntime = FieldRuntime (\_ _ -> ResultEntry RSNoData Nothing) |
88 |
|
89 |
-- * Error conversion |
90 |
|
91 |
-- | Convert RpcError to ResultStatus |
92 |
rpcErrorToStatus :: RpcError -> ResultStatus |
93 |
rpcErrorToStatus (OfflineNodeError _) = RSOffline |
94 |
rpcErrorToStatus _ = RSNoData |
95 |
|
96 |
-- * Common fields |
97 |
|
98 |
-- | The list of timestamp fields. |
99 |
timeStampFields :: (TimeStampObject a) => FieldList a b |
100 |
timeStampFields = |
101 |
[ (FieldDefinition "ctime" "CTime" QFTTimestamp "Creation timestamp", |
102 |
FieldSimple (rsNormal . cTimeOf)) |
103 |
, (FieldDefinition "mtime" "MTime" QFTTimestamp "Modification timestamp", |
104 |
FieldSimple (rsNormal . mTimeOf)) |
105 |
] |
106 |
|
107 |
-- | The list of UUID fields. |
108 |
uuidFields :: (UuidObject a) => String -> FieldList a b |
109 |
uuidFields name = |
110 |
[ (FieldDefinition "uuid" "UUID" QFTText (name ++ " UUID"), |
111 |
FieldSimple (rsNormal . uuidOf)) ] |
112 |
|
113 |
-- | The list of serial number fields. |
114 |
serialFields :: (SerialNoObject a) => String -> FieldList a b |
115 |
serialFields name = |
116 |
[ (FieldDefinition "serial_no" "SerialNo" QFTNumber |
117 |
(name ++ " object serial number, incremented on each modification"), |
118 |
FieldSimple (rsNormal . serialOf)) ] |
119 |
|
120 |
-- | The list of tag fields. |
121 |
tagsFields :: (TagsObject a) => FieldList a b |
122 |
tagsFields = |
123 |
[ (FieldDefinition "tags" "Tags" QFTOther "Tags", |
124 |
FieldSimple (rsNormal . tagsOf)) ] |
125 |
|
126 |
-- * Generic parameter functions |
127 |
|
128 |
-- | Returns a field from a (possibly missing) 'DictObject'. This is |
129 |
-- used by parameter dictionaries, usually. Note that we have two |
130 |
-- levels of maybe: the top level dict might be missing, or one key in |
131 |
-- the dictionary might be. |
132 |
dictFieldGetter :: (DictObject a) => String -> Maybe a -> ResultEntry |
133 |
dictFieldGetter k = maybe rsNoData (rsMaybe . lookup k . toDict) |
134 |
|
135 |
-- | Build an optimised lookup map from a Python _PARAMETER_TYPES |
136 |
-- association list. |
137 |
buildQFTLookup :: [(String, String)] -> Map.Map String FieldType |
138 |
buildQFTLookup = |
139 |
Map.fromList . |
140 |
map (\(k, v) -> (k, maybe QFTOther vTypeToQFT (vTypeFromRaw v))) |
141 |
|
142 |
-- | Ndparams optimised lookup map. |
143 |
ndParamTypes :: Map.Map String FieldType |
144 |
ndParamTypes = buildQFTLookup C.ndsParameterTypes |
145 |
|
146 |
-- | Ndparams title map. |
147 |
ndParamTitles :: Map.Map String FieldTitle |
148 |
ndParamTitles = Map.fromList C.ndsParameterTitles |
149 |
|
150 |
-- | Ndparam getter builder: given a field, it returns a FieldConfig |
151 |
-- getter, that is a function that takes the config and the object and |
152 |
-- returns the Ndparam field specified when the getter was built. |
153 |
ndParamGetter :: (NdParamObject a) => |
154 |
String -- ^ The field we're building the getter for |
155 |
-> ConfigData -> a -> ResultEntry |
156 |
ndParamGetter field config = |
157 |
dictFieldGetter field . getNdParamsOf config |
158 |
|
159 |
-- | Builds the ndparam fields for an object. |
160 |
buildNdParamField :: (NdParamObject a) => String -> FieldData a b |
161 |
buildNdParamField field = |
162 |
let full_name = "ndp/" ++ field |
163 |
title = fromMaybe field $ field `Map.lookup` ndParamTitles |
164 |
qft = fromMaybe QFTOther $ field `Map.lookup` ndParamTypes |
165 |
desc = "The \"" ++ field ++ "\" node parameter" |
166 |
in (FieldDefinition full_name title qft desc, |
167 |
FieldConfig (ndParamGetter field)) |