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