]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Handle KeyManager exception when deleting a volume
authorEli Qiao <liyong.qiao@intel.com>
Thu, 6 Aug 2015 08:58:21 +0000 (16:58 +0800)
committerEli Qiao <liyong.qiao@intel.com>
Tue, 1 Sep 2015 01:24:23 +0000 (01:24 +0000)
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

cinder/tests/unit/test_volume.py
cinder/volume/api.py

index 537e1586998c28c7fdff0be2b814e0fe17d9c41e..8ffd4b23969dff6bb5d43ad026e2fd94f13673a6 100644 (file)
@@ -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):
index 18500e479d9d6e04853218685d69e6c93ce522b6..d2d26de3be08ac9f931aa5060743b9f5eaa64770 100644 (file)
@@ -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,