From fc24142ed41e15622687558c68d670bdc37223f0 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 18 Nov 2020 15:55:44 +0000 Subject: [PATCH] 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 --- openstackclient/compute/v2/keypair.py | 47 ++++++++++++- .../tests/unit/compute/v2/test_keypair.py | 68 +++++++++++++++++++ ...ng-keypair-list-opts-243a33d8276f91b8.yaml | 5 ++ 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/add-missing-keypair-list-opts-243a33d8276f91b8.yaml diff --git a/openstackclient/compute/v2/keypair.py b/openstackclient/compute/v2/keypair.py index 19e30bff10..7dabf78d9c 100644 --- a/openstackclient/compute/v2/keypair.py +++ b/openstackclient/compute/v2/keypair.py @@ -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", diff --git a/openstackclient/tests/unit/compute/v2/test_keypair.py b/openstackclient/tests/unit/compute/v2/test_keypair.py index 5a17808fa1..65d9396aee 100644 --- a/openstackclient/tests/unit/compute/v2/test_keypair.py +++ b/openstackclient/tests/unit/compute/v2/test_keypair.py @@ -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): diff --git a/releasenotes/notes/add-missing-keypair-list-opts-243a33d8276f91b8.yaml b/releasenotes/notes/add-missing-keypair-list-opts-243a33d8276f91b8.yaml new file mode 100644 index 0000000000..789eaa029e --- /dev/null +++ b/releasenotes/notes/add-missing-keypair-list-opts-243a33d8276f91b8.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``--limit`` and ``--marker`` options to ``keypair list`` command, to + configure pagination of results.