]> 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 15:39:16 +0000 (09:39 -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

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

index 6929066d8cfa4e702c6fec8debe4077c50e3194a..59d26a6e0501dbad711e6fe9d9e1887b126a539b 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 89ebba9e7b54cde771f552168acf9221a05ad270..6e32ea14e96ebb13c017fe6498d3702f40b5c796 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_update(self):
         self.stubs.Set(volume.api.API, "get_snapshot", stub_snapshot_get)
         self.stubs.Set(volume.api.API, "update_snapshot",
index 113370c08b0a3ab2929aa1b02aa25e569e1c2a95..100bbd6a891d143c5cd24f861a49528ed187268c 100644 (file)
@@ -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.