From 8b1ed34ec1b01ad77f1008a464302d94fa14c7b6 Mon Sep 17 00:00:00 2001 From: Sean McGinnis Date: Thu, 18 Apr 2019 16:09:26 -0500 Subject: [PATCH] Add transfer-list --sort argument Change Idb04f783b2287b2b45d626131648b0005a232fbe to the cinder service introduced pagination and the ability to sort results for listing volume transfers. This adds the sort ability to the cinder client. The service side uses the sort_key and sort_dir that we've deprecated long ago, but was unfortunately missed when merging this support. Since we have been giving deprecation warnings for using those for other operations, this adds support for --sort that internally will convert to the API's expected sort_key and sort_dir. Change-Id: I137436c76852cbb974eee87e49712c698cbf081b Signed-off-by: Sean McGinnis --- cinderclient/api_versions.py | 2 +- cinderclient/tests/unit/v3/test_shell.py | 21 ++++++++ cinderclient/v3/shell.py | 50 +++++++++++++++++++ cinderclient/v3/volume_transfers.py | 22 ++++---- .../notes/transfer-sort-ca622e9b8da605c1.yaml | 8 +++ 5 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 releasenotes/notes/transfer-sort-ca622e9b8da605c1.yaml diff --git a/cinderclient/api_versions.py b/cinderclient/api_versions.py index 474bdfbbc..8652f0e35 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.58" +MAX_VERSION = "3.59" MIN_VERSION = "3.0" _SUBSTITUTIONS = {} diff --git a/cinderclient/tests/unit/v3/test_shell.py b/cinderclient/tests/unit/v3/test_shell.py index b145d8e5e..f56872dbe 100644 --- a/cinderclient/tests/unit/v3/test_shell.py +++ b/cinderclient/tests/unit/v3/test_shell.py @@ -1351,3 +1351,24 @@ class ShellTest(utils.TestCase): 'no_snapshots': True }} self.assert_called('POST', '/volume-transfers', body=expected) + + def test_list_transfer_sort_key(self): + self.run_command( + '--os-volume-api-version 3.59 transfer-list --sort=id') + url = ('/volume-transfers/detail?%s' % + parse.urlencode([('sort_key', 'id')])) + self.assert_called('GET', url) + + def test_list_transfer_sort_key_dir(self): + self.run_command( + '--os-volume-api-version 3.59 transfer-list --sort=id:asc') + url = ('/volume-transfers/detail?%s' % + parse.urlencode([('sort_dir', 'asc'), + ('sort_key', 'id')])) + self.assert_called('GET', url) + + def test_list_transfer_sorty_not_sorty(self): + self.run_command( + '--os-volume-api-version 3.59 transfer-list') + url = ('/volume-transfers/detail') + self.assert_called('GET', url) diff --git a/cinderclient/v3/shell.py b/cinderclient/v3/shell.py index fefec60e6..98c1bcd7e 100644 --- a/cinderclient/v3/shell.py +++ b/cinderclient/v3/shell.py @@ -2503,3 +2503,53 @@ def do_transfer_create(cs, args): info.pop('links', None) utils.print_dict(info) + + +@utils.arg('--all-tenants', + dest='all_tenants', + metavar='<0|1>', + nargs='?', + type=int, + const=1, + default=0, + help='Shows details for all tenants. Admin only.') +@utils.arg('--all_tenants', + nargs='?', + type=int, + const=1, + help=argparse.SUPPRESS) +@utils.arg('--sort', + metavar='[:]', + default=None, + help='Sort keys and directions in the form of [:].', + start_version='3.59') +def do_transfer_list(cs, args): + """Lists all transfers.""" + all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants)) + search_opts = { + 'all_tenants': all_tenants, + } + + sort = getattr(args, 'sort', None) + sort_key = None + sort_dir = None + if sort: + # We added this feature with sort_key and sort_dir, but that was a + # mistake as we've deprecated that construct a long time ago and should + # be removing it in favor of --sort. Too late for the service side, but + # to make the client experience consistent, we handle the compatibility + # here. + sort_args = sort.split(':') + if len(sort_args) > 2: + raise exceptions.CommandError( + 'Invalid sort parameter provided. Argument must be in the ' + 'form "key[:]".') + + sort_key = sort_args[0] + if len(sort_args) == 2: + sort_dir = sort_args[1] + + transfers = cs.transfers.list( + search_opts=search_opts, sort_key=sort_key, sort_dir=sort_dir) + columns = ['ID', 'Volume ID', 'Name'] + utils.print_list(transfers, columns) diff --git a/cinderclient/v3/volume_transfers.py b/cinderclient/v3/volume_transfers.py index 39e1a2e4e..fe790f24f 100644 --- a/cinderclient/v3/volume_transfers.py +++ b/cinderclient/v3/volume_transfers.py @@ -16,7 +16,6 @@ """Volume transfer interface (v3 extension).""" from cinderclient import base -from cinderclient import utils from cinderclient.v2 import volume_transfers @@ -63,25 +62,24 @@ class VolumeTransferManager(volume_transfers.VolumeTransferManager): return self._get("/os-volume-transfer/%s" % transfer_id, "transfer") - def list(self, detailed=True, search_opts=None): + def list(self, detailed=True, search_opts=None, sort_key=None, + sort_dir=None): """Get a list of all volume transfer. :param detailed: Get detailed object information. :param search_opts: Filtering options. + :param sort_key: Optional key to sort on. + :param sort_dir: Optional direction to sort. :rtype: list of :class:`VolumeTransfer` """ - query_string = utils.build_query_param(search_opts) - - detail = "" - if detailed: - detail = "/detail" - + resource_type = 'os-volume-transfer' if self.api_version.matches('3.55'): - return self._list("/volume-transfers%s%s" % (detail, query_string), - "transfers") + resource_type = 'volume-transfers' - return self._list("/os-volume-transfer%s%s" % (detail, query_string), - "transfers") + url = self._build_list_url(resource_type, detailed=detailed, + search_opts=search_opts, + sort_key=sort_key, sort_dir=sort_dir) + return self._list(url, 'transfers') def delete(self, transfer_id): """Delete a volume transfer. diff --git a/releasenotes/notes/transfer-sort-ca622e9b8da605c1.yaml b/releasenotes/notes/transfer-sort-ca622e9b8da605c1.yaml new file mode 100644 index 000000000..5080f97a5 --- /dev/null +++ b/releasenotes/notes/transfer-sort-ca622e9b8da605c1.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Starting with microversion 3.59, the ``cinder transfer-list`` command now + supports the ``--sort`` argument to sort the returned results. This + argument takes either just the attribute to sort on, or the attribute and + the sort direction. Examples include ``cinder transfer-list --sort=id`` and + ``cinder transfer-list --sort=name:asc``.