Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Query2.hs @ fce98abd

History | View | Annotate | Download (4.1 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Implementation of the Ganeti Query2 language.
4

    
5
 -}
6

    
7
{-
8

    
9
Copyright (C) 2012 Google Inc.
10

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

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

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

    
26
-}
27

    
28
module Ganeti.Query2
29
    ( Filter(..)
30
    , Query(..)
31
    , QueryResult(..)
32
    , QueryFields(..)
33
    , QueryFieldsResult(..)
34
    , FieldDefinition(..)
35
    , ResultEntry(..)
36
    ) where
37

    
38

    
39
import Text.JSON.Types
40
import Text.JSON
41

    
42
import qualified Ganeti.Constants as C
43
import Ganeti.THH
44

    
45
-- * THH declarations, that require ordering.
46

    
47
-- | Status of a query field.
48
$(declareIADT "ResultStatus"
49
  [ ("RSNormal",  'C.rsNormal )
50
  , ("RSUnknown", 'C.rsUnknown )
51
  , ("RSNoData",  'C.rsNodata )
52
  , ("RSUnavail", 'C.rsUnavail )
53
  , ("RSOffline", 'C.rsOffline )
54
  ])
55
$(makeJSONInstance ''ResultStatus)
56

    
57
-- | Type of a query field.
58
$(declareSADT "FieldType"
59
  [ ("QFTUnknown",   'C.qftUnknown )
60
  , ("QFTText",      'C.qftText )
61
  , ("QFTBool",      'C.qftBool )
62
  , ("QFTNumber",    'C.qftNumber )
63
  , ("QFTUnit",      'C.qftUnit )
64
  , ("QFTTimestamp", 'C.qftTimestamp )
65
  , ("QFTOther",     'C.qftOther )
66
  ])
67
$(makeJSONInstance ''FieldType)
68

    
69
-- | Supported items on which Query2 works.
70
$(declareSADT "ItemType"
71
  [ ("QRCluster",  'C.qrCluster )
72
  , ("QRInstance", 'C.qrInstance )
73
  , ("QRNode",     'C.qrNode )
74
  , ("QRLock",     'C.qrLock )
75
  , ("QRGroup",    'C.qrGroup )
76
  , ("QROs",       'C.qrOs )
77
  , ("QRJob",      'C.qrJob )
78
  , ("QRExport",   'C.qrExport )
79
  ])
80
$(makeJSONInstance ''ItemType)
81

    
82
-- * Main Query2 queries and responses.
83

    
84
-- | Query2 query.
85
data Query = Query ItemType Fields (Maybe Filter)
86

    
87
-- | Query2 result.
88
data QueryResult = QueryResult [ FieldDefinition ] [ ResultEntry ]
89

    
90
-- | Query2 Fields query.
91
-- (to get supported fields names, descriptions, and types)
92
data QueryFields = QueryFields ItemType Fields
93

    
94
-- | Query2 Fields result.
95
data QueryFieldsResult = QueryFieldsResult [ FieldDefinition ]
96

    
97
-- * Sub data types for query2 queries and responses.
98

    
99
-- | List of requested fields.
100
type Fields = [ String ]
101

    
102
-- | Query2 filter expression.
103
data Filter
104
    = AndFilter [ Filter ] -- ^ & [<expression>, ...]
105
    | OrFilter [ Filter ] -- ^ | [<expression>, ...]
106
    | NotFilter Filter -- ^ ! <expression>
107
    | TrueFilter FilterField -- ^ ? <field>
108
    | EqualFilter FilterField FilterValue -- ^ (=|!=) <field> <value>
109
    | LessThanFilter FilterField FilterValue -- ^ < <field> <value>
110
    | GreaterThanFilter FilterField FilterValue -- ^ > <field> <value>
111
    | LEThanFilter FilterField FilterValue -- ^ <= <field> <value>
112
    | GEThanFilter FilterField FilterValue -- ^ >= <field> <value>
113
    | RegexpFilter FilterField FilterRegexp -- ^ =~ <field> <regexp>
114
    | ContainsFilter FilterField FilterValue -- ^ =[] <list-field> <value>
115

    
116
-- | Field name to filter on.
117
type FilterField = String
118

    
119
-- | Value to compare the field value to, for filtering purposes.
120
type FilterValue = String
121

    
122
-- | Regexp to apply to the filter value, for filteriong purposes.
123
type FilterRegexp = String
124

    
125
-- | Definition of a field.
126
data FieldDefinition = FieldDefinition FieldName FieldTitle FieldType FieldDoc
127

    
128
-- | Name of a field.
129
type FieldName = String
130
-- | Title of a field, when represented in tabular format.
131
type FieldTitle = String
132
-- | Human redable description of a field.
133
type FieldDoc = String
134

    
135
--- | Single field entry result.
136
data ResultEntry = ResultEntry ResultStatus (Maybe ResultValue)
137

    
138
-- | Value of a field, in json encoding.
139
-- (its type will be depending on ResultStatus and FieldType)
140
type ResultValue = JSValue