Revision 046fe3f5 htools/Ganeti/Query/Query.hs

b/htools/Ganeti/Query/Query.hs
27 27
    ( query
28 28
    ) where
29 29

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

  
30 33
import Ganeti.BasicTypes
34
import Ganeti.HTools.JSON
31 35
import Ganeti.Qlang
36
import Ganeti.Query.Common
37
import Ganeti.Query.Types
38
import Ganeti.Query.Node
32 39
import Ganeti.Objects
33 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

  
34 67
-- | Main query execution function.
35 68
query :: ConfigData   -- ^ The current configuration
36 69
      -> Query        -- ^ The query (item, fields, filter)
37 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

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

Also available in: Unified diff