]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Capture exception when delete a volume detached
authorliu-sheng <liusheng@huawei.com>
Mon, 3 Nov 2014 08:45:22 +0000 (16:45 +0800)
committerliu-sheng <liusheng@huawei.com>
Mon, 3 Nov 2014 08:45:22 +0000 (16:45 +0800)
Currently, the cinder api validate volume state before validating volume
attach status, so the VolumeAttached exception will never be raised. this
patch change the order of validation to fix this issue.

Change-Id: I41451f5f9d8e60b3cfc1a8004b704285fc3ddc2b

cinder/tests/api/v2/test_volumes.py
cinder/volume/api.py

index 1fded8937ead48e008e368e24967981aa25c18e2..2f1c96f7354a8009ba957f3723951b2ae8c0d514 100644 (file)
@@ -18,6 +18,7 @@ import datetime
 
 from lxml import etree
 from oslo.config import cfg
+import six
 import six.moves.urllib.parse as urlparse
 import webob
 
@@ -1292,9 +1293,11 @@ class VolumeApiTest(test.TestCase):
         self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get)
 
         req = fakes.HTTPRequest.blank('/v2/volumes/1')
-        self.assertRaises(webob.exc.HTTPBadRequest,
-                          self.controller.delete,
-                          req, 1)
+        exp = self.assertRaises(webob.exc.HTTPBadRequest,
+                                self.controller.delete,
+                                req, 1)
+        expect_msg = "Volume cannot be deleted while in attached state"
+        self.assertEqual(expect_msg, six.text_type(exp))
 
     def test_volume_delete_no_volume(self):
         self.stubs.Set(volume_api.API, "get", stubs.stub_volume_get_notfound)
index 57c79a36710430307a9a3e464dc4684127079906..7389b711f1750cb2c41ff6e566f7296350b63019 100644 (file)
@@ -280,6 +280,10 @@ class API(base.Base):
             volume_utils.notify_about_volume_usage(context,
                                                    volume, "delete.end")
             return
+        if volume['attach_status'] == "attached":
+            # Volume is still attached, need to detach first
+            raise exception.VolumeAttached(volume_id=volume_id)
+
         if not force and volume['status'] not in ["available", "error",
                                                   "error_restoring",
                                                   "error_extending"]:
@@ -287,10 +291,6 @@ class API(base.Base):
                     "but current status is: %s") % volume['status']
             raise exception.InvalidVolume(reason=msg)
 
-        if volume['attach_status'] == "attached":
-            # Volume is still attached, need to detach first
-            raise exception.VolumeAttached(volume_id=volume_id)
-
         if volume['migration_status'] is not None:
             # Volume is migrating, wait until done
             msg = _("Volume cannot be deleted while migrating")