Skip to content

Commit 5ec4d4c

Browse files
committed
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>
1 parent 1a6df70 commit 5ec4d4c

3 files changed

Lines changed: 85 additions & 5 deletions

File tree

openstackclient/compute/v2/server_group.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,47 @@ def get_parser(self, prog_name):
177177
default=False,
178178
help=_("List additional fields in output")
179179
)
180+
# TODO(stephenfin): This should really be a --marker option, but alas
181+
# the API doesn't support that for some reason
182+
parser.add_argument(
183+
'--offset',
184+
metavar='<offset>',
185+
type=int,
186+
default=None,
187+
help=_(
188+
'Index from which to start listing servers. This should '
189+
'typically be a factor of --limit. Display all servers groups '
190+
'if not specified.'
191+
),
192+
)
193+
parser.add_argument(
194+
'--limit',
195+
metavar='<limit>',
196+
type=int,
197+
default=None,
198+
help=_(
199+
"Maximum number of server groups to display. "
200+
"If limit is greater than 'osapi_max_limit' option of Nova "
201+
"API, 'osapi_max_limit' will be used instead."
202+
),
203+
)
180204
return parser
181205

182206
def take_action(self, parsed_args):
183207
compute_client = self.app.client_manager.compute
184-
data = compute_client.server_groups.list(parsed_args.all_projects)
208+
209+
kwargs = {}
210+
211+
if parsed_args.all_projects:
212+
kwargs['all_projects'] = parsed_args.all_projects
213+
214+
if parsed_args.offset:
215+
kwargs['offset'] = parsed_args.offset
216+
217+
if parsed_args.limit:
218+
kwargs['limit'] = parsed_args.limit
219+
220+
data = compute_client.server_groups.list(**kwargs)
185221

186222
policy_key = 'Policies'
187223
if compute_client.api_version >= api_versions.APIVersion("2.64"):

openstackclient/tests/unit/compute/v2/test_server_group.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,13 @@ def test_server_group_list(self):
326326
verifylist = [
327327
('all_projects', False),
328328
('long', False),
329+
('limit', None),
330+
('offset', None),
329331
]
330332
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
331333
columns, data = self.cmd.take_action(parsed_args)
332-
self.server_groups_mock.list.assert_called_once_with(False)
334+
335+
self.server_groups_mock.list.assert_called_once_with()
333336

334337
self.assertCountEqual(self.list_columns, columns)
335338
self.assertCountEqual(self.list_data, tuple(data))
@@ -342,14 +345,49 @@ def test_server_group_list_with_all_projects_and_long(self):
342345
verifylist = [
343346
('all_projects', True),
344347
('long', True),
348+
('limit', None),
349+
('offset', None),
345350
]
346351
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
347352
columns, data = self.cmd.take_action(parsed_args)
348-
self.server_groups_mock.list.assert_called_once_with(True)
353+
self.server_groups_mock.list.assert_called_once_with(
354+
all_projects=True)
349355

350356
self.assertCountEqual(self.list_columns_long, columns)
351357
self.assertCountEqual(self.list_data_long, tuple(data))
352358

359+
def test_server_group_list_with_limit(self):
360+
arglist = [
361+
'--limit', '1',
362+
]
363+
verifylist = [
364+
('all_projects', False),
365+
('long', False),
366+
('limit', 1),
367+
('offset', None),
368+
]
369+
370+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
371+
self.cmd.take_action(parsed_args)
372+
373+
self.server_groups_mock.list.assert_called_once_with(limit=1)
374+
375+
def test_server_group_list_with_offset(self):
376+
arglist = [
377+
'--offset', '5',
378+
]
379+
verifylist = [
380+
('all_projects', False),
381+
('long', False),
382+
('limit', None),
383+
('offset', 5),
384+
]
385+
386+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
387+
self.cmd.take_action(parsed_args)
388+
389+
self.server_groups_mock.list.assert_called_once_with(offset=5)
390+
353391

354392
class TestServerGroupListV264(TestServerGroupV264):
355393

@@ -400,7 +438,7 @@ def test_server_group_list(self):
400438
]
401439
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
402440
columns, data = self.cmd.take_action(parsed_args)
403-
self.server_groups_mock.list.assert_called_once_with(False)
441+
self.server_groups_mock.list.assert_called_once_with()
404442

405443
self.assertCountEqual(self.list_columns, columns)
406444
self.assertCountEqual(self.list_data, tuple(data))
@@ -416,7 +454,8 @@ def test_server_group_list_with_all_projects_and_long(self):
416454
]
417455
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
418456
columns, data = self.cmd.take_action(parsed_args)
419-
self.server_groups_mock.list.assert_called_once_with(True)
457+
self.server_groups_mock.list.assert_called_once_with(
458+
all_projects=True)
420459

421460
self.assertCountEqual(self.list_columns_long, columns)
422461
self.assertCountEqual(self.list_data_long, tuple(data))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add ``--limit`` and ``--offset`` options to ``server group list`` command,
5+
to configure pagination of results.

0 commit comments

Comments
 (0)