From eae19c1a7c2303e398b3348651482f49ea7508ce Mon Sep 17 00:00:00 2001 From: Brooklyn Chen Date: Sun, 19 May 2013 18:31:50 +0800 Subject: [PATCH] Shows a warning message when deleting a container that contains data. We cannot delete objects in the container since there may be millions of objects in that. The batch deletion should be done in swift instead of Horizon. The batch remove of objects function is only in swift CLI command now. This commit replace the unclear message with a warning message that tells the user the container is not empy. Related bug: https://bugs.launchpad.net/horizon/+bug/1034890 Fixes bug: #1096084 Change-Id: I59b46a3535b8654734c3dae4eec916ab1b53c716 --- openstack_dashboard/api/swift.py | 9 +++++++++ .../dashboards/project/containers/tests.py | 14 ++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/openstack_dashboard/api/swift.py b/openstack_dashboard/api/swift.py index 860c49297..ead62568e 100644 --- a/openstack_dashboard/api/swift.py +++ b/openstack_dashboard/api/swift.py @@ -25,6 +25,8 @@ import swiftclient from django.conf import settings from horizon import exceptions +from horizon import messages +from django.utils.translation import ugettext_lazy as _ from openstack_dashboard.api.base import url_for, APIDictWrapper @@ -135,6 +137,13 @@ def swift_create_container(request, name): def swift_delete_container(request, name): + # It cannot be deleted if it's not empty. The batch remove of objects + # be done in swiftclient instead of Horizon. + objects, more = swift_get_objects(request, name) + if objects: + messages.warning(request, + _("The container cannot be deleted since it's not empty.")) + return False swift_api(request).delete_container(name) return True diff --git a/openstack_dashboard/dashboards/project/containers/tests.py b/openstack_dashboard/dashboards/project/containers/tests.py index f4ebe822d..bd5cf9f16 100644 --- a/openstack_dashboard/dashboards/project/containers/tests.py +++ b/openstack_dashboard/dashboards/project/containers/tests.py @@ -50,7 +50,7 @@ class SwiftTests(test.TestCase): resp_containers = res.context['table'].data self.assertEqual(len(resp_containers), len(containers)) - @test.create_stubs({api.swift: ('swift_delete_container',)}) + @test.create_stubs({api.swift: ('swift_delete_container', )}) def test_delete_container(self): container = self.containers.get(name=u"container_two\u6346") api.swift.swift_delete_container(IsA(http.HttpRequest), container.name) @@ -63,13 +63,12 @@ class SwiftTests(test.TestCase): handled = table.maybe_handle() self.assertEqual(handled['location'], CONTAINER_INDEX_URL) - @test.create_stubs({api.swift: ('swift_delete_container',)}) + @test.create_stubs({api.swift: ('swift_get_objects', )}) def test_delete_container_nonempty(self): container = self.containers.first() - exc = self.exceptions.swift - exc.silence_logging = True - api.swift.swift_delete_container(IsA(http.HttpRequest), - container.name).AndRaise(exc) + objects = self.objects.list() + api.swift.swift_get_objects(IsA(http.HttpRequest), + container.name).AndReturn([objects, False]) self.mox.ReplayAll() action_string = u"containers__delete__%s" % container.name @@ -78,6 +77,9 @@ class SwiftTests(test.TestCase): table = ContainersTable(req, self.containers.list()) handled = table.maybe_handle() self.assertEqual(handled['location'], CONTAINER_INDEX_URL) + self.assertEqual(unicode(list(req._messages)[0].message), + u"The container cannot be deleted " + u"since it's not empty.") def test_create_container_get(self): res = self.client.get(reverse('horizon:project:containers:create'))