Container deletion will now clean up Consumers

Previously, deleting a Container would orphan any existing Consumers.
They will now be deleted during the Container deletion process.

Change-Id: Ib7942911895a33cc34e8a297a63c463cf488f042
This commit is contained in:
Adam Harwell
2014-11-24 13:13:04 -06:00
parent 0b8743948a
commit 668ebe87d7
2 changed files with 48 additions and 0 deletions
+10
View File
@@ -71,6 +71,10 @@ class ContainerController(object):
@controllers.handle_exceptions(u._('Container deletion'))
@controllers.enforce_rbac('container:delete')
def on_delete(self, keystone_id, **kwargs):
container_consumers = self.consumer_repo.get_by_container_id(
self.container_id,
suppress_exception=True
)
try:
self.container_repo.delete_entity_by_id(
entity_id=self.container_id,
@@ -80,6 +84,12 @@ class ContainerController(object):
LOG.exception('Problem deleting container')
container_not_found()
for consumer in container_consumers[0]:
try:
self.consumer_repo.delete_entity_by_id(consumer.id)
except exception.NotFound:
pass
class ContainersController(object):
"""Handles Container creation requests."""
+38
View File
@@ -2294,6 +2294,7 @@ class WhenGettingOrDeletingConsumersUsingConsumerResource(FunctionalTest):
self.container = create_container(id_ref='id1')
self.consumer = create_consumer(self.container.id, id_ref='id2')
self.consumer2 = create_consumer(self.container.id, id_ref='id3')
self.consumer_ref = {
'name': self.consumer.name,
@@ -2392,6 +2393,43 @@ class WhenGettingOrDeletingConsumersUsingConsumerResource(FunctionalTest):
# Error response should have json content type
self.assertEqual(resp.content_type, "application/json")
def test_should_delete_consumers_on_container_delete(self):
consumers = [self.consumer, self.consumer2]
ret_val = (consumers, 0, 0, 1)
self.consumer_repo.get_by_container_id.return_value = ret_val
resp = self.app.delete(
'/containers/{0}/'.format(self.container.id)
)
self.assertEqual(resp.status_int, 204)
# Verify consumers were deleted
calls = []
for consumer in consumers:
calls.append(mock.call(consumer.id))
self.consumer_repo.delete_entity_by_id.assert_has_calls(
calls, any_order=True
)
def test_should_pass_on_container_delete_with_missing_consumers(self):
consumers = [self.consumer, self.consumer2]
ret_val = (consumers, 0, 0, 1)
self.consumer_repo.get_by_container_id.return_value = ret_val
self.consumer_repo.delete_entity_by_id.side_effect = excep.NotFound
resp = self.app.delete(
'/containers/{0}/'.format(self.container.id)
)
self.assertEqual(resp.status_int, 204)
# Verify consumers were deleted
calls = []
for consumer in consumers:
calls.append(mock.call(consumer.id))
self.consumer_repo.delete_entity_by_id.assert_has_calls(
calls, any_order=True
)
class WhenGettingContainersListUsingResource(FunctionalTest):
def setUp(self):