Commit cfaeaaf7 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

baserlib: Add function for filling opcodes

This function makes use of the opcode parameters which now live
directly in the opcode. A number of RAPI resources can now be simplified.
Signed-off-by: default avatarMichael Hanselmann <>
Reviewed-by: default avatarIustin Pop <>
parent 65ffb373
......@@ -461,6 +461,7 @@ python_tests = \
test/ \
test/ \
test/ \
test/ \
test/ \
test/ \
test/ \
......@@ -170,6 +170,46 @@ def MakeParamsDict(opts, params):
return result
def FillOpcode(opcls, body, static):
"""Fills an opcode with body parameters.
Parameter types are checked.
@type opcls: L{opcodes.OpCode}
@param opcls: Opcode class
@type body: dict
@param body: Body parameters as received from client
@type static: dict
@param static: Static parameters which can't be modified by client
@return: Opcode object
CheckType(body, dict, "Body contents")
if static:
overwritten = set(body.keys()) & set(static.keys())
if overwritten:
raise http.HttpBadRequest("Can't overwrite static parameters %r" %
# Combine parameters
params = body.copy()
if static:
# Convert keys to strings (simplejson decodes them as unicode)
params = dict((str(key), value) for (key, value) in params.items())
op = opcls(**params) # pylint: disable-msg=W0142
except (errors.OpPrereqError, TypeError), err:
raise http.HttpBadRequest("Invalid body parameters: %s" % err)
return op
def SubmitJob(op, cl=None):
"""Generic wrapper for submit job, for better http compatibility.
# Copyright (C) 2011 Google Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
"""Script for testing ganeti.rapi.baserlib"""
import unittest
from ganeti import errors
from ganeti import opcodes
from ganeti import ht
from ganeti import http
from ganeti.rapi import baserlib
import testutils
class TestFillOpcode(unittest.TestCase):
class _TestOp(opcodes.OpCode):
("test", None, ht.TMaybeString),
def test(self):
for static in [None, {}]:
op = baserlib.FillOpcode(self._TestOp, {}, static)
self.assertTrue(isinstance(op, self._TestOp))
self.assertFalse(hasattr(op, "test"))
def testStatic(self):
op = baserlib.FillOpcode(self._TestOp, {}, {"test": "abc"})
self.assertTrue(isinstance(op, self._TestOp))
self.assertEqual(op.test, "abc")
# Overwrite static parameter
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
self._TestOp, {"test": 123}, {"test": "abc"})
def testType(self):
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
self._TestOp, {"test": [1, 2, 3]}, {})
def testStaticType(self):
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
self._TestOp, {}, {"test": [1, 2, 3]})
def testUnicode(self):
op = baserlib.FillOpcode(self._TestOp, {u"test": "abc"}, {})
self.assertTrue(isinstance(op, self._TestOp))
self.assertEqual(op.test, "abc")
op = baserlib.FillOpcode(self._TestOp, {}, {u"test": "abc"})
self.assertTrue(isinstance(op, self._TestOp))
self.assertEqual(op.test, "abc")
def testUnknownParameter(self):
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
self._TestOp, {"othervalue": 123}, None)
def testInvalidBody(self):
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
self._TestOp, "", None)
self.assertRaises(http.HttpBadRequest, baserlib.FillOpcode,
self._TestOp, range(10), None)
if __name__ == "__main__":
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment