From 5a1513244caf7acbd41e181419bc8b62bf4bcaba Mon Sep 17 00:00:00 2001 From: TommyLike Date: Wed, 14 Mar 2018 15:18:55 +0800 Subject: [PATCH] Support availability-zone in volume type Since 3.52, new option '--filters' has been added to 'type-list' command, and it's only valid for administrator. Change-Id: I140f6d61a2747d4fcaabfbccea864dcc7eb841d1 Depends-On: I4e6aa7af707bd063e7edf2b0bf28e3071ad5c67a Partial-Implements: bp support-az-in-volumetype --- cinderclient/api_versions.py | 2 +- cinderclient/tests/unit/v3/test_shell.py | 14 +++++++++++++ cinderclient/v3/shell.py | 21 +++++++++++++++++++ cinderclient/v3/volume_types.py | 12 +++++++---- doc/source/cli/details.rst | 9 +++++++- .../support-filter-type-7yt69ub7ccbf7419.yaml | 5 +++++ 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/support-filter-type-7yt69ub7ccbf7419.yaml diff --git a/cinderclient/api_versions.py b/cinderclient/api_versions.py index 1dad7945d..0fcb20839 100644 --- a/cinderclient/api_versions.py +++ b/cinderclient/api_versions.py @@ -29,7 +29,7 @@ LOG = logging.getLogger(__name__) # key is a deprecated version and value is an alternative version. DEPRECATED_VERSIONS = {"1": "2"} DEPRECATED_VERSION = "2.0" -MAX_VERSION = "3.50" +MAX_VERSION = "3.52" MIN_VERSION = "3.0" _SUBSTITUTIONS = {} diff --git a/cinderclient/tests/unit/v3/test_shell.py b/cinderclient/tests/unit/v3/test_shell.py index 84ca4cbb0..cff2145ce 100644 --- a/cinderclient/tests/unit/v3/test_shell.py +++ b/cinderclient/tests/unit/v3/test_shell.py @@ -244,6 +244,20 @@ class ShellTest(utils.TestCase): self.run_command, 'list --group_id fake_id') + def test_type_list_with_filters_invalid(self): + self.assertRaises(exceptions.UnsupportedAttribute, + self.run_command, + '--os-volume-api-version 3.51 type-list ' + '--filters key=value') + + def test_type_list_with_filters(self): + self.run_command('--os-volume-api-version 3.52 type-list ' + '--filters extra_specs={key:value}') + self.assert_called( + 'GET', '/types?%s' % parse.urlencode( + {'extra_specs': + {six.text_type('key'): six.text_type('value')}})) + @ddt.data("3.10", "3.11") def test_list_with_group_id_after_3_10(self, version): command = ('--os-volume-api-version %s list --group_id fake_id' % diff --git a/cinderclient/v3/shell.py b/cinderclient/v3/shell.py index f012cc8d3..4149f612d 100644 --- a/cinderclient/v3/shell.py +++ b/cinderclient/v3/shell.py @@ -52,6 +52,27 @@ def do_list_filters(cs, args): shell_utils.print_resource_filter_list(filters) +@utils.arg('--filters', + type=six.text_type, + nargs='*', + start_version='3.52', + metavar='', + default=None, + help="Filter key and value pairs. Admin only.") +def do_type_list(cs, args): + """Lists available 'volume types'. + + (Only admin and tenant users will see private types) + """ + # pylint: disable=function-redefined + search_opts = {} + # Update search option with `filters` + if hasattr(args, 'filters') and args.filters is not None: + search_opts.update(shell_utils.extract_filters(args.filters)) + vtypes = cs.volume_types.list(search_opts=search_opts) + shell_utils.print_volume_type_list(vtypes) + + @utils.arg('--all-tenants', metavar='', nargs='?', diff --git a/cinderclient/v3/volume_types.py b/cinderclient/v3/volume_types.py index 7f26d69f4..5030b5e94 100644 --- a/cinderclient/v3/volume_types.py +++ b/cinderclient/v3/volume_types.py @@ -15,6 +15,7 @@ """Volume Type interface.""" +from six.moves.urllib import parse from cinderclient.apiclient import base as common_base from cinderclient import base @@ -86,10 +87,13 @@ class VolumeTypeManager(base.ManagerWithFind): :rtype: list of :class:`VolumeType`. """ - query_string = '' - if not is_public: - query_string = '?is_public=%s' % is_public - return self._list("/types%s" % (query_string), "volume_types") + if not search_opts: + search_opts = dict() + if is_public: + search_opts.update({"is_public": is_public}) + query_string = "?%s" % parse.urlencode( + search_opts) if search_opts else '' + return self._list("/types%s" % query_string, "volume_types") def get(self, volume_type): """Get a specific volume type. diff --git a/doc/source/cli/details.rst b/doc/source/cli/details.rst index 351ee9bf8..87e18f51a 100644 --- a/doc/source/cli/details.rst +++ b/doc/source/cli/details.rst @@ -4341,11 +4341,18 @@ cinder type-list .. code-block:: console - usage: cinder type-list + usage: cinder type-list [--filters [ ...]] Lists available 'volume types'. (Only admin and tenant users will see private types) +**Optional arguments:** + +``--filters [ [ ...]]`` + Filter key and value pairs. Please use 'cinder list-filters' + to check enabled filters from server, Default=None. + (Supported by API version 3.52 and later) + .. _cinder_type-show: cinder type-show diff --git a/releasenotes/notes/support-filter-type-7yt69ub7ccbf7419.yaml b/releasenotes/notes/support-filter-type-7yt69ub7ccbf7419.yaml new file mode 100644 index 000000000..9036c13c8 --- /dev/null +++ b/releasenotes/notes/support-filter-type-7yt69ub7ccbf7419.yaml @@ -0,0 +1,5 @@ +--- +features: + - New command option ``--filters`` is added to ``type-list`` + command to support filter types since 3.52, and it's only + valid for administrator.