Revision b166ef84
b/lib/rapi/baserlib.py | ||
---|---|---|
170 | 170 |
return result |
171 | 171 |
|
172 | 172 |
|
173 |
def FillOpcode(opcls, body, static): |
|
173 |
def FillOpcode(opcls, body, static, rename=None):
|
|
174 | 174 |
"""Fills an opcode with body parameters. |
175 | 175 |
|
176 | 176 |
Parameter types are checked. |
... | ... | |
181 | 181 |
@param body: Body parameters as received from client |
182 | 182 |
@type static: dict |
183 | 183 |
@param static: Static parameters which can't be modified by client |
184 |
@type rename: dict |
|
185 |
@param rename: Renamed parameters, key as old name, value as new name |
|
184 | 186 |
@return: Opcode object |
185 | 187 |
|
186 | 188 |
""" |
187 | 189 |
CheckType(body, dict, "Body contents") |
188 | 190 |
|
191 |
# Make copy to be modified |
|
192 |
params = body.copy() |
|
193 |
|
|
194 |
if rename: |
|
195 |
for old, new in rename.items(): |
|
196 |
if new in params and old in params: |
|
197 |
raise http.HttpBadRequest("Parameter '%s' was renamed to '%s', but" |
|
198 |
" both are specified" % |
|
199 |
(old, new)) |
|
200 |
if old in params: |
|
201 |
assert new not in params |
|
202 |
params[new] = params.pop(old) |
|
203 |
|
|
189 | 204 |
if static: |
190 |
overwritten = set(body.keys()) & set(static.keys())
|
|
205 |
overwritten = set(params.keys()) & set(static.keys())
|
|
191 | 206 |
if overwritten: |
192 | 207 |
raise http.HttpBadRequest("Can't overwrite static parameters %r" % |
193 | 208 |
overwritten) |
194 | 209 |
|
195 |
# Combine parameters |
|
196 |
params = body.copy() |
|
197 |
|
|
198 |
if static: |
|
199 | 210 |
params.update(static) |
200 | 211 |
|
201 | 212 |
# Convert keys to strings (simplejson decodes them as unicode) |
b/test/ganeti.rapi.baserlib_unittest.py | ||
---|---|---|
80 | 80 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode, |
81 | 81 |
self.OpTest, range(10), None) |
82 | 82 |
|
83 |
def testRenameBothSpecified(self): |
|
84 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode, |
|
85 |
self.OpTest, { "old": 123, "new": 999, }, None, |
|
86 |
rename={ "old": "new", }) |
|
87 |
|
|
88 |
def testRename(self): |
|
89 |
value = "Hello World" |
|
90 |
op = baserlib.FillOpcode(self.OpTest, { "data": value, }, None, |
|
91 |
rename={ "data": "test", }) |
|
92 |
self.assertEqual(op.test, value) |
|
93 |
|
|
94 |
def testRenameStatic(self): |
|
95 |
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode, |
|
96 |
self.OpTest, { "data": 0, }, { "test": None, }, |
|
97 |
rename={ "data": "test", }) |
|
98 |
|
|
83 | 99 |
|
84 | 100 |
if __name__ == "__main__": |
85 | 101 |
testutils.GanetiTestProgram() |
Also available in: Unified diff