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