Fix 'project purge' deletes ALL images problem

Closes-Bug: #1717130
Change-Id: I33c6fc7897dfee85d1c197a1267bde4abfa5bbd9
This commit is contained in:
lihaijing 2017-09-21 14:55:17 +08:00 committed by Dean Troyer
parent 953d74b5d6
commit 254dbf3294
5 changed files with 28 additions and 13 deletions

View File

@ -95,7 +95,14 @@ class ProjectPurge(command.Command):
# images # images
try: try:
image_client = self.app.client_manager.image image_client = self.app.client_manager.image
data = image_client.images.list(owner=project_id) api_version = int(image_client.version)
if api_version == 1:
data = image_client.images.list(owner=project_id)
elif api_version == 2:
kwargs = {'filters': {'owner': project_id}}
data = image_client.images.list(**kwargs)
else:
raise NotImplementedError
self.delete_objects( self.delete_objects(
image_client.images.delete, data, 'image', dry_run) image_client.images.delete, data, 'image', dry_run)
except Exception: except Exception:

View File

@ -118,8 +118,8 @@ class TestProjectPurge(TestProjectPurgeInit):
self.projects_mock.delete.assert_called_once_with(self.project.id) self.projects_mock.delete.assert_called_once_with(self.project.id)
self.servers_mock.list.assert_called_once_with( self.servers_mock.list.assert_called_once_with(
search_opts={'tenant_id': self.project.id}) search_opts={'tenant_id': self.project.id})
self.images_mock.list.assert_called_once_with( kwargs = {'filters': {'owner': self.project.id}}
owner=self.project.id) self.images_mock.list.assert_called_once_with(**kwargs)
volume_search_opts = {'project_id': self.project.id} volume_search_opts = {'project_id': self.project.id}
self.volumes_mock.list.assert_called_once_with( self.volumes_mock.list.assert_called_once_with(
search_opts=volume_search_opts) search_opts=volume_search_opts)
@ -153,8 +153,8 @@ class TestProjectPurge(TestProjectPurgeInit):
self.projects_mock.delete.assert_not_called() self.projects_mock.delete.assert_not_called()
self.servers_mock.list.assert_called_once_with( self.servers_mock.list.assert_called_once_with(
search_opts={'tenant_id': self.project.id}) search_opts={'tenant_id': self.project.id})
self.images_mock.list.assert_called_once_with( kwargs = {'filters': {'owner': self.project.id}}
owner=self.project.id) self.images_mock.list.assert_called_once_with(**kwargs)
volume_search_opts = {'project_id': self.project.id} volume_search_opts = {'project_id': self.project.id}
self.volumes_mock.list.assert_called_once_with( self.volumes_mock.list.assert_called_once_with(
search_opts=volume_search_opts) search_opts=volume_search_opts)
@ -188,8 +188,8 @@ class TestProjectPurge(TestProjectPurgeInit):
self.projects_mock.delete.assert_not_called() self.projects_mock.delete.assert_not_called()
self.servers_mock.list.assert_called_once_with( self.servers_mock.list.assert_called_once_with(
search_opts={'tenant_id': self.project.id}) search_opts={'tenant_id': self.project.id})
self.images_mock.list.assert_called_once_with( kwargs = {'filters': {'owner': self.project.id}}
owner=self.project.id) self.images_mock.list.assert_called_once_with(**kwargs)
volume_search_opts = {'project_id': self.project.id} volume_search_opts = {'project_id': self.project.id}
self.volumes_mock.list.assert_called_once_with( self.volumes_mock.list.assert_called_once_with(
search_opts=volume_search_opts) search_opts=volume_search_opts)
@ -224,8 +224,8 @@ class TestProjectPurge(TestProjectPurgeInit):
self.projects_mock.delete.assert_called_once_with(self.project.id) self.projects_mock.delete.assert_called_once_with(self.project.id)
self.servers_mock.list.assert_called_once_with( self.servers_mock.list.assert_called_once_with(
search_opts={'tenant_id': self.project.id}) search_opts={'tenant_id': self.project.id})
self.images_mock.list.assert_called_once_with( kwargs = {'filters': {'owner': self.project.id}}
owner=self.project.id) self.images_mock.list.assert_called_once_with(**kwargs)
volume_search_opts = {'project_id': self.project.id} volume_search_opts = {'project_id': self.project.id}
self.volumes_mock.list.assert_called_once_with( self.volumes_mock.list.assert_called_once_with(
search_opts=volume_search_opts) search_opts=volume_search_opts)
@ -260,8 +260,8 @@ class TestProjectPurge(TestProjectPurgeInit):
self.projects_mock.delete.assert_called_once_with(self.project.id) self.projects_mock.delete.assert_called_once_with(self.project.id)
self.servers_mock.list.assert_called_once_with( self.servers_mock.list.assert_called_once_with(
search_opts={'tenant_id': self.project.id}) search_opts={'tenant_id': self.project.id})
self.images_mock.list.assert_called_once_with( kwargs = {'filters': {'owner': self.project.id}}
owner=self.project.id) self.images_mock.list.assert_called_once_with(**kwargs)
volume_search_opts = {'project_id': self.project.id} volume_search_opts = {'project_id': self.project.id}
self.volumes_mock.list.assert_called_once_with( self.volumes_mock.list.assert_called_once_with(
search_opts=volume_search_opts) search_opts=volume_search_opts)
@ -296,8 +296,8 @@ class TestProjectPurge(TestProjectPurgeInit):
self.projects_mock.delete.assert_called_once_with(self.project.id) self.projects_mock.delete.assert_called_once_with(self.project.id)
self.servers_mock.list.assert_called_once_with( self.servers_mock.list.assert_called_once_with(
search_opts={'tenant_id': self.project.id}) search_opts={'tenant_id': self.project.id})
self.images_mock.list.assert_called_once_with( kwargs = {'filters': {'owner': self.project.id}}
owner=self.project.id) self.images_mock.list.assert_called_once_with(**kwargs)
volume_search_opts = {'project_id': self.project.id} volume_search_opts = {'project_id': self.project.id}
self.volumes_mock.list.assert_called_once_with( self.volumes_mock.list.assert_called_once_with(
search_opts=volume_search_opts) search_opts=volume_search_opts)

View File

@ -64,6 +64,7 @@ class FakeImagev1Client(object):
self.images.resource_class = fakes.FakeResource(None, {}) self.images.resource_class = fakes.FakeResource(None, {})
self.auth_token = kwargs['token'] self.auth_token = kwargs['token']
self.management_url = kwargs['endpoint'] self.management_url = kwargs['endpoint']
self.version = 1.0
class TestImagev1(utils.TestCommand): class TestImagev1(utils.TestCommand):

View File

@ -156,6 +156,7 @@ class FakeImagev2Client(object):
self.image_tags.resource_class = fakes.FakeResource(None, {}) self.image_tags.resource_class = fakes.FakeResource(None, {})
self.auth_token = kwargs['token'] self.auth_token = kwargs['token']
self.management_url = kwargs['endpoint'] self.management_url = kwargs['endpoint']
self.version = 2.0
class TestImagev2(utils.TestCommand): class TestImagev2(utils.TestCommand):

View File

@ -0,0 +1,6 @@
---
fixes:
- |
Fix the ``project purge`` command to correctly delete only images owned by the
specified project ID when run by an administrative user.
[Bug `1717130 <https://bugs.launchpad.net/python-openstackclient/+bug/1717130>`_]