compute: Add missing options for 'server group list'

Add pagination parameters, '--limit' and '--offset'. It's unfortunate
that we can't use '--marker' like elsewhere but that requires
server-side support to be truly effective.

Change-Id: I186adc8cdf28e9c540ad22bca6684d9dd892976a
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2020-12-10 11:29:01 +00:00
parent 1a6df700be
commit 5ec4d4c718
3 changed files with 85 additions and 5 deletions

View File

@ -177,11 +177,47 @@ class ListServerGroup(command.Lister):
default=False,
help=_("List additional fields in output")
)
# TODO(stephenfin): This should really be a --marker option, but alas
# the API doesn't support that for some reason
parser.add_argument(
'--offset',
metavar='<offset>',
type=int,
default=None,
help=_(
'Index from which to start listing servers. This should '
'typically be a factor of --limit. Display all servers groups '
'if not specified.'
),
)
parser.add_argument(
'--limit',
metavar='<limit>',
type=int,
default=None,
help=_(
"Maximum number of server groups to display. "
"If limit is greater than 'osapi_max_limit' option of Nova "
"API, 'osapi_max_limit' will be used instead."
),
)
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute
data = compute_client.server_groups.list(parsed_args.all_projects)
kwargs = {}
if parsed_args.all_projects:
kwargs['all_projects'] = parsed_args.all_projects
if parsed_args.offset:
kwargs['offset'] = parsed_args.offset
if parsed_args.limit:
kwargs['limit'] = parsed_args.limit
data = compute_client.server_groups.list(**kwargs)
policy_key = 'Policies'
if compute_client.api_version >= api_versions.APIVersion("2.64"):

View File

@ -326,10 +326,13 @@ class TestServerGroupList(TestServerGroup):
verifylist = [
('all_projects', False),
('long', False),
('limit', None),
('offset', None),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.server_groups_mock.list.assert_called_once_with(False)
self.server_groups_mock.list.assert_called_once_with()
self.assertCountEqual(self.list_columns, columns)
self.assertCountEqual(self.list_data, tuple(data))
@ -342,14 +345,49 @@ class TestServerGroupList(TestServerGroup):
verifylist = [
('all_projects', True),
('long', True),
('limit', None),
('offset', None),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.server_groups_mock.list.assert_called_once_with(True)
self.server_groups_mock.list.assert_called_once_with(
all_projects=True)
self.assertCountEqual(self.list_columns_long, columns)
self.assertCountEqual(self.list_data_long, tuple(data))
def test_server_group_list_with_limit(self):
arglist = [
'--limit', '1',
]
verifylist = [
('all_projects', False),
('long', False),
('limit', 1),
('offset', None),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.server_groups_mock.list.assert_called_once_with(limit=1)
def test_server_group_list_with_offset(self):
arglist = [
'--offset', '5',
]
verifylist = [
('all_projects', False),
('long', False),
('limit', None),
('offset', 5),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.server_groups_mock.list.assert_called_once_with(offset=5)
class TestServerGroupListV264(TestServerGroupV264):
@ -400,7 +438,7 @@ class TestServerGroupListV264(TestServerGroupV264):
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.server_groups_mock.list.assert_called_once_with(False)
self.server_groups_mock.list.assert_called_once_with()
self.assertCountEqual(self.list_columns, columns)
self.assertCountEqual(self.list_data, tuple(data))
@ -416,7 +454,8 @@ class TestServerGroupListV264(TestServerGroupV264):
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
self.server_groups_mock.list.assert_called_once_with(True)
self.server_groups_mock.list.assert_called_once_with(
all_projects=True)
self.assertCountEqual(self.list_columns_long, columns)
self.assertCountEqual(self.list_data_long, tuple(data))

View File

@ -0,0 +1,5 @@
---
features:
- |
Add ``--limit`` and ``--offset`` options to ``server group list`` command,
to configure pagination of results.