compute: Add missing options for 'keypair list'

Add pagination parameters, '--limit' and '--marker'. This isn't
compatible with our client-side '--project' parameter so we error out
for that.

Change-Id: I403cf0fb7aabad4a3dfda5adae62d47ecf7faf5c
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2020-11-18 15:55:44 +00:00
parent 8a0f3fc6a8
commit fc24142ed4
3 changed files with 117 additions and 3 deletions

View File

@ -249,12 +249,43 @@ class ListKeypair(command.Lister):
),
)
identity_common.add_project_domain_option_to_parser(parser)
parser.add_argument(
'--marker',
help=_('The last keypair ID of the previous page'),
)
parser.add_argument(
'--limit',
type=int,
help=_('Maximum number of keypairs to display'),
)
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.sdk_connection.compute
identity_client = self.app.client_manager.identity
kwargs = {}
if parsed_args.marker:
if not sdk_utils.supports_microversion(compute_client, '2.35'):
msg = _(
'--os-compute-api-version 2.35 or greater is required '
'to support the --marker option'
)
raise exceptions.CommandError(msg)
kwargs['marker'] = parsed_args.marker
if parsed_args.limit:
if not sdk_utils.supports_microversion(compute_client, '2.35'):
msg = _(
'--os-compute-api-version 2.35 or greater is required '
'to support the --limit option'
)
raise exceptions.CommandError(msg)
kwargs['limit'] = parsed_args.limit
if parsed_args.project:
if not sdk_utils.supports_microversion(compute_client, '2.10'):
msg = _(
@ -263,6 +294,14 @@ class ListKeypair(command.Lister):
)
raise exceptions.CommandError(msg)
if parsed_args.marker:
# NOTE(stephenfin): Because we're doing this client-side, we
# can't really rely on the marker, because we don't know what
# user the marker is associated with
msg = _(
'--project is not compatible with --marker'
)
# NOTE(stephenfin): This is done client side because nova doesn't
# currently support doing so server-side. If this is slow, we can
# think about spinning up a threadpool or similar.
@ -275,7 +314,8 @@ class ListKeypair(command.Lister):
data = []
for user in users:
data.extend(compute_client.keypairs(user_id=user.id))
kwargs['user_id'] = user.id
data.extend(compute_client.keypairs(**kwargs))
elif parsed_args.user:
if not sdk_utils.supports_microversion(compute_client, '2.10'):
msg = _(
@ -289,10 +329,11 @@ class ListKeypair(command.Lister):
parsed_args.user,
parsed_args.user_domain,
)
kwargs['user_id'] = user.id
data = compute_client.keypairs(user_id=user.id)
data = compute_client.keypairs(**kwargs)
else:
data = compute_client.keypairs()
data = compute_client.keypairs(**kwargs)
columns = (
"Name",

View File

@ -569,6 +569,74 @@ class TestKeypairList(TestKeypair):
tests_utils.ParserException,
self.check_parser, self.cmd, arglist, None)
@mock.patch.object(
sdk_utils, 'supports_microversion', new=mock.Mock(return_value=True))
def test_keypair_list_with_limit(self):
arglist = [
'--limit', '1',
]
verifylist = [
('limit', 1),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.sdk_client.keypairs.assert_called_with(limit=1)
@mock.patch.object(
sdk_utils, 'supports_microversion', new=mock.Mock(return_value=False))
def test_keypair_list_with_limit_pre_v235(self):
arglist = [
'--limit', '1',
]
verifylist = [
('limit', 1),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ex = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.35 or greater is required', str(ex))
@mock.patch.object(
sdk_utils, 'supports_microversion', new=mock.Mock(return_value=True))
def test_keypair_list_with_marker(self):
arglist = [
'--marker', 'test_kp',
]
verifylist = [
('marker', 'test_kp'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.sdk_client.keypairs.assert_called_with(marker='test_kp')
@mock.patch.object(
sdk_utils, 'supports_microversion', new=mock.Mock(return_value=False))
def test_keypair_list_with_marker_pre_v235(self):
arglist = [
'--marker', 'test_kp',
]
verifylist = [
('marker', 'test_kp'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
ex = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.35 or greater is required', str(ex))
class TestKeypairShow(TestKeypair):

View File

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