]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Support volume_readonly_update using XML format
authorzhangyanzi <zhangyanzi@huawei.com>
Sat, 26 Oct 2013 01:39:58 +0000 (09:39 +0800)
committerzhangyanzi <zhangyanzi@huawei.com>
Tue, 5 Nov 2013 01:51:51 +0000 (09:51 +0800)
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

cinder/api/contrib/volume_actions.py
cinder/tests/api/contrib/test_volume_actions.py

index 5b482f453c9a33b8fa99d859b468bfe4cd1e8729..9c2e6cc0aa8c3d0addf4db34c2f5f9e066816878 100644 (file)
@@ -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)
index ab856e10bd19f31cb9291db2b5e553ffc0357986..3312879cf9febc09b90ee80002eb237c85b02380 100644 (file)
@@ -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):