diff --git a/openstackclient/compute/v2/hypervisor.py b/openstackclient/compute/v2/hypervisor.py index 8fdb66983c..5f7497b5b1 100644 --- a/openstackclient/compute/v2/hypervisor.py +++ b/openstackclient/compute/v2/hypervisor.py @@ -22,6 +22,7 @@ from novaclient import api_versions from novaclient import exceptions as nova_exceptions from osc_lib.cli import format_columns from osc_lib.command import command +from osc_lib import exceptions from osc_lib import utils from openstackclient.i18n import _ @@ -33,10 +34,31 @@ class ListHypervisor(command.Lister): def get_parser(self, prog_name): parser = super(ListHypervisor, self).get_parser(prog_name) parser.add_argument( - "--matching", - metavar="", + '--matching', + metavar='', help=_("Filter hypervisors using substring") ) + parser.add_argument( + '--marker', + metavar='', + help=_( + "The UUID of the last hypervisor of the previous page; " + "displays list of hypervisors after 'marker'. " + "(supported with --os-compute-api-version 2.33 or above)" + ), + ) + parser.add_argument( + '--limit', + metavar='', + type=int, + help=_( + "Maximum number of hypervisors to display. Note that there " + "is a configurable max limit on the server, and the limit " + "that is used will be the minimum of what is requested " + "here and what is configured in the server. " + "(supported with --os-compute-api-version 2.33 or above)" + ), + ) parser.add_argument( '--long', action='store_true', @@ -46,6 +68,33 @@ class ListHypervisor(command.Lister): def take_action(self, parsed_args): compute_client = self.app.client_manager.compute + + list_opts = {} + + if parsed_args.matching and (parsed_args.marker or parsed_args.limit): + msg = _( + '--matching is not compatible with --marker or --limit' + ) + raise exceptions.CommandError(msg) + + if parsed_args.marker: + if compute_client.api_version < api_versions.APIVersion('2.33'): + msg = _( + '--os-compute-api-version 2.33 or greater is required to ' + 'support the --marker option' + ) + raise exceptions.CommandError(msg) + list_opts['marker'] = parsed_args.marker + + if parsed_args.limit: + if compute_client.api_version < api_versions.APIVersion('2.33'): + msg = _( + '--os-compute-api-version 2.33 or greater is required to ' + 'support the --limit option' + ) + raise exceptions.CommandError(msg) + list_opts['limit'] = parsed_args.limit + columns = ( "ID", "Hypervisor Hostname", @@ -59,12 +108,12 @@ class ListHypervisor(command.Lister): if parsed_args.matching: data = compute_client.hypervisors.search(parsed_args.matching) else: - data = compute_client.hypervisors.list() + data = compute_client.hypervisors.list(**list_opts) - return (columns, - (utils.get_item_properties( - s, columns, - ) for s in data)) + return ( + columns, + (utils.get_item_properties(s, columns) for s in data), + ) class ShowHypervisor(command.ShowOne): diff --git a/openstackclient/tests/unit/compute/v2/test_hypervisor.py b/openstackclient/tests/unit/compute/v2/test_hypervisor.py index 518109c6b0..3220a76450 100644 --- a/openstackclient/tests/unit/compute/v2/test_hypervisor.py +++ b/openstackclient/tests/unit/compute/v2/test_hypervisor.py @@ -173,6 +173,30 @@ class TestHypervisorList(TestHypervisor): self.cmd.take_action, parsed_args) + def test_hypervisor_list_with_matching_and_pagination_options(self): + self.app.client_manager.compute.api_version = \ + api_versions.APIVersion('2.32') + + arglist = [ + '--matching', self.hypervisors[0].hypervisor_hostname, + '--limit', '1', + '--marker', self.hypervisors[0].hypervisor_hostname, + ] + verifylist = [ + ('matching', self.hypervisors[0].hypervisor_hostname), + ('limit', 1), + ('marker', self.hypervisors[0].hypervisor_hostname), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + ex = self.assertRaises( + exceptions.CommandError, + self.cmd.take_action, + parsed_args) + + self.assertIn( + '--matching is not compatible with --marker or --limit', str(ex)) + def test_hypervisor_list_long_option(self): arglist = [ '--long', @@ -191,6 +215,78 @@ class TestHypervisorList(TestHypervisor): self.assertEqual(self.columns_long, columns) self.assertEqual(self.data_long, tuple(data)) + def test_hypervisor_list_with_limit(self): + self.app.client_manager.compute.api_version = \ + api_versions.APIVersion('2.33') + + arglist = [ + '--limit', '1', + ] + verifylist = [ + ('limit', 1), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + + self.hypervisors_mock.list.assert_called_with(limit=1) + + def test_hypervisor_list_with_limit_pre_v233(self): + self.app.client_manager.compute.api_version = \ + api_versions.APIVersion('2.32') + + 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.33 or greater is required', str(ex)) + + def test_hypervisor_list_with_marker(self): + self.app.client_manager.compute.api_version = \ + api_versions.APIVersion('2.33') + + arglist = [ + '--marker', 'test_hyp', + ] + verifylist = [ + ('marker', 'test_hyp'), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + + self.hypervisors_mock.list.assert_called_with(marker='test_hyp') + + def test_hypervisor_list_with_marker_pre_v233(self): + self.app.client_manager.compute.api_version = \ + api_versions.APIVersion('2.32') + + arglist = [ + '--marker', 'test_hyp', + ] + verifylist = [ + ('marker', 'test_hyp'), + ] + + 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.33 or greater is required', str(ex)) + class TestHypervisorShow(TestHypervisor): diff --git a/releasenotes/notes/add-missing-hypervisor-list-opts-71da2cc36eac4edd.yaml b/releasenotes/notes/add-missing-hypervisor-list-opts-71da2cc36eac4edd.yaml new file mode 100644 index 0000000000..c5401210a4 --- /dev/null +++ b/releasenotes/notes/add-missing-hypervisor-list-opts-71da2cc36eac4edd.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``--limit`` and ``--marker`` options to ``hypervisor list`` command, to + configure pagination of results.