Merge "Do not delete group if group snapshot exists"

This commit is contained in:
Jenkins 2017-07-21 18:28:48 +00:00 committed by Gerrit Code Review
commit 54d6c2da9c
4 changed files with 33 additions and 0 deletions

View File

@ -517,6 +517,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

@ -654,6 +654,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.