From 2c7758d4513fa257b0d684de878f921184b47ae1 Mon Sep 17 00:00:00 2001 From: wangxiyuan Date: Fri, 6 Jan 2017 11:32:38 +0800 Subject: [PATCH] Support sort backup with name Cinder now doesn't support sorting backup by "name". This patch adds the support to keep it consistent with volume and snapshot. APIImpact Change-Id: If4779ee1905d2ceea44f1faeaf772b209b0d074c --- cinder/api/contrib/backups.py | 5 +++++ cinder/api/openstack/api_version_request.py | 3 ++- .../openstack/rest_api_version_history.rst | 4 ++++ cinder/api/v3/backups.py | 4 ++++ cinder/tests/unit/api/v3/test_backups.py | 21 +++++++++++++++++++ ..._sort_backup_by_name-0b080bcb60c0eaa0.yaml | 3 +++ 6 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/support_sort_backup_by_name-0b080bcb60c0eaa0.yaml diff --git a/cinder/api/contrib/backups.py b/cinder/api/contrib/backups.py index 261d6287dfc..a23a65b3d74 100644 --- a/cinder/api/contrib/backups.py +++ b/cinder/api/contrib/backups.py @@ -87,6 +87,10 @@ class BackupsController(wsgi.Controller): filters, self._get_backup_filter_options()) + def _convert_sort_name(self, req_version, sort_keys): + """Convert sort key "name" to "display_name". """ + pass + def _get_backups(self, req, is_detail): """Returns a list of backups, transformed through view builder.""" context = req.environ['cinder.context'] @@ -95,6 +99,7 @@ class BackupsController(wsgi.Controller): marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) + self._convert_sort_name(req_version, sort_keys) self._process_backup_filtering(context=context, filters=filters, req_version=req_version) diff --git a/cinder/api/openstack/api_version_request.py b/cinder/api/openstack/api_version_request.py index 7bc492d971c..4d229f08bc0 100644 --- a/cinder/api/openstack/api_version_request.py +++ b/cinder/api/openstack/api_version_request.py @@ -90,6 +90,7 @@ REST_API_VERSION_HISTORY = """ list APIs. * 3.35 - Add ``volume-type`` filter to Get-Pools API. * 3.36 - Add metadata to volumes/summary response body. + * 3.37 - Support sort backup by "name". """ # The minimum and maximum versions of the API supported @@ -97,7 +98,7 @@ REST_API_VERSION_HISTORY = """ # minimum version of the API supported. # Explicitly using /v1 or /v2 endpoints will still work _MIN_API_VERSION = "3.0" -_MAX_API_VERSION = "3.36" +_MAX_API_VERSION = "3.37" _LEGACY_API_VERSION1 = "1.0" _LEGACY_API_VERSION2 = "2.0" diff --git a/cinder/api/openstack/rest_api_version_history.rst b/cinder/api/openstack/rest_api_version_history.rst index 3de8d56b4de..e1045c5f408 100644 --- a/cinder/api/openstack/rest_api_version_history.rst +++ b/cinder/api/openstack/rest_api_version_history.rst @@ -321,3 +321,7 @@ user documentation. 3.36 ---- Add metadata to volumes/summary response body. + +3.37 +---- + Support sort backup by "name". diff --git a/cinder/api/v3/backups.py b/cinder/api/v3/backups.py index d639fb2b948..5793a4cadd9 100644 --- a/cinder/api/v3/backups.py +++ b/cinder/api/v3/backups.py @@ -96,6 +96,10 @@ class BackupsController(backups_v2.BackupsController): pass return resp_backup + def _convert_sort_name(self, req_version, sort_keys): + if req_version.matches("3.37") and 'name' in sort_keys: + sort_keys[sort_keys.index('name')] = 'display_name' + def create_resource(): return wsgi.Resource(BackupsController()) diff --git a/cinder/tests/unit/api/v3/test_backups.py b/cinder/tests/unit/api/v3/test_backups.py index 2bf9e0d16fe..33fdabae805 100644 --- a/cinder/tests/unit/api/v3/test_backups.py +++ b/cinder/tests/unit/api/v3/test_backups.py @@ -21,6 +21,7 @@ import webob from cinder.api.openstack import api_version_request as api_version from cinder.api.v3 import backups +from cinder.api.views import backups as backup_view import cinder.backup from cinder import context from cinder import exception @@ -100,6 +101,26 @@ class BackupsControllerAPITestCase(test.TestCase): mock.ANY, 'backup', support_like) + @ddt.data('3.36', '3.37') + def test_backup_list_with_name(self, version): + backup1 = test_utils.create_backup( + self.ctxt, display_name='b_test_name', + status=fields.BackupStatus.AVAILABLE) + backup2 = test_utils.create_backup( + self.ctxt, display_name='a_test_name', + status=fields.BackupStatus.AVAILABLE) + url = '/v3/%s/backups?sort_key=name' % fake.PROJECT_ID + req = fakes.HTTPRequest.blank(url, version=version) + if version == '3.36': + self.assertRaises(exception.InvalidInput, + self.controller.index, + req) + else: + expect = backup_view.ViewBuilder().summary_list(req, + [backup1, backup2]) + result = self.controller.index(req) + self.assertEqual(expect, result) + def test_backup_update(self): backup = test_utils.create_backup( self.ctxt, diff --git a/releasenotes/notes/support_sort_backup_by_name-0b080bcb60c0eaa0.yaml b/releasenotes/notes/support_sort_backup_by_name-0b080bcb60c0eaa0.yaml new file mode 100644 index 00000000000..79d56362915 --- /dev/null +++ b/releasenotes/notes/support_sort_backup_by_name-0b080bcb60c0eaa0.yaml @@ -0,0 +1,3 @@ +--- +features: + - Add support for sorting backups by "name". \ No newline at end of file