root / src / Ganeti / Confd / Types.hs @ ace37e24
History | View | Annotate | Download (6 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 |
, ConfdClient(..) |
38 |
, ConfdRequestType(..) |
39 |
, ConfdReqQ(..) |
40 |
, ConfdReqField(..) |
41 |
, ConfdReplyStatus(..) |
42 |
, ConfdNodeRole(..) |
43 |
, ConfdErrorType(..) |
44 |
, ConfdRequest(..) |
45 |
, newConfdRequest |
46 |
, ConfdReply(..) |
47 |
, ConfdQuery(..) |
48 |
, SignedMessage(..) |
49 |
) where |
50 |
|
51 |
import Text.JSON |
52 |
import qualified Network.Socket as S |
53 |
|
54 |
import qualified Ganeti.Constants as C |
55 |
import Ganeti.Hash |
56 |
import Ganeti.THH |
57 |
import Ganeti.Utils (newUUID) |
58 |
|
59 |
{- |
60 |
Note that we re-export as is from Constants the following simple items: |
61 |
- confdProtocolVersion |
62 |
- confdMaxClockSkew |
63 |
- confdConfigReloadTimeout |
64 |
- confdConfigReloadRatelimit |
65 |
- confdMagicFourcc |
66 |
- confdDefaultReqCoverage |
67 |
- confdClientExpireTimeout |
68 |
- maxUdpDataSize |
69 |
|
70 |
-} |
71 |
|
72 |
$(declareIADT "ConfdRequestType" |
73 |
[ ("ReqPing", 'C.confdReqPing ) |
74 |
, ("ReqNodeRoleByName", 'C.confdReqNodeRoleByname ) |
75 |
, ("ReqNodePipList", 'C.confdReqNodePipList ) |
76 |
, ("ReqNodePipByInstPip", 'C.confdReqNodePipByInstanceIp ) |
77 |
, ("ReqClusterMaster", 'C.confdReqClusterMaster ) |
78 |
, ("ReqMcPipList", 'C.confdReqMcPipList ) |
79 |
, ("ReqInstIpsList", 'C.confdReqInstancesIpsList ) |
80 |
, ("ReqNodeDrbd", 'C.confdReqNodeDrbd ) |
81 |
]) |
82 |
$(makeJSONInstance ''ConfdRequestType) |
83 |
|
84 |
$(declareSADT "ConfdReqField" |
85 |
[ ("ReqFieldName", 'C.confdReqfieldName ) |
86 |
, ("ReqFieldIp", 'C.confdReqfieldIp ) |
87 |
, ("ReqFieldMNodePip", 'C.confdReqfieldMnodePip ) |
88 |
]) |
89 |
$(makeJSONInstance ''ConfdReqField) |
90 |
|
91 |
-- Confd request query fields. These are used to narrow down queries. |
92 |
-- These must be strings rather than integers, because json-encoding |
93 |
-- converts them to strings anyway, as they're used as dict-keys. |
94 |
|
95 |
$(buildObject "ConfdReqQ" "confdReqQ" |
96 |
[ renameField "Ip" . |
97 |
optionalField $ simpleField C.confdReqqIp [t| String |] |
98 |
, renameField "IpList" . |
99 |
defaultField [| [] |] $ |
100 |
simpleField C.confdReqqIplist [t| [String] |] |
101 |
, renameField "Link" . optionalField $ |
102 |
simpleField C.confdReqqLink [t| String |] |
103 |
, renameField "Fields" . defaultField [| [] |] $ |
104 |
simpleField C.confdReqqFields [t| [ConfdReqField] |] |
105 |
]) |
106 |
|
107 |
-- | Confd query type. This is complex enough that we can't |
108 |
-- automatically derive it via THH. |
109 |
data ConfdQuery = EmptyQuery |
110 |
| PlainQuery String |
111 |
| DictQuery ConfdReqQ |
112 |
deriving (Show, Eq) |
113 |
|
114 |
instance JSON ConfdQuery where |
115 |
readJSON o = case o of |
116 |
JSNull -> return EmptyQuery |
117 |
JSString s -> return . PlainQuery . fromJSString $ s |
118 |
JSObject _ -> fmap DictQuery (readJSON o::Result ConfdReqQ) |
119 |
_ -> fail $ "Cannot deserialise into ConfdQuery\ |
120 |
\ the value '" ++ show o ++ "'" |
121 |
showJSON cq = case cq of |
122 |
EmptyQuery -> JSNull |
123 |
PlainQuery s -> showJSON s |
124 |
DictQuery drq -> showJSON drq |
125 |
|
126 |
$(declareIADT "ConfdReplyStatus" |
127 |
[ ( "ReplyStatusOk", 'C.confdReplStatusOk ) |
128 |
, ( "ReplyStatusError", 'C.confdReplStatusError ) |
129 |
, ( "ReplyStatusNotImpl", 'C.confdReplStatusNotimplemented ) |
130 |
]) |
131 |
$(makeJSONInstance ''ConfdReplyStatus) |
132 |
|
133 |
$(declareIADT "ConfdNodeRole" |
134 |
[ ( "NodeRoleMaster", 'C.confdNodeRoleMaster ) |
135 |
, ( "NodeRoleCandidate", 'C.confdNodeRoleCandidate ) |
136 |
, ( "NodeRoleOffline", 'C.confdNodeRoleOffline ) |
137 |
, ( "NodeRoleDrained", 'C.confdNodeRoleDrained ) |
138 |
, ( "NodeRoleRegular", 'C.confdNodeRoleRegular ) |
139 |
]) |
140 |
$(makeJSONInstance ''ConfdNodeRole) |
141 |
|
142 |
|
143 |
-- Note that the next item is not a frozenset in Python, but we make |
144 |
-- it a separate type for safety |
145 |
|
146 |
$(declareIADT "ConfdErrorType" |
147 |
[ ( "ConfdErrorUnknownEntry", 'C.confdErrorUnknownEntry ) |
148 |
, ( "ConfdErrorInternal", 'C.confdErrorInternal ) |
149 |
, ( "ConfdErrorArgument", 'C.confdErrorArgument ) |
150 |
]) |
151 |
$(makeJSONInstance ''ConfdErrorType) |
152 |
|
153 |
$(buildObject "ConfdRequest" "confdRq" |
154 |
[ simpleField "protocol" [t| Int |] |
155 |
, simpleField "type" [t| ConfdRequestType |] |
156 |
, defaultField [| EmptyQuery |] $ simpleField "query" [t| ConfdQuery |] |
157 |
, simpleField "rsalt" [t| String |] |
158 |
]) |
159 |
|
160 |
-- | Client side helper function for creating requests. It automatically fills |
161 |
-- in some default values. |
162 |
newConfdRequest :: ConfdRequestType -> ConfdQuery -> IO ConfdRequest |
163 |
newConfdRequest reqType query = do |
164 |
rsalt <- newUUID |
165 |
return $ ConfdRequest C.confdProtocolVersion reqType query rsalt |
166 |
|
167 |
$(buildObject "ConfdReply" "confdReply" |
168 |
[ simpleField "protocol" [t| Int |] |
169 |
, simpleField "status" [t| ConfdReplyStatus |] |
170 |
, simpleField "answer" [t| JSValue |] |
171 |
, simpleField "serial" [t| Int |] |
172 |
]) |
173 |
|
174 |
$(buildObject "SignedMessage" "signedMsg" |
175 |
[ simpleField "hmac" [t| String |] |
176 |
, simpleField "msg" [t| String |] |
177 |
, simpleField "salt" [t| String |] |
178 |
]) |
179 |
|
180 |
-- | Data type containing information used by the Confd client. |
181 |
data ConfdClient = ConfdClient |
182 |
{ hmacKey :: HashKey -- ^ The hmac used for authentication |
183 |
, peers :: [String] -- ^ The list of nodes to query |
184 |
, serverPort :: S.PortNumber -- ^ The port where confd server is listening |
185 |
} |