From 9ee74d3ac6d26018d0161e24fe8c0f3f29b21c06 Mon Sep 17 00:00:00 2001 From: Victor Coutellier Date: Wed, 11 Mar 2020 12:50:13 +0100 Subject: [PATCH] Microversion 2.83 - Add more filters for the nova list command Add these new filters, admin-only until microversion 2.82: - availability-zone - config-drive - key-name - power-state - task-state - vm-state - progress Existing user filter will be available to non admin since microversion 2.83. Part of blueprint non-admin-filter-instance-by-az Change-Id: Id2b5e600c0a41790830823031b20983808cb5ace --- doc/source/cli/nova.rst | 37 +++++++++- novaclient/__init__.py | 2 +- novaclient/tests/unit/v2/test_servers.py | 7 ++ novaclient/tests/unit/v2/test_shell.py | 50 ++++++++++++- novaclient/v2/servers.py | 6 ++ novaclient/v2/shell.py | 71 ++++++++++++++++++- ...-filter-to-nova-list-831dcbb34420fb29.yaml | 18 +++++ 7 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 releasenotes/notes/add-filter-to-nova-list-831dcbb34420fb29.yaml diff --git a/doc/source/cli/nova.rst b/doc/source/cli/nova.rst index 84c441bd7..48b1dbda2 100644 --- a/doc/source/cli/nova.rst +++ b/doc/source/cli/nova.rst @@ -2223,7 +2223,11 @@ nova list [--tenant []] [--user []] [--deleted] [--fields ] [--minimal] [--sort [:]] [--marker ] - [--limit ] [--changes-since ] + [--limit ] [--availability-zone ] + [--key-name ] [--config-drive ] + [--progress ] [--vm-state ] + [--task-state ] [--power-state ] + [--changes-since ] [--changes-before ] [--tags ] [--tags-any ] [--not-tags ] [--not-tags-any ] @@ -2274,7 +2278,7 @@ present in the failure domain. ``--user []`` Display information from single user (Admin - only). + only until microversion 2.82). ``--deleted`` Only display deleted servers (Admin only). @@ -2304,6 +2308,35 @@ present in the failure domain. Nova API, limit 'CONF.api.max_limit' will be used instead. +``--availability-zone `` + Display servers based on their availability zone + (Admin only until microversion 2.82). + +``--key-name `` + Display servers based on their keypair name + (Admin only until microversion 2.82). + +``--config-drive `` + Display servers based on their config_drive value + The value must be a boolean. (Admin only until + microversion 2.82). + +``--progress `` + Display servers based on their progress value + (Admin only until microversion 2.82). + +``--vm-state `` + Display servers based on their vm_state value + (Admin only until microversion 2.82). + +``--task-state `` + Display servers based on their task_state value + (Admin only until microversion 2.82). + +``--power-state `` + Display servers based on their power_state value + (Admin only until microversion 2.82). + ``--changes-since `` List only servers changed later or equal to a certain point of time. The provided time should diff --git a/novaclient/__init__.py b/novaclient/__init__.py index 773a4b709..f86d680fb 100644 --- a/novaclient/__init__.py +++ b/novaclient/__init__.py @@ -25,4 +25,4 @@ API_MIN_VERSION = api_versions.APIVersion("2.1") # when client supported the max version, and bumped sequentially, otherwise # the client may break due to server side new version may include some # backward incompatible change. -API_MAX_VERSION = api_versions.APIVersion("2.82") +API_MAX_VERSION = api_versions.APIVersion("2.83") diff --git a/novaclient/tests/unit/v2/test_servers.py b/novaclient/tests/unit/v2/test_servers.py index d65ec145c..0d3ca4b2c 100644 --- a/novaclient/tests/unit/v2/test_servers.py +++ b/novaclient/tests/unit/v2/test_servers.py @@ -85,6 +85,13 @@ class ServersTest(utils.FixturedTestCase): self.assertIn("'locked' argument is only allowed since " "microversion 2.73.", str(e)) + def test_filter_without_config_drive(self): + sl = self.cs.servers.list(search_opts={'config_drive': None}) + self.assert_request_id(sl, fakes.FAKE_REQUEST_ID_LIST) + self.assert_called('GET', '/servers/detail') + for s in sl: + self.assertIsInstance(s, servers.Server) + def test_list_servers_undetailed(self): sl = self.cs.servers.list(detailed=False) self.assert_request_id(sl, fakes.FAKE_REQUEST_ID_LIST) diff --git a/novaclient/tests/unit/v2/test_shell.py b/novaclient/tests/unit/v2/test_shell.py index dcd487076..c8d3dc9be 100644 --- a/novaclient/tests/unit/v2/test_shell.py +++ b/novaclient/tests/unit/v2/test_shell.py @@ -1704,7 +1704,7 @@ class ShellTest(utils.TestCase): self.run_command('list --user fake_user') self.assert_called( 'GET', - '/servers/detail?all_tenants=1&user_id=fake_user') + '/servers/detail?user_id=fake_user') def test_list_with_single_sort_key_no_dir(self): self.run_command('list --sort 1') @@ -1838,6 +1838,51 @@ class ShellTest(utils.TestCase): 'list --changes-before 2016-02-29T06:23:22', api_version='2.65') + def test_list_with_availability_zone(self): + self.run_command('list --availability-zone nova') + self.assert_called('GET', '/servers/detail?availability_zone=nova') + + def test_list_with_key_name(self): + self.run_command('list --key-name my_key') + self.assert_called('GET', '/servers/detail?key_name=my_key') + + def test_list_with_config_drive_passing_through_any_value(self): + self.run_command('list --config-drive True') + self.assert_called('GET', '/servers/detail?config_drive=True') + self.run_command('list --config-drive some-random-string') + self.assert_called( + 'GET', '/servers/detail?config_drive=some-random-string') + # This form is special for the test env to pass through an empty string + # as a parameter. The real CLI call would look like + # list --config drive '' + self.run_command(['list', '--config-drive', '']) + self.assert_called( + 'GET', '/servers/detail?config_drive=') + + def test_list_with_progress(self): + self.run_command('list --progress 100') + self.assert_called('GET', '/servers/detail?progress=100') + + def test_list_with_0_progress(self): + self.run_command('list --progress 0') + self.assert_called('GET', '/servers/detail?progress=0') + + def test_list_with_vm_state(self): + self.run_command('list --vm-state active') + self.assert_called('GET', '/servers/detail?vm_state=active') + + def test_list_with_task_state(self): + self.run_command('list --task-state reboot_started') + self.assert_called('GET', '/servers/detail?task_state=reboot_started') + + def test_list_with_power_state(self): + self.run_command('list --power-state 1') + self.assert_called('GET', '/servers/detail?power_state=1') + + def test_list_with_power_state_filter_for_0_state(self): + self.run_command('list --power-state 0') + self.assert_called('GET', '/servers/detail?power_state=0') + def test_list_fields_redundant(self): output, _err = self.run_command('list --fields id,status,status') header = output.splitlines()[1] @@ -4458,7 +4503,8 @@ class ShellTest(utils.TestCase): 75, # There are no version-wrapped shell method changes for this. 76, # doesn't require any changes in novaclient. 77, # There are no version-wrapped shell method changes for this. - 82, # doesn't require any changes in novaclient. + 82, # There are no version-wrapped shell method changes for this. + 83, # There are no version-wrapped shell method changes for this. ]) versions_supported = set(range(0, novaclient.API_MAX_VERSION.ver_minor + 1)) diff --git a/novaclient/v2/servers.py b/novaclient/v2/servers.py index ff8c166ec..42184345d 100644 --- a/novaclient/v2/servers.py +++ b/novaclient/v2/servers.py @@ -893,6 +893,12 @@ class ServerManager(base.BootingManagerWithFind): if isinstance(val, str): val = val.encode('utf-8') qparams[opt] = val + # NOTE(gibi): After we fixing bug 1871409 and cleaning up the API + # inconsistency around config_drive we can make the + # config_drive filter a boolean filter. But until that we + # simply pass through any value to the API even empty string. + if opt == 'config_drive' and val is not None: + qparams[opt] = val detail = "" if detailed: diff --git a/novaclient/v2/shell.py b/novaclient/v2/shell.py index e86add103..8dafc164d 100644 --- a/novaclient/v2/shell.py +++ b/novaclient/v2/shell.py @@ -1486,7 +1486,8 @@ def _print_flavor(flavor): dest='user', metavar='', nargs='?', - help=_('Display information from single user (Admin only).')) + help=_('Display information from single user (Admin only until ' + 'microversion 2.82).')) @utils.arg( '--deleted', dest='deleted', @@ -1529,6 +1530,61 @@ def _print_flavor(flavor): "will be displayed. If limit is bigger than 'CONF.api.max_limit' " "option of Nova API, limit 'CONF.api.max_limit' will be used " "instead.")) +@utils.arg( + '--availability-zone', + dest='availability_zone', + metavar='', + default=None, + help=_('Display servers based on their availability zone (Admin only ' + 'until microversion 2.82).')) +@utils.arg( + '--key-name', + dest='key_name', + metavar='', + default=None, + help=_('Display servers based on their keypair name (Admin only until ' + 'microversion 2.82).')) +# NOTE(gibi): we can make this a real boolean filter after bug 1871409 is fixed +# and the REST API is cleaned up regarding the values of config_drive. Unit +# that we simply pass through any string from the user to the REST API. +@utils.arg( + '--config-drive', + dest='config_drive', + metavar='', + default=None, + help=_('Display servers based on their config_drive value (Admin only ' + 'until microversion 2.82). The value must be a boolean value.')) +@utils.arg( + '--progress', + dest='progress', + metavar='', + default=None, + help=_('Display servers based on their progress value (Admin only until ' + 'microversion 2.82).')) +@utils.arg( + '--vm-state', + dest='vm_state', + metavar='', + default=None, + help=_('Display servers based on their vm_state value (Admin only until ' + 'microversion 2.82).')) +@utils.arg( + '--task-state', + dest='task_state', + metavar='', + default=None, + help=_('Display servers based on their task_state value (Admin only until ' + 'microversion 2.82).')) +# TODO(gibi): this is now only work with the integer power state values. +# Later on we can extend this to accept the string values of the power state +# and translate it to integers towards the REST API. +@utils.arg( + '--power-state', + dest='power_state', + metavar='', + default=None, + help=_('Display servers based on their power_state value (Admin only ' + 'until microversion 2.82).')) @utils.arg( '--changes-since', dest='changes_since', @@ -1603,8 +1659,9 @@ def do_list(cs, args): if args.flavor: flavorid = _find_flavor(cs, args.flavor).id # search by tenant or user only works with all_tenants - if args.tenant or args.user: + if args.tenant: args.all_tenants = 1 + search_opts = { 'all_tenants': args.all_tenants, 'reservation_id': args.reservation_id, @@ -1618,7 +1675,15 @@ def do_list(cs, args): 'user_id': args.user, 'host': args.host, 'deleted': args.deleted, - 'changes-since': args.changes_since} + 'changes-since': args.changes_since, + 'availability_zone': args.availability_zone, + 'config_drive': args.config_drive, + 'key_name': args.key_name, + 'progress': args.progress, + 'vm_state': args.vm_state, + 'task_state': args.task_state, + 'power_state': args.power_state, + } for arg in ('tags', "tags-any", 'not-tags', 'not-tags-any'): if arg in args: diff --git a/releasenotes/notes/add-filter-to-nova-list-831dcbb34420fb29.yaml b/releasenotes/notes/add-filter-to-nova-list-831dcbb34420fb29.yaml new file mode 100644 index 000000000..e91a6080e --- /dev/null +++ b/releasenotes/notes/add-filter-to-nova-list-831dcbb34420fb29.yaml @@ -0,0 +1,18 @@ +--- + features: + - | + Added the following filters support for the ``nova list`` command, + these filters are admin-only restricted until microversion 2.82: + + * --availability-zone + * --config-drive + * --key-name + * --power-state + * --task-state + * --vm-state + * --progress + + Existing user filter will be available to non admin since + `microversion 2.83`_. + + .. _microversion 2.83: https://docs.openstack.org/nova/latest/reference/api-microversion-history.html#id76