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
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)
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):