Support backup filtering

To get all the backups of an instance:

    openstack database backup list --instance-id <instance-id>

Admin user gets backups of all the projects:

    openstack database backup list --all-projects

Story: #2006433
Task: #36346
Change-Id: I871d1ffa27805158024e8ecbe1dceae725c4deac
This commit is contained in:
Lingxian Kong 2019-08-25 13:10:29 +12:00
parent 8e56d90af2
commit 982b3f035a
4 changed files with 88 additions and 15 deletions

View File

@ -61,19 +61,38 @@ class ListDatabaseBackups(command.Lister):
default=None,
help=_('ID or name of the datastore (to filter backups by).')
)
parser.add_argument(
'--instance-id',
default=None,
help=_('Filter backups by database instance ID.')
)
parser.add_argument(
'--all-projects',
action='store_true',
help=_('Get all the backups of all the projects(Admin only).')
)
return parser
def take_action(self, parsed_args):
database_backups = self.app.client_manager.database.backups
items = database_backups.list(limit=parsed_args.limit,
datastore=parsed_args.datastore,
marker=parsed_args.marker)
marker=parsed_args.marker,
instance_id=parsed_args.instance_id,
all_projects=parsed_args.all_projects)
backups = items
while items.next and not parsed_args.limit:
items = database_backups.list(marker=items.next)
items = database_backups.list(
marker=items.next,
datastore=parsed_args.datastore,
instance_id=parsed_args.instance_id,
all_projects=parsed_args.all_projects
)
backups += items
backups = [osc_utils.get_item_properties(b, self.columns)
for b in backups]
return self.columns, backups

View File

@ -32,12 +32,6 @@ class TestBackups(fakes.TestDatabasev1):
class TestBackupList(TestBackups):
defaults = {
'datastore': None,
'limit': None,
'marker': None
}
columns = database_backups.ListDatabaseBackups.columns
values = ('bk-1234', '1234', 'bkp_1', 'COMPLETED', None,
'2015-05-16T14:23:08')
@ -51,10 +45,48 @@ class TestBackupList(TestBackups):
def test_backup_list_defaults(self):
parsed_args = self.check_parser(self.cmd, [], [])
columns, data = self.cmd.take_action(parsed_args)
self.backup_client.list.assert_called_once_with(**self.defaults)
params = {
'datastore': None,
'limit': None,
'marker': None,
'instance_id': None,
'all_projects': False
}
self.backup_client.list.assert_called_once_with(**params)
self.assertEqual(self.columns, columns)
self.assertEqual([self.values], data)
def test_backup_list_by_instance_id(self):
parsed_args = self.check_parser(self.cmd, ["--instance-id", "fake_id"],
[])
self.cmd.take_action(parsed_args)
params = {
'datastore': None,
'limit': None,
'marker': None,
'instance_id': 'fake_id',
'all_projects': False
}
self.backup_client.list.assert_called_once_with(**params)
def test_backup_list_all_projects(self):
parsed_args = self.check_parser(self.cmd, ["--all-projects"], [])
self.cmd.take_action(parsed_args)
params = {
'datastore': None,
'limit': None,
'marker': None,
'instance_id': None,
'all_projects': True
}
self.backup_client.list.assert_called_once_with(**params)
class TestBackupListInstance(TestBackups):

View File

@ -114,6 +114,26 @@ class BackupManagerTest(testtools.TestCase):
page_mock.assert_called_with("/backups", "backups", limit, marker,
{'datastore': datastore})
def test_list_by_instance(self):
page_mock = mock.Mock()
self.backups._paginated = page_mock
instance_id = "fake_instance"
self.backups.list(instance_id=instance_id)
page_mock.assert_called_with("/backups", "backups", None, None,
{'instance_id': instance_id})
def test_list_by_all_projects(self):
page_mock = mock.Mock()
self.backups._paginated = page_mock
all_projects = True
self.backups.list(all_projects=all_projects)
page_mock.assert_called_with("/backups", "backups", None, None,
{'all_projects': all_projects})
def test_get(self):
get_mock = mock.Mock()
self.backups._get = get_mock

View File

@ -59,14 +59,16 @@ class Backups(base.ManagerWithFind):
return self._get("/backups/%s" % base.getid(backup),
"backup")
def list(self, limit=None, marker=None, datastore=None):
"""Get a list of all backups.
:rtype: list of :class:`Backups`.
"""
def list(self, limit=None, marker=None, datastore=None, instance_id=None,
all_projects=False):
"""Get a list of all backups."""
query_strings = {}
if datastore:
query_strings = {'datastore': datastore}
query_strings["datastore"] = datastore
if instance_id:
query_strings["instance_id"] = instance_id
if all_projects:
query_strings["all_projects"] = True
return self._paginated("/backups", "backups", limit, marker,
query_strings)