Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Confd / Types.hs @ 32a569fe

History | View | Annotate | Download (5.3 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Implementation of the Ganeti confd types.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2011, 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.Confd.Types
29
  ( C.confdProtocolVersion
30
  , C.confdMaxClockSkew
31
  , C.confdConfigReloadTimeout
32
  , C.confdConfigReloadRatelimit
33
  , C.confdMagicFourcc
34
  , C.confdDefaultReqCoverage
35
  , C.confdClientExpireTimeout
36
  , C.maxUdpDataSize
37
  , ConfdRequestType(..)
38
  , ConfdReqQ(..)
39
  , ConfdReqField(..)
40
  , ConfdReplyStatus(..)
41
  , ConfdNodeRole(..)
42
  , ConfdErrorType(..)
43
  , ConfdRequest(..)
44
  , ConfdReply(..)
45
  , ConfdQuery(..)
46
  , SignedMessage(..)
47
  ) where
48

    
49
import Text.JSON
50

    
51
import qualified Ganeti.Constants as C
52
import Ganeti.THH
53

    
54
{-
55
   Note that we re-export as is from Constants the following simple items:
56
   - confdProtocolVersion
57
   - confdMaxClockSkew
58
   - confdConfigReloadTimeout
59
   - confdConfigReloadRatelimit
60
   - confdMagicFourcc
61
   - confdDefaultReqCoverage
62
   - confdClientExpireTimeout
63
   - maxUdpDataSize
64

    
65
-}
66

    
67
$(declareIADT "ConfdRequestType"
68
  [ ("ReqPing",             'C.confdReqPing )
69
  , ("ReqNodeRoleByName",   'C.confdReqNodeRoleByname )
70
  , ("ReqNodePipList",      'C.confdReqNodePipList )
71
  , ("ReqNodePipByInstPip", 'C.confdReqNodePipByInstanceIp )
72
  , ("ReqClusterMaster",    'C.confdReqClusterMaster )
73
  , ("ReqMcPipList",        'C.confdReqMcPipList )
74
  , ("ReqInstIpsList",      'C.confdReqInstancesIpsList )
75
  , ("ReqNodeDrbd",         'C.confdReqNodeDrbd )
76
  ])
77
$(makeJSONInstance ''ConfdRequestType)
78

    
79
$(declareSADT "ConfdReqField"
80
  [ ("ReqFieldName",     'C.confdReqfieldName )
81
  , ("ReqFieldIp",       'C.confdReqfieldIp )
82
  , ("ReqFieldMNodePip", 'C.confdReqfieldMnodePip )
83
  ])
84
$(makeJSONInstance ''ConfdReqField)
85

    
86
-- Confd request query fields. These are used to narrow down queries.
87
-- These must be strings rather than integers, because json-encoding
88
-- converts them to strings anyway, as they're used as dict-keys.
89

    
90
$(buildObject "ConfdReqQ" "confdReqQ"
91
  [ renameField "Ip" .
92
                optionalField $ simpleField C.confdReqqIp [t| String   |]
93
  , renameField "IpList" .
94
                defaultField [| [] |] $
95
                simpleField C.confdReqqIplist [t| [String] |]
96
  , renameField "Link" . optionalField $
97
                simpleField C.confdReqqLink [t| String   |]
98
  , renameField "Fields" . defaultField [| [] |] $
99
                simpleField C.confdReqqFields [t| [ConfdReqField] |]
100
  ])
101

    
102
-- | Confd query type. This is complex enough that we can't
103
-- automatically derive it via THH.
104
data ConfdQuery = EmptyQuery
105
                | PlainQuery String
106
                | DictQuery  ConfdReqQ
107
                  deriving (Show, Read, Eq)
108

    
109
instance JSON ConfdQuery where
110
  readJSON o = case o of
111
                 JSNull     -> return EmptyQuery
112
                 JSString s -> return . PlainQuery . fromJSString $ s
113
                 JSObject _ -> fmap DictQuery (readJSON o::Result ConfdReqQ)
114
                 _ -> fail $ "Cannot deserialise into ConfdQuery\
115
                             \ the value '" ++ show o ++ "'"
116
  showJSON cq = case cq of
117
                  EmptyQuery -> JSNull
118
                  PlainQuery s -> showJSON s
119
                  DictQuery drq -> showJSON drq
120

    
121
$(declareIADT "ConfdReplyStatus"
122
  [ ( "ReplyStatusOk",      'C.confdReplStatusOk )
123
  , ( "ReplyStatusError",   'C.confdReplStatusError )
124
  , ( "ReplyStatusNotImpl", 'C.confdReplStatusNotimplemented )
125
  ])
126
$(makeJSONInstance ''ConfdReplyStatus)
127

    
128
$(declareIADT "ConfdNodeRole"
129
  [ ( "NodeRoleMaster",    'C.confdNodeRoleMaster )
130
  , ( "NodeRoleCandidate", 'C.confdNodeRoleCandidate )
131
  , ( "NodeRoleOffline",   'C.confdNodeRoleOffline )
132
  , ( "NodeRoleDrained",   'C.confdNodeRoleDrained )
133
  , ( "NodeRoleRegular",   'C.confdNodeRoleRegular )
134
  ])
135
$(makeJSONInstance ''ConfdNodeRole)
136

    
137

    
138
-- Note that the next item is not a frozenset in Python, but we make
139
-- it a separate type for safety
140

    
141
$(declareIADT "ConfdErrorType"
142
  [ ( "ConfdErrorUnknownEntry", 'C.confdErrorUnknownEntry )
143
  , ( "ConfdErrorInternal",     'C.confdErrorInternal )
144
  , ( "ConfdErrorArgument",     'C.confdErrorArgument )
145
  ])
146
$(makeJSONInstance ''ConfdErrorType)
147

    
148
$(buildObject "ConfdRequest" "confdRq"
149
  [ simpleField "protocol" [t| Int |]
150
  , simpleField "type"     [t| ConfdRequestType |]
151
  , defaultField [| EmptyQuery |] $ simpleField "query" [t| ConfdQuery |]
152
  , simpleField "rsalt"    [t| String |]
153
  ])
154

    
155
$(buildObject "ConfdReply" "confdReply"
156
  [ simpleField "protocol" [t| Int              |]
157
  , simpleField "status"   [t| ConfdReplyStatus |]
158
  , simpleField "answer"   [t| JSValue          |]
159
  , simpleField "serial"   [t| Int              |]
160
  ])
161

    
162
$(buildObject "SignedMessage" "signedMsg"
163
  [ simpleField "hmac" [t| String |]
164
  , simpleField "msg"  [t| String |]
165
  , simpleField "salt" [t| String |]
166
  ])