diff --git a/cinder/api/openstack/api_version_request.py b/cinder/api/openstack/api_version_request.py index 1680580616d..a34d58b77ce 100644 --- a/cinder/api/openstack/api_version_request.py +++ b/cinder/api/openstack/api_version_request.py @@ -80,6 +80,7 @@ REST_API_VERSION_HISTORY = """ * 3.27 - Add attachment API * 3.28 - Add filters support to get_pools * 3.29 - Add filter, sorter and pagination support in group snapshot. + * 3.30 - Support sort snapshots with "name". """ @@ -88,7 +89,7 @@ REST_API_VERSION_HISTORY = """ # minimum version of the API supported. # Explicitly using /v1 or /v2 enpoints will still work _MIN_API_VERSION = "3.0" -_MAX_API_VERSION = "3.29" +_MAX_API_VERSION = "3.30" _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 5794133424f..532f5cd9069 100644 --- a/cinder/api/openstack/rest_api_version_history.rst +++ b/cinder/api/openstack/rest_api_version_history.rst @@ -292,3 +292,7 @@ user documentation. 3.29 ---- Add filter, sorter and pagination support in group snapshot. + +3.30 +---- + Support sort snapshots with "name". diff --git a/cinder/api/v3/snapshots.py b/cinder/api/v3/snapshots.py index f5c0be42229..5b5b9984474 100644 --- a/cinder/api/v3/snapshots.py +++ b/cinder/api/v3/snapshots.py @@ -81,6 +81,10 @@ class SnapshotsController(snapshots_v2.SnapshotsController): # process filters self._process_filters(req, context, search_opts) + req_version = req.api_version_request + if req_version.matches("3.30", None) and 'name' in sort_keys: + sort_keys[sort_keys.index('name')] = 'display_name' + # NOTE(thingee): v3 API allows name instead of display_name if 'name' in search_opts: search_opts['display_name'] = search_opts.pop('name') diff --git a/cinder/tests/unit/api/v3/test_snapshots.py b/cinder/tests/unit/api/v3/test_snapshots.py index 45ea55a9986..8a74df0fe36 100644 --- a/cinder/tests/unit/api/v3/test_snapshots.py +++ b/cinder/tests/unit/api/v3/test_snapshots.py @@ -107,23 +107,39 @@ class SnapshotApiTest(test.TestCase): self.assertRaises(exception.SnapshotNotFound, self.controller.show, req, snapshot_id) - def _create_snapshot_with_metadata(self, metadata): + def _create_snapshot(self, name=None, metadata=None): """Creates test snapshopt with provided metadata""" req = fakes.HTTPRequest.blank('/v3/snapshots') snap = {"volume_size": 200, "volume_id": fake.VOLUME_ID, - "display_name": "Volume Test Name", + "display_name": name or "Volume Test Name", "display_description": "Volume Test Desc", "availability_zone": "zone1:host1", - "host": "fake-host", - "metadata": metadata} + "host": "fake-host"} + if metadata: + snap["metadata"] = metadata body = {"snapshot": snap} self.controller.create(req, body) + def test_snapshot_list_with_sort_name(self): + self._create_snapshot(name='test1') + self._create_snapshot(name='test2') + + req = fakes.HTTPRequest.blank('/v3/snapshots?sort_key=name', + version='3.29') + self.assertRaises(exception.InvalidInput, self.controller.detail, req) + + req = fakes.HTTPRequest.blank('/v3/snapshots?sort_key=name', + version='3.30') + res_dict = self.controller.detail(req) + self.assertEqual(2, len(res_dict['snapshots'])) + self.assertEqual('test2', res_dict['snapshots'][0]['name']) + self.assertEqual('test1', res_dict['snapshots'][1]['name']) + def test_snapshot_list_with_one_metadata_in_filter(self): # Create snapshot with metadata key1: value1 metadata = {"key1": "val1"} - self._create_snapshot_with_metadata(metadata) + self._create_snapshot(metadata=metadata) # Create request with metadata filter key1: value1 req = create_snapshot_query_with_metadata('{"key1":"val1"}', '3.22') @@ -150,7 +166,7 @@ class SnapshotApiTest(test.TestCase): def test_snapshot_list_with_multiple_metadata_in_filter(self): # Create snapshot with metadata key1: value1, key11: value11 metadata = {"key1": "val1", "key11": "val11"} - self._create_snapshot_with_metadata(metadata) + self._create_snapshot(metadata=metadata) # Create request with metadata filter key1: value1, key11: value11 req = create_snapshot_query_with_metadata( @@ -182,7 +198,7 @@ class SnapshotApiTest(test.TestCase): def test_snapshot_list_with_metadata_unsupported_microversion(self): # Create snapshot with metadata key1: value1 metadata = {"key1": "val1"} - self._create_snapshot_with_metadata(metadata) + self._create_snapshot(metadata=metadata) # Create request with metadata filter key2: value2 req = create_snapshot_query_with_metadata('{"key2":"val2"}', '3.21') diff --git a/releasenotes/notes/support_sort_snapshot_with_name-7b66a2d8e587275d.yaml b/releasenotes/notes/support_sort_snapshot_with_name-7b66a2d8e587275d.yaml new file mode 100644 index 00000000000..1277314aa62 --- /dev/null +++ b/releasenotes/notes/support_sort_snapshot_with_name-7b66a2d8e587275d.yaml @@ -0,0 +1,3 @@ +--- +features: + - Support to sort snapshots with "name".