]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Return 400 if create volume snapshot force parameter is invalid
authorRongze Zhu <zrzhit@gmail.com>
Wed, 12 Sep 2012 10:18:31 +0000 (10:18 +0000)
committerJohn Griffith <john.griffith@solidfire.com>
Fri, 21 Sep 2012 18:01:29 +0000 (12:01 -0600)
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
(cherry picked from commit c678b045c9839ed30e13fe5e7e655e3cb2e2c3f9)

cinder/api/openstack/volume/snapshots.py
cinder/tests/api/openstack/volume/test_snapshots.py
cinder/utils.py

index 192dee223ed68880d7ea2d3ed43e9ae8d2dafc1a..f4f79f54def3e76973b7f1efba6aca258d3a0ae4 100644 (file)
@@ -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'),
index 9a0e8f24f212182adc21904548f633709f68bc2e..f9d61b39ee059b47c89351bed49c4c3f66cd9cb6 100644 (file)
@@ -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_delete(self):
         self.stubs.Set(volume.api.API, "get_snapshot", stub_snapshot_get)
         self.stubs.Set(volume.api.API, "delete_snapshot", stub_snapshot_delete)
index bfcda366a2b3feedbd4f0cebd9411959d61aa2ac..0e6038cda80acb2fee8e87df45805a097cb3c751 100644 (file)
@@ -691,6 +691,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.