root / src / Ganeti / Query / Export.hs @ c92b4671
History | View | Annotate | Download (2.6 kB)
1 |
{-| Implementation of the Ganeti Query2 export queries. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 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.Export |
27 |
( Runtime |
28 |
, fieldsMap |
29 |
, collectLiveData |
30 |
) where |
31 |
|
32 |
import Control.Monad (liftM) |
33 |
|
34 |
import Ganeti.Objects |
35 |
import Ganeti.Rpc |
36 |
import Ganeti.Query.Language |
37 |
import Ganeti.Query.Common |
38 |
import Ganeti.Query.Types |
39 |
|
40 |
-- | The parsed result of the ExportList. This is a bit tricky, in |
41 |
-- that we already do parsing of the results in the RPC calls, so the |
42 |
-- runtime type is a plain 'ResultEntry', as we have just one type. |
43 |
type Runtime = ResultEntry |
44 |
|
45 |
-- | Small helper for rpc to rs. |
46 |
rpcErrToRs :: RpcError -> ResultEntry |
47 |
rpcErrToRs err = ResultEntry (rpcErrorToStatus err) Nothing |
48 |
|
49 |
-- | Helper for extracting fields from RPC result. |
50 |
rpcExtractor :: Node -> Either RpcError RpcResultExportList |
51 |
-> [(Node, ResultEntry)] |
52 |
rpcExtractor node (Right res) = |
53 |
[(node, rsNormal path) | path <- rpcResExportListExports res] |
54 |
rpcExtractor node (Left err) = [(node, rpcErrToRs err)] |
55 |
|
56 |
-- | List of all node fields. |
57 |
exportFields :: FieldList Node Runtime |
58 |
exportFields = |
59 |
[ (FieldDefinition "node" "Node" QFTText "Node name", |
60 |
FieldRuntime (\_ n -> rsNormal $ nodeName n), QffHostname) |
61 |
, (FieldDefinition "export" "Export" QFTText "Export name", |
62 |
FieldRuntime (curry fst), QffNormal) |
63 |
] |
64 |
|
65 |
-- | The node fields map. |
66 |
fieldsMap :: FieldMap Node Runtime |
67 |
fieldsMap = fieldListToFieldMap exportFields |
68 |
|
69 |
-- | Collect live data from RPC query if enabled. |
70 |
-- |
71 |
-- Note that this function is \"funny\": the returned rows will not be |
72 |
-- 1:1 with the input, as nodes without exports will be pruned, |
73 |
-- whereas nodes with multiple exports will be listed multiple times. |
74 |
collectLiveData:: Bool -> ConfigData -> [Node] -> IO [(Node, Runtime)] |
75 |
collectLiveData False _ nodes = |
76 |
return [(n, rpcErrToRs $ RpcResultError "Live data disabled") | n <- nodes] |
77 |
collectLiveData True _ nodes = |
78 |
concatMap (uncurry rpcExtractor) `liftM` |
79 |
executeRpcCall nodes RpcCallExportList |