From: zhangyanzi Date: Sat, 26 Oct 2013 01:39:58 +0000 (+0800) Subject: Support volume_readonly_update using XML format X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=76497ce9607df98e9e10fb1de1b6e607432f731b;p=openstack-build%2Fcinder-build.git Support volume_readonly_update using XML format Almost all API support JSON and XML format together, But this API does not support xml format. In the request of xml format, the type of parameter "readonly" in the body is string. In the request of JSON format, the type of parameter "readonly" in the body is bool. But the code only support bool, not support string. This branch is to solve this problem. Closes-bug #1244848 Change-Id: I7f2b09a0063df9daad3fdd09789f8e19c43b0aa3 --- diff --git a/cinder/api/contrib/volume_actions.py b/cinder/api/contrib/volume_actions.py index 5b482f453..9c2e6cc0a 100644 --- a/cinder/api/contrib/volume_actions.py +++ b/cinder/api/contrib/volume_actions.py @@ -20,6 +20,7 @@ from cinder.api import xmlutil from cinder import exception from cinder.openstack.common import log as logging from cinder.openstack.common.rpc import common as rpc_common +from cinder.openstack.common import strutils from cinder import utils from cinder import volume @@ -274,16 +275,17 @@ class VolumeActionsController(wsgi.Controller): except exception.VolumeNotFound as error: raise webob.exc.HTTPNotFound(explanation=error.msg) - if not self.is_valid_body(body, 'os-update_readonly_flag'): - msg = _("No 'os-update_readonly_flag' was specified " - "in request.") - raise webob.exc.HTTPBadRequest(explanation=msg) - readonly_flag = body['os-update_readonly_flag'].get('readonly') - - if not isinstance(readonly_flag, bool): - msg = _("Volume 'readonly' flag must be specified " - "in request as a boolean.") + if isinstance(readonly_flag, basestring): + try: + readonly_flag = strutils.bool_from_string(readonly_flag, + strict=True) + except ValueError: + msg = _("Bad value for 'readonly'") + raise webob.exc.HTTPBadRequest(explanation=msg) + + elif not isinstance(readonly_flag, bool): + msg = _("'readonly' not string or bool") raise webob.exc.HTTPBadRequest(explanation=msg) self.volume_api.update_readonly_flag(context, volume, readonly_flag) diff --git a/cinder/tests/api/contrib/test_volume_actions.py b/cinder/tests/api/contrib/test_volume_actions.py index ab856e10b..3312879cf 100644 --- a/cinder/tests/api/contrib/test_volume_actions.py +++ b/cinder/tests/api/contrib/test_volume_actions.py @@ -209,14 +209,21 @@ class VolumeActionsTest(test.TestCase): self.stubs.Set(volume.API, 'update_readonly_flag', fake_update_readonly_flag) - body = {'os-update_readonly_flag': {'readonly': True}} - req = webob.Request.blank('/v2/fake/volumes/1/action') - req.method = "POST" - req.body = jsonutils.dumps(body) - req.headers["content-type"] = "application/json" - - res = req.get_response(fakes.wsgi_app()) - self.assertEqual(res.status_int, 202) + def make_update_readonly_flag_test(self, readonly, return_code): + body = {"os-update_readonly_flag": {"readonly": readonly}} + req = webob.Request.blank('/v2/fake/volumes/1/action') + req.method = "POST" + req.body = jsonutils.dumps(body) + req.headers["content-type"] = "application/json" + res = req.get_response(fakes.wsgi_app()) + self.assertEqual(res.status_int, return_code) + + make_update_readonly_flag_test(self, True, 202) + make_update_readonly_flag_test(self, '0', 202) + make_update_readonly_flag_test(self, 'true', 202) + make_update_readonly_flag_test(self, 'false', 202) + make_update_readonly_flag_test(self, 'tt', 400) + make_update_readonly_flag_test(self, 11, 400) def stub_volume_get(self, context, volume_id):