From 41646ab48be69f01afa943190d6fc41234c78976 Mon Sep 17 00:00:00 2001 From: Rakesh H S Date: Tue, 23 Jun 2015 19:36:24 +0530 Subject: [PATCH] Return 404 if volume type encryption is not found This commit adds a check for volume type encryption existence before marking it as deleted. If it isn't present call returns 404 error code so that it can be handled properly in services that use the REST API (like Heat). APIImpact HTTP 404 will be returned if volume type encryption is not found. Change-Id: I3107b8df0121de1ebe9e812ef7a842d09500e030 Closes-Bug: #1468751 --- cinder/api/contrib/volume_type_encryption.py | 5 ++++- cinder/db/sqlalchemy/api.py | 3 +++ .../contrib/test_volume_type_encryption.py | 21 +++++++++++++++++++ cinder/tests/unit/test_db_api.py | 8 ++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/cinder/api/contrib/volume_type_encryption.py b/cinder/api/contrib/volume_type_encryption.py index 26dd9d1a5..288169a61 100644 --- a/cinder/api/contrib/volume_type_encryption.py +++ b/cinder/api/contrib/volume_type_encryption.py @@ -188,7 +188,10 @@ class VolumeTypeEncryptionController(wsgi.Controller): expl = _('Cannot delete encryption specs. Volume type in use.') raise webob.exc.HTTPBadRequest(explanation=expl) else: - db.volume_type_encryption_delete(context, type_id) + try: + db.volume_type_encryption_delete(context, type_id) + except exception.VolumeTypeEncryptionNotFound as ex: + raise webob.exc.HTTPNotFound(explanation=ex.msg) return webob.Response(status_int=202) diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index 7dc908013..7f2dd33b3 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -2946,6 +2946,9 @@ def volume_type_encryption_delete(context, volume_type_id): with session.begin(): encryption = volume_type_encryption_get(context, volume_type_id, session) + if not encryption: + raise exception.VolumeTypeEncryptionNotFound( + type_id=volume_type_id) encryption.update({'deleted': True, 'deleted_at': timeutils.utcnow(), 'updated_at': literal_column('updated_at')}) diff --git a/cinder/tests/unit/api/contrib/test_volume_type_encryption.py b/cinder/tests/unit/api/contrib/test_volume_type_encryption.py index 036a93b9d..dd29802d0 100644 --- a/cinder/tests/unit/api/contrib/test_volume_type_encryption.py +++ b/cinder/tests/unit/api/contrib/test_volume_type_encryption.py @@ -475,6 +475,27 @@ class VolumeTypeEncryptionTest(test.TestCase): db.volume_type_destroy(context.get_admin_context(), volume_type['id']) + def test_delete_with_no_encryption(self): + volume_type = self._default_volume_type + # create an volume type + db.volume_type_create(context.get_admin_context(), volume_type) + + # without creating encryption type, try to delete + # and check if 404 is raised. + res = self._get_response(volume_type, req_method='DELETE', + req_headers='application/json', + url='/v2/fake/types/%s/encryption/provider') + self.assertEqual(404, res.status_code) + expected = { + "itemNotFound": { + "message": "Volume type encryption for type " + "fake_type_id does not exist.", + "code": 404 + } + } + self.assertEqual(expected, json.loads(res.body)) + db.volume_type_destroy(context.get_admin_context(), volume_type['id']) + def test_update_item(self): volume_type = self._default_volume_type diff --git a/cinder/tests/unit/test_db_api.py b/cinder/tests/unit/test_db_api.py index 1688759cd..6acd4ab79 100644 --- a/cinder/tests/unit/test_db_api.py +++ b/cinder/tests/unit/test_db_api.py @@ -1207,7 +1207,7 @@ class DBAPIEncryptionTestCase(BaseTest): self._assertEqualObjects(encryption, encryption_get, self._ignored_keys) - def test_volume_type_update_with_no_create(self): + def test_volume_type_encryption_update_with_no_create(self): self.assertRaises(exception.VolumeTypeEncryptionNotFound, db.volume_type_encryption_update, self.ctxt, @@ -1234,6 +1234,12 @@ class DBAPIEncryptionTestCase(BaseTest): encryption['volume_type_id']) self.assertIsNone(encryption_get) + def test_volume_type_encryption_delete_no_create(self): + self.assertRaises(exception.VolumeTypeEncryptionNotFound, + db.volume_type_encryption_delete, + self.ctxt, + 'fake_no_create_type') + def test_volume_encryption_get(self): # normal volume -- metadata should be None volume = db.volume_create(self.ctxt, {}) -- 2.45.2