root / src / Ganeti / Confd / Types.hs @ d860352f
History | View | Annotate | Download (6 kB)
1 | 417cc253 | Iustin Pop | {-# LANGUAGE TemplateHaskell #-} |
---|---|---|---|
2 | 417cc253 | Iustin Pop | |
3 | 417cc253 | Iustin Pop | {-| Implementation of the Ganeti confd types. |
4 | 417cc253 | Iustin Pop | |
5 | 417cc253 | Iustin Pop | -} |
6 | 417cc253 | Iustin Pop | |
7 | 417cc253 | Iustin Pop | {- |
8 | 417cc253 | Iustin Pop | |
9 | 417cc253 | Iustin Pop | Copyright (C) 2011, 2012 Google Inc. |
10 | 417cc253 | Iustin Pop | |
11 | 417cc253 | Iustin Pop | This program is free software; you can redistribute it and/or modify |
12 | 417cc253 | Iustin Pop | it under the terms of the GNU General Public License as published by |
13 | 417cc253 | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
14 | 417cc253 | Iustin Pop | (at your option) any later version. |
15 | 417cc253 | Iustin Pop | |
16 | 417cc253 | Iustin Pop | This program is distributed in the hope that it will be useful, but |
17 | 417cc253 | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | 417cc253 | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | 417cc253 | Iustin Pop | General Public License for more details. |
20 | 417cc253 | Iustin Pop | |
21 | 417cc253 | Iustin Pop | You should have received a copy of the GNU General Public License |
22 | 417cc253 | Iustin Pop | along with this program; if not, write to the Free Software |
23 | 417cc253 | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
24 | 417cc253 | Iustin Pop | 02110-1301, USA. |
25 | 417cc253 | Iustin Pop | |
26 | 417cc253 | Iustin Pop | -} |
27 | 417cc253 | Iustin Pop | |
28 | cdc2392b | Iustin Pop | module Ganeti.Confd.Types |
29 | 417cc253 | Iustin Pop | ( C.confdProtocolVersion |
30 | 417cc253 | Iustin Pop | , C.confdMaxClockSkew |
31 | 417cc253 | Iustin Pop | , C.confdConfigReloadTimeout |
32 | 417cc253 | Iustin Pop | , C.confdConfigReloadRatelimit |
33 | 417cc253 | Iustin Pop | , C.confdMagicFourcc |
34 | 417cc253 | Iustin Pop | , C.confdDefaultReqCoverage |
35 | 417cc253 | Iustin Pop | , C.confdClientExpireTimeout |
36 | 417cc253 | Iustin Pop | , C.maxUdpDataSize |
37 | 04063ba7 | Michele Tartara | , ConfdClient(..) |
38 | 417cc253 | Iustin Pop | , ConfdRequestType(..) |
39 | 417cc253 | Iustin Pop | , ConfdReqQ(..) |
40 | 417cc253 | Iustin Pop | , ConfdReqField(..) |
41 | 417cc253 | Iustin Pop | , ConfdReplyStatus(..) |
42 | 417cc253 | Iustin Pop | , ConfdNodeRole(..) |
43 | 417cc253 | Iustin Pop | , ConfdErrorType(..) |
44 | 417cc253 | Iustin Pop | , ConfdRequest(..) |
45 | 04063ba7 | Michele Tartara | , newConfdRequest |
46 | 417cc253 | Iustin Pop | , ConfdReply(..) |
47 | 417cc253 | Iustin Pop | , ConfdQuery(..) |
48 | 417cc253 | Iustin Pop | , SignedMessage(..) |
49 | 417cc253 | Iustin Pop | ) where |
50 | 417cc253 | Iustin Pop | |
51 | 417cc253 | Iustin Pop | import Text.JSON |
52 | 04063ba7 | Michele Tartara | import qualified Network.Socket as S |
53 | 417cc253 | Iustin Pop | |
54 | 417cc253 | Iustin Pop | import qualified Ganeti.Constants as C |
55 | 04063ba7 | Michele Tartara | import Ganeti.Hash |
56 | 417cc253 | Iustin Pop | import Ganeti.THH |
57 | 04063ba7 | Michele Tartara | import Ganeti.Utils (newUUID) |
58 | 417cc253 | Iustin Pop | |
59 | 417cc253 | Iustin Pop | {- |
60 | 417cc253 | Iustin Pop | Note that we re-export as is from Constants the following simple items: |
61 | 417cc253 | Iustin Pop | - confdProtocolVersion |
62 | 417cc253 | Iustin Pop | - confdMaxClockSkew |
63 | 417cc253 | Iustin Pop | - confdConfigReloadTimeout |
64 | 417cc253 | Iustin Pop | - confdConfigReloadRatelimit |
65 | 417cc253 | Iustin Pop | - confdMagicFourcc |
66 | 417cc253 | Iustin Pop | - confdDefaultReqCoverage |
67 | 417cc253 | Iustin Pop | - confdClientExpireTimeout |
68 | 417cc253 | Iustin Pop | - maxUdpDataSize |
69 | 417cc253 | Iustin Pop | |
70 | 417cc253 | Iustin Pop | -} |
71 | 417cc253 | Iustin Pop | |
72 | 417cc253 | Iustin Pop | $(declareIADT "ConfdRequestType" |
73 | 417cc253 | Iustin Pop | [ ("ReqPing", 'C.confdReqPing ) |
74 | 417cc253 | Iustin Pop | , ("ReqNodeRoleByName", 'C.confdReqNodeRoleByname ) |
75 | 417cc253 | Iustin Pop | , ("ReqNodePipList", 'C.confdReqNodePipList ) |
76 | 417cc253 | Iustin Pop | , ("ReqNodePipByInstPip", 'C.confdReqNodePipByInstanceIp ) |
77 | 417cc253 | Iustin Pop | , ("ReqClusterMaster", 'C.confdReqClusterMaster ) |
78 | 417cc253 | Iustin Pop | , ("ReqMcPipList", 'C.confdReqMcPipList ) |
79 | 417cc253 | Iustin Pop | , ("ReqInstIpsList", 'C.confdReqInstancesIpsList ) |
80 | d81ec8b7 | Iustin Pop | , ("ReqNodeDrbd", 'C.confdReqNodeDrbd ) |
81 | 417cc253 | Iustin Pop | ]) |
82 | 417cc253 | Iustin Pop | $(makeJSONInstance ''ConfdRequestType) |
83 | 417cc253 | Iustin Pop | |
84 | 417cc253 | Iustin Pop | $(declareSADT "ConfdReqField" |
85 | 417cc253 | Iustin Pop | [ ("ReqFieldName", 'C.confdReqfieldName ) |
86 | 417cc253 | Iustin Pop | , ("ReqFieldIp", 'C.confdReqfieldIp ) |
87 | 417cc253 | Iustin Pop | , ("ReqFieldMNodePip", 'C.confdReqfieldMnodePip ) |
88 | 417cc253 | Iustin Pop | ]) |
89 | 417cc253 | Iustin Pop | $(makeJSONInstance ''ConfdReqField) |
90 | 417cc253 | Iustin Pop | |
91 | 417cc253 | Iustin Pop | -- Confd request query fields. These are used to narrow down queries. |
92 | 417cc253 | Iustin Pop | -- These must be strings rather than integers, because json-encoding |
93 | 417cc253 | Iustin Pop | -- converts them to strings anyway, as they're used as dict-keys. |
94 | 417cc253 | Iustin Pop | |
95 | 417cc253 | Iustin Pop | $(buildObject "ConfdReqQ" "confdReqQ" |
96 | 2cdaf225 | Iustin Pop | [ renameField "Ip" . |
97 | 417cc253 | Iustin Pop | optionalField $ simpleField C.confdReqqIp [t| String |] |
98 | 2cdaf225 | Iustin Pop | , renameField "IpList" . |
99 | 417cc253 | Iustin Pop | defaultField [| [] |] $ |
100 | 417cc253 | Iustin Pop | simpleField C.confdReqqIplist [t| [String] |] |
101 | 2cdaf225 | Iustin Pop | , renameField "Link" . optionalField $ |
102 | 417cc253 | Iustin Pop | simpleField C.confdReqqLink [t| String |] |
103 | 2cdaf225 | Iustin Pop | , renameField "Fields" . defaultField [| [] |] $ |
104 | 417cc253 | Iustin Pop | simpleField C.confdReqqFields [t| [ConfdReqField] |] |
105 | 417cc253 | Iustin Pop | ]) |
106 | 417cc253 | Iustin Pop | |
107 | 417cc253 | Iustin Pop | -- | Confd query type. This is complex enough that we can't |
108 | 417cc253 | Iustin Pop | -- automatically derive it via THH. |
109 | 417cc253 | Iustin Pop | data ConfdQuery = EmptyQuery |
110 | 417cc253 | Iustin Pop | | PlainQuery String |
111 | 417cc253 | Iustin Pop | | DictQuery ConfdReqQ |
112 | 139c0683 | Iustin Pop | deriving (Show, Eq) |
113 | 417cc253 | Iustin Pop | |
114 | 417cc253 | Iustin Pop | instance JSON ConfdQuery where |
115 | 417cc253 | Iustin Pop | readJSON o = case o of |
116 | 417cc253 | Iustin Pop | JSNull -> return EmptyQuery |
117 | 417cc253 | Iustin Pop | JSString s -> return . PlainQuery . fromJSString $ s |
118 | 417cc253 | Iustin Pop | JSObject _ -> fmap DictQuery (readJSON o::Result ConfdReqQ) |
119 | 417cc253 | Iustin Pop | _ -> fail $ "Cannot deserialise into ConfdQuery\ |
120 | 417cc253 | Iustin Pop | \ the value '" ++ show o ++ "'" |
121 | 417cc253 | Iustin Pop | showJSON cq = case cq of |
122 | 417cc253 | Iustin Pop | EmptyQuery -> JSNull |
123 | 417cc253 | Iustin Pop | PlainQuery s -> showJSON s |
124 | 417cc253 | Iustin Pop | DictQuery drq -> showJSON drq |
125 | 417cc253 | Iustin Pop | |
126 | 417cc253 | Iustin Pop | $(declareIADT "ConfdReplyStatus" |
127 | 417cc253 | Iustin Pop | [ ( "ReplyStatusOk", 'C.confdReplStatusOk ) |
128 | 417cc253 | Iustin Pop | , ( "ReplyStatusError", 'C.confdReplStatusError ) |
129 | 417cc253 | Iustin Pop | , ( "ReplyStatusNotImpl", 'C.confdReplStatusNotimplemented ) |
130 | 417cc253 | Iustin Pop | ]) |
131 | 417cc253 | Iustin Pop | $(makeJSONInstance ''ConfdReplyStatus) |
132 | 417cc253 | Iustin Pop | |
133 | 417cc253 | Iustin Pop | $(declareIADT "ConfdNodeRole" |
134 | 417cc253 | Iustin Pop | [ ( "NodeRoleMaster", 'C.confdNodeRoleMaster ) |
135 | 417cc253 | Iustin Pop | , ( "NodeRoleCandidate", 'C.confdNodeRoleCandidate ) |
136 | 417cc253 | Iustin Pop | , ( "NodeRoleOffline", 'C.confdNodeRoleOffline ) |
137 | 417cc253 | Iustin Pop | , ( "NodeRoleDrained", 'C.confdNodeRoleDrained ) |
138 | 417cc253 | Iustin Pop | , ( "NodeRoleRegular", 'C.confdNodeRoleRegular ) |
139 | 417cc253 | Iustin Pop | ]) |
140 | 417cc253 | Iustin Pop | $(makeJSONInstance ''ConfdNodeRole) |
141 | 417cc253 | Iustin Pop | |
142 | 417cc253 | Iustin Pop | |
143 | 417cc253 | Iustin Pop | -- Note that the next item is not a frozenset in Python, but we make |
144 | 417cc253 | Iustin Pop | -- it a separate type for safety |
145 | 417cc253 | Iustin Pop | |
146 | 417cc253 | Iustin Pop | $(declareIADT "ConfdErrorType" |
147 | 417cc253 | Iustin Pop | [ ( "ConfdErrorUnknownEntry", 'C.confdErrorUnknownEntry ) |
148 | 417cc253 | Iustin Pop | , ( "ConfdErrorInternal", 'C.confdErrorInternal ) |
149 | 417cc253 | Iustin Pop | , ( "ConfdErrorArgument", 'C.confdErrorArgument ) |
150 | 417cc253 | Iustin Pop | ]) |
151 | 417cc253 | Iustin Pop | $(makeJSONInstance ''ConfdErrorType) |
152 | 417cc253 | Iustin Pop | |
153 | 5b11f8db | Iustin Pop | $(buildObject "ConfdRequest" "confdRq" |
154 | 417cc253 | Iustin Pop | [ simpleField "protocol" [t| Int |] |
155 | 417cc253 | Iustin Pop | , simpleField "type" [t| ConfdRequestType |] |
156 | 417cc253 | Iustin Pop | , defaultField [| EmptyQuery |] $ simpleField "query" [t| ConfdQuery |] |
157 | 417cc253 | Iustin Pop | , simpleField "rsalt" [t| String |] |
158 | 417cc253 | Iustin Pop | ]) |
159 | 417cc253 | Iustin Pop | |
160 | 04063ba7 | Michele Tartara | -- | Client side helper function for creating requests. It automatically fills |
161 | 04063ba7 | Michele Tartara | -- in some default values. |
162 | 04063ba7 | Michele Tartara | newConfdRequest :: ConfdRequestType -> ConfdQuery -> IO ConfdRequest |
163 | 04063ba7 | Michele Tartara | newConfdRequest reqType query = do |
164 | 04063ba7 | Michele Tartara | rsalt <- newUUID |
165 | 04063ba7 | Michele Tartara | return $ ConfdRequest C.confdProtocolVersion reqType query rsalt |
166 | 04063ba7 | Michele Tartara | |
167 | 417cc253 | Iustin Pop | $(buildObject "ConfdReply" "confdReply" |
168 | 417cc253 | Iustin Pop | [ simpleField "protocol" [t| Int |] |
169 | 417cc253 | Iustin Pop | , simpleField "status" [t| ConfdReplyStatus |] |
170 | 417cc253 | Iustin Pop | , simpleField "answer" [t| JSValue |] |
171 | 417cc253 | Iustin Pop | , simpleField "serial" [t| Int |] |
172 | 417cc253 | Iustin Pop | ]) |
173 | 417cc253 | Iustin Pop | |
174 | 417cc253 | Iustin Pop | $(buildObject "SignedMessage" "signedMsg" |
175 | 417cc253 | Iustin Pop | [ simpleField "hmac" [t| String |] |
176 | 417cc253 | Iustin Pop | , simpleField "msg" [t| String |] |
177 | 417cc253 | Iustin Pop | , simpleField "salt" [t| String |] |
178 | 417cc253 | Iustin Pop | ]) |
179 | 04063ba7 | Michele Tartara | |
180 | 04063ba7 | Michele Tartara | -- | Data type containing information used by the Confd client. |
181 | 04063ba7 | Michele Tartara | data ConfdClient = ConfdClient |
182 | 04063ba7 | Michele Tartara | { hmacKey :: HashKey -- ^ The hmac used for authentication |
183 | 04063ba7 | Michele Tartara | , peers :: [String] -- ^ The list of nodes to query |
184 | 04063ba7 | Michele Tartara | , serverPort :: S.PortNumber -- ^ The port where confd server is listening |
185 | 04063ba7 | Michele Tartara | } |