From b6375ca0efa5fb9761f7ea2eff9165966189bb0d Mon Sep 17 00:00:00 2001 From: liu-sheng Date: Mon, 3 Nov 2014 16:45:22 +0800 Subject: [PATCH] Capture exception when delete a volume detached 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 | 9 ++++++--- cinder/volume/api.py | 8 ++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index 1fded8937..2f1c96f73 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -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) diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 57c79a367..7389b711f 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -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") -- 2.45.2