From: Rongze Zhu Date: Wed, 12 Sep 2012 10:18:31 +0000 (+0000) Subject: Return 400 if create volume snapshot force parameter is invalid X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=c678b045c9839ed30e13fe5e7e655e3cb2e2c3f9;p=openstack-build%2Fcinder-build.git Return 400 if create volume snapshot force parameter is invalid Fixes bug #1014689 * Add is_valid_boolstr function in utils.py * Add force parameter check in SnapshotsController.create() * Add unittest for invalid force parameter. Change-Id: I0f64326f33eb4fad1cf384bd825f56f09e935f40 --- diff --git a/cinder/api/openstack/volume/snapshots.py b/cinder/api/openstack/volume/snapshots.py index 6929066d8..59d26a6e0 100644 --- a/cinder/api/openstack/volume/snapshots.py +++ b/cinder/api/openstack/volume/snapshots.py @@ -25,6 +25,7 @@ from cinder.api.openstack.volume import volumes from cinder import exception from cinder import flags from cinder.openstack.common import log as logging +from cinder import utils from cinder import volume @@ -158,7 +159,11 @@ class SnapshotsController(wsgi.Controller): msg = _("Create snapshot from volume %s") LOG.audit(msg, volume_id, context=context) - if force: + if not utils.is_valid_boolstr(force): + msg = _("Invalid value '%s' for force. ") % force + raise exception.InvalidParameterValue(err=msg) + + if utils.bool_from_str(force): new_snapshot = self.volume_api.create_snapshot_force(context, volume, snapshot.get('display_name'), diff --git a/cinder/tests/api/openstack/volume/test_snapshots.py b/cinder/tests/api/openstack/volume/test_snapshots.py index 89ebba9e7..6e32ea14e 100644 --- a/cinder/tests/api/openstack/volume/test_snapshots.py +++ b/cinder/tests/api/openstack/volume/test_snapshots.py @@ -118,6 +118,17 @@ class SnapshotApiTest(test.TestCase): self.assertEqual(resp_dict['snapshot']['display_description'], snapshot['display_description']) + snapshot = {"volume_id": "12", + "force": "**&&^^%%$$##@@", + "display_name": "Snapshot Test Name", + "display_description": "Snapshot Test Desc"} + body = dict(snapshot=snapshot) + req = fakes.HTTPRequest.blank('/v1/snapshots') + self.assertRaises(exception.InvalidParameterValue, + self.controller.create, + req, + body) + def test_snapshot_update(self): self.stubs.Set(volume.api.API, "get_snapshot", stub_snapshot_get) self.stubs.Set(volume.api.API, "update_snapshot", diff --git a/cinder/utils.py b/cinder/utils.py index 113370c08..100bbd6a8 100644 --- a/cinder/utils.py +++ b/cinder/utils.py @@ -699,6 +699,15 @@ def bool_from_str(val): return val.lower() == 'true' +def is_valid_boolstr(val): + """Check if the provided string is a valid bool string or not. """ + val = str(val).lower() + return val == 'true' or val == 'false' or \ + val == 'yes' or val == 'no' or \ + val == 'y' or val == 'n' or \ + val == '1' or val == '0' + + def is_valid_ipv4(address): """valid the address strictly as per format xxx.xxx.xxx.xxx. where xxx is a value between 0 and 255.