Do not delete group if group snapshot exists

This patch adds group snapshot's existence validation
before performing group's deletion operation.

Closes-Bug: #1705375
Change-Id: I928eded513772b8b1c9f050f2d31d4334b1da8ae
(cherry picked from commit 252ff38a9d)
This commit is contained in:
TommyLike 2017-07-20 10:45:01 +08:00
parent 0d5ced6b6a
commit 42aa97ba3e
4 changed files with 34 additions and 0 deletions

View File

@ -518,6 +518,12 @@ class API(base.Base):
"but current status is: %s") % group.status
raise exception.InvalidGroup(reason=msg)
# NOTE(tommylikehu): Admin context is required to load group snapshots.
with group.obj_as_admin():
if group.group_snapshots:
raise exception.InvalidGroup(
reason=_("Group has existing snapshots."))
volumes = self.db.volume_get_all_by_generic_group(context.elevated(),
group.id)
if volumes and not delete_volumes:

View File

@ -19,6 +19,7 @@ Tests for group code.
import ddt
import mock
from six.moves import http_client
import webob
from cinder.api.v3 import groups as v3_groups
@ -625,6 +626,29 @@ class GroupsAPITestCase(test.TestCase):
self.controller.delete_group,
req, self.group1.id, body)
def test_delete_group_with_group_snapshot(self):
self.group1.status = fields.GroupStatus.AVAILABLE
self.group1.save()
g_snapshot = utils.create_group_snapshot(self.ctxt, self.group1.id)
req = fakes.HTTPRequest.blank('/v3/%s/groups/%s/action' %
(fake.PROJECT_ID, self.group1.id),
version=GROUP_MICRO_VERSION)
body = {"delete": {"delete-volumes": True}}
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.delete_group,
req, self.group1.id, body)
g_snapshot.destroy()
res_dict = self.controller.delete_group(
req, self.group1.id, body)
group = objects.Group.get_by_id(
self.ctxt, self.group1.id)
self.assertEqual(http_client.ACCEPTED, res_dict.status_int)
self.assertEqual(fields.GroupStatus.DELETING, group.status)
def test_delete_group_delete_volumes(self):
self.group1.status = fields.GroupStatus.AVAILABLE
self.group1.save()

View File

@ -114,6 +114,7 @@ class GroupAPITestCase(test.TestCase):
ret_group.host = "test_host@fakedrv#fakepool"
ret_group.status = fields.GroupStatus.AVAILABLE
ret_group.assert_not_frozen = mock.Mock(return_value=True)
ret_group.group_snapshots = []
self.group_api.delete(self.ctxt, ret_group, delete_volumes = True)
mock_volume_get_all.assert_called_once_with(mock.ANY, ret_group.id)
mock_volumes_update.assert_called_once_with(self.ctxt, [])

View File

@ -0,0 +1,3 @@
---
fixes:
- Prohibit the deletion of group if group snapshot exists.