Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Export.hs @ 030ab01a

History | View | Annotate | Download (2.7 kB)

1 c4bf507b Iustin Pop
{-| Implementation of the Ganeti Query2 export queries.
2 c4bf507b Iustin Pop
3 c4bf507b Iustin Pop
 -}
4 c4bf507b Iustin Pop
5 c4bf507b Iustin Pop
{-
6 c4bf507b Iustin Pop
7 c4bf507b Iustin Pop
Copyright (C) 2013 Google Inc.
8 c4bf507b Iustin Pop
9 c4bf507b Iustin Pop
This program is free software; you can redistribute it and/or modify
10 c4bf507b Iustin Pop
it under the terms of the GNU General Public License as published by
11 c4bf507b Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 c4bf507b Iustin Pop
(at your option) any later version.
13 c4bf507b Iustin Pop
14 c4bf507b Iustin Pop
This program is distributed in the hope that it will be useful, but
15 c4bf507b Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 c4bf507b Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 c4bf507b Iustin Pop
General Public License for more details.
18 c4bf507b Iustin Pop
19 c4bf507b Iustin Pop
You should have received a copy of the GNU General Public License
20 c4bf507b Iustin Pop
along with this program; if not, write to the Free Software
21 c4bf507b Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 c4bf507b Iustin Pop
02110-1301, USA.
23 c4bf507b Iustin Pop
24 c4bf507b Iustin Pop
-}
25 c4bf507b Iustin Pop
26 c4bf507b Iustin Pop
module Ganeti.Query.Export
27 c4bf507b Iustin Pop
  ( Runtime
28 c4bf507b Iustin Pop
  , fieldsMap
29 c4bf507b Iustin Pop
  , collectLiveData
30 c4bf507b Iustin Pop
  ) where
31 c4bf507b Iustin Pop
32 c4bf507b Iustin Pop
import Control.Monad (liftM)
33 c4bf507b Iustin Pop
import qualified Data.Map as Map
34 c4bf507b Iustin Pop
35 c4bf507b Iustin Pop
import Ganeti.Objects
36 c4bf507b Iustin Pop
import Ganeti.Rpc
37 c4bf507b Iustin Pop
import Ganeti.Query.Language
38 c4bf507b Iustin Pop
import Ganeti.Query.Common
39 c4bf507b Iustin Pop
import Ganeti.Query.Types
40 c4bf507b Iustin Pop
41 c4bf507b Iustin Pop
-- | The parsed result of the ExportList. This is a bit tricky, in
42 c4bf507b Iustin Pop
-- that we already do parsing of the results in the RPC calls, so the
43 c4bf507b Iustin Pop
-- runtime type is a plain 'ResultEntry', as we have just one type.
44 c4bf507b Iustin Pop
type Runtime = ResultEntry
45 c4bf507b Iustin Pop
46 c4bf507b Iustin Pop
-- | Small helper for rpc to rs.
47 c4bf507b Iustin Pop
rpcErrToRs :: RpcError -> ResultEntry
48 c4bf507b Iustin Pop
rpcErrToRs err = ResultEntry (rpcErrorToStatus err) Nothing
49 c4bf507b Iustin Pop
50 c4bf507b Iustin Pop
-- | Helper for extracting fields from RPC result.
51 c4bf507b Iustin Pop
rpcExtractor :: Node -> Either RpcError RpcResultExportList
52 c4bf507b Iustin Pop
             -> [(Node, ResultEntry)]
53 c4bf507b Iustin Pop
rpcExtractor node (Right res) =
54 c4bf507b Iustin Pop
  [(node, rsNormal path) | path <- rpcResExportListExports res]
55 c4bf507b Iustin Pop
rpcExtractor node (Left err)  = [(node, rpcErrToRs err)]
56 c4bf507b Iustin Pop
57 c4bf507b Iustin Pop
-- | List of all node fields.
58 c4bf507b Iustin Pop
exportFields :: FieldList Node Runtime
59 c4bf507b Iustin Pop
exportFields =
60 c4bf507b Iustin Pop
  [ (FieldDefinition "node" "Node" QFTText "Node name",
61 7c17db26 Iustin Pop
     FieldRuntime (\_ n -> rsNormal $ nodeName n), QffHostname)
62 c4bf507b Iustin Pop
  , (FieldDefinition "export" "Export" QFTText "Export name",
63 c4bf507b Iustin Pop
     FieldRuntime (curry fst), QffNormal)
64 c4bf507b Iustin Pop
  ]
65 c4bf507b Iustin Pop
66 c4bf507b Iustin Pop
-- | The node fields map.
67 c4bf507b Iustin Pop
fieldsMap :: FieldMap Node Runtime
68 c4bf507b Iustin Pop
fieldsMap =
69 c4bf507b Iustin Pop
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) exportFields
70 c4bf507b Iustin Pop
71 c4bf507b Iustin Pop
-- | Collect live data from RPC query if enabled.
72 c4bf507b Iustin Pop
--
73 c4bf507b Iustin Pop
-- Note that this function is \"funny\": the returned rows will not be
74 c4bf507b Iustin Pop
-- 1:1 with the input, as nodes without exports will be pruned,
75 c4bf507b Iustin Pop
-- whereas nodes with multiple exports will be listed multiple times.
76 c4bf507b Iustin Pop
collectLiveData:: Bool -> ConfigData -> [Node] -> IO [(Node, Runtime)]
77 c4bf507b Iustin Pop
collectLiveData False _ nodes =
78 c4bf507b Iustin Pop
  return [(n, rpcErrToRs $ RpcResultError "Live data disabled") | n <- nodes]
79 c4bf507b Iustin Pop
collectLiveData True _ nodes =
80 c4bf507b Iustin Pop
  concatMap (uncurry rpcExtractor) `liftM`
81 c4bf507b Iustin Pop
    executeRpcCall nodes RpcCallExportList