Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Query / Query.hs @ 046fe3f5

History | View | Annotate | Download (2.7 kB)

1
{-| Implementation of the Ganeti Query2 functionality.
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.Query
27
    ( query
28
    ) where
29

    
30
import Data.Maybe (fromMaybe)
31
import qualified Data.Map as Map
32

    
33
import Ganeti.BasicTypes
34
import Ganeti.HTools.JSON
35
import Ganeti.Qlang
36
import Ganeti.Query.Common
37
import Ganeti.Query.Types
38
import Ganeti.Query.Node
39
import Ganeti.Objects
40

    
41
-- * Helper functions
42

    
43
-- | Builds an unknown field definition.
44
mkUnknownFDef :: String -> FieldData a b
45
mkUnknownFDef name =
46
  ( FieldDefinition name name QFTUnknown ("Unknown field '" ++ name ++ "'")
47
  , FieldUnknown )
48

    
49
-- | Runs a field getter on the existing contexts.
50
execGetter :: ConfigData -> b -> a -> FieldGetter a b -> ResultEntry
51
execGetter _   _ item (FieldSimple getter)  = getter item
52
execGetter cfg _ item (FieldConfig getter)  = getter cfg item
53
execGetter _  rt item (FieldRuntime getter) = getter rt item
54
execGetter _   _ _    FieldUnknown          = rsUnknown
55

    
56
-- * Main query execution
57

    
58
-- | Helper to build the list of requested fields. This transforms the
59
-- list of string fields to a list of field defs and getters, with
60
-- some of them possibly being unknown fields.
61
getSelectedFields :: FieldMap a b  -- ^ Defined fields
62
                  -> [String]      -- ^ Requested fields
63
                  -> FieldList a b -- ^ Selected fields
64
getSelectedFields defined =
65
  map (\name -> fromMaybe (mkUnknownFDef name) $ name `Map.lookup` defined)
66

    
67
-- | Main query execution function.
68
query :: ConfigData   -- ^ The current configuration
69
      -> Query        -- ^ The query (item, fields, filter)
70
      -> IO (Result QueryResult) -- ^ Result
71

    
72
query cfg (Query QRNode fields _) = return $ do
73
  let selected = getSelectedFields nodeFieldsMap fields
74
      (fdefs, fgetters) = unzip selected
75
      nodes = Map.elems . fromContainer $ configNodes cfg
76
      fdata = map (\node -> map (execGetter cfg NodeRuntime node) fgetters)
77
              nodes
78
  return QueryResult { qresFields = fdefs, qresData = fdata }
79

    
80
query _ (Query qkind _ _) =
81
  return . Bad $ "Query '" ++ show qkind ++ "' not supported"