Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Types.hs @ 4fb78549

History | View | Annotate | Download (2.9 kB)

1
{-| Implementation of the Ganeti Query2 basic types.
2

    
3
These are types internal to the library, and for example clients that
4
use the library should not need to import it.
5

    
6
 -}
7

    
8
{-
9

    
10
Copyright (C) 2012, 2013 Google Inc.
11

    
12
This program is free software; you can redistribute it and/or modify
13
it under the terms of the GNU General Public License as published by
14
the Free Software Foundation; either version 2 of the License, or
15
(at your option) any later version.
16

    
17
This program is distributed in the hope that it will be useful, but
18
WITHOUT ANY WARRANTY; without even the implied warranty of
19
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
General Public License for more details.
21

    
22
You should have received a copy of the GNU General Public License
23
along with this program; if not, write to the Free Software
24
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
02110-1301, USA.
26

    
27
-}
28

    
29
module Ganeti.Query.Types
30
  ( FieldGetter(..)
31
  , QffMode(..)
32
  , FieldData
33
  , FieldList
34
  , FieldMap
35
  , isRuntimeField
36
  , fieldListToFieldMap
37
  ) where
38

    
39
import qualified Data.Map as Map
40

    
41
import Ganeti.Query.Language
42
import Ganeti.Objects
43

    
44
-- | The type of field getters. The \"a\" type represents the type
45
-- we're querying, whereas the \"b\" type represents the \'runtime\'
46
-- data for that type (if any). Note that we don't support multiple
47
-- runtime sources, and we always consider the entire configuration as
48
-- a given (so no equivalent for Python's /*_CONFIG/ and /*_GROUP/;
49
-- configuration accesses are cheap for us).
50
data FieldGetter a b = FieldSimple        (a -> ResultEntry)
51
                     | FieldRuntime       (b -> a -> ResultEntry)
52
                     | FieldConfig        (ConfigData -> a -> ResultEntry)
53
                     | FieldConfigRuntime (ConfigData -> b -> a -> ResultEntry)
54
                     | FieldUnknown
55

    
56
-- | Type defining how the value of a field is used in filtering. This
57
-- implements the equivalent to Python's QFF_ flags, except that we
58
-- don't use OR-able values.
59
data QffMode = QffNormal     -- ^ Value is used as-is in filters
60
             | QffTimestamp  -- ^ Value is a timestamp tuple, convert to float
61
             | QffHostname   -- ^ Value is a hostname, compare it smartly
62
               deriving (Show, Eq)
63

    
64

    
65
-- | Alias for a field data (definition and getter).
66
type FieldData a b = (FieldDefinition, FieldGetter a b, QffMode)
67

    
68
-- | Alias for a field data list.
69
type FieldList a b = [FieldData a b]
70

    
71
-- | Alias for field maps.
72
type FieldMap a b = Map.Map String (FieldData a b)
73

    
74
-- | Helper function to check if a getter is a runtime one.
75
isRuntimeField :: FieldGetter a b -> Bool
76
isRuntimeField FieldRuntime {}       = True
77
isRuntimeField FieldConfigRuntime {} = True
78
isRuntimeField _                     = False
79

    
80
-- | Helper function to obtain a FieldMap from the corresponding FieldList.
81
fieldListToFieldMap :: FieldList a b -> FieldMap a b
82
fieldListToFieldMap = Map.fromList . map (\v@(f, _, _) -> (fdefName f, v))