From: Eli Qiao Date: Thu, 6 Aug 2015 08:58:21 +0000 (+0800) Subject: Handle KeyManager exception when deleting a volume X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=35a90fd3b599186ef49da2908f9ac452446339db;p=openstack-build%2Fcinder-build.git Handle KeyManager exception when deleting a volume This patch adds exception handler when deleting key of an encryption volume. Raise 400 invalid volume instead of 500 to give the user a better understanding why deleting failed. Closes-bug: #1482120 Change-Id: I591f65823d1f212e92434323905cc9c4fbe6fd96 --- diff --git a/cinder/tests/unit/test_volume.py b/cinder/tests/unit/test_volume.py index 537e15869..8ffd4b239 100644 --- a/cinder/tests/unit/test_volume.py +++ b/cinder/tests/unit/test_volume.py @@ -5592,6 +5592,19 @@ class VolumeTestCase(BaseVolumeTestCase): discover) self.assertEqual(expected, capabilities['properties']) + def test_delete_encryptied_volume(self): + self.volume_params['status'] = 'active' + volume = tests_utils.create_volume(self.context, + **self.volume_params) + vol_api = cinder.volume.api.API() + with mock.patch.object( + vol_api.key_manager, + 'delete_key', + side_effect=Exception): + self.assertRaises(exception.InvalidVolume, + vol_api.delete, + self.context, volume) + class CopyVolumeToImageTestCase(BaseVolumeTestCase): def fake_local_path(self, volume): diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 18500e479..d2d26de3b 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -394,7 +394,11 @@ class API(base.Base): # because the volume cannot be decrypted without its key. encryption_key_id = volume.get('encryption_key_id', None) if encryption_key_id is not None: - self.key_manager.delete_key(context, encryption_key_id) + try: + self.key_manager.delete_key(context, encryption_key_id) + except Exception as e: + msg = _("Unable to delete encrypted volume: %s.") % e.msg + raise exception.InvalidVolume(reason=msg) now = timeutils.utcnow() vref = self.db.volume_update(context,