diff --git a/cinder/db/sqlalchemy/api.py b/cinder/db/sqlalchemy/api.py index 991bde07065..d526befd74e 100644 --- a/cinder/db/sqlalchemy/api.py +++ b/cinder/db/sqlalchemy/api.py @@ -3130,7 +3130,8 @@ def snapshot_get_all_by_project(context, project_id, filters=None, marker=None, paired with corresponding item in sort_keys :returns: list of matching snapshots """ - if filters and not is_valid_model_filters(models.Snapshot, filters): + if filters and not is_valid_model_filters( + models.Snapshot, filters, exclude_list=('host', 'cluster_name')): return [] authorize_project_context(context, project_id) diff --git a/cinder/tests/unit/api/v3/test_snapshots.py b/cinder/tests/unit/api/v3/test_snapshots.py index 5cb1408822e..b8788b2e43b 100644 --- a/cinder/tests/unit/api/v3/test_snapshots.py +++ b/cinder/tests/unit/api/v3/test_snapshots.py @@ -27,6 +27,7 @@ from cinder.tests.unit.api import fakes from cinder.tests.unit import fake_constants as fake from cinder.tests.unit import fake_snapshot from cinder.tests.unit import fake_volume +from cinder.tests.unit import utils as test_utils from cinder import volume UUID = '00000000-0000-0000-0000-000000000001' @@ -125,6 +126,23 @@ class SnapshotApiTest(test.TestCase): body = {"snapshot": snap} self.controller.create(req, body) + @ddt.data(('host', 'test_host1'), ('cluster_name', 'cluster1')) + @ddt.unpack + def test_snapshot_list_with_filter(self, filter_name, filter_value): + volume1 = test_utils.create_volume(self.ctx, host='test_host1', + cluster_name='cluster1') + volume2 = test_utils.create_volume(self.ctx, host='test_host2', + cluster_name='cluster2') + snapshot1 = test_utils.create_snapshot(self.ctx, volume1.id) + snapshot2 = test_utils.create_snapshot(self.ctx, volume2.id) # noqa + + url = '/v3/snapshots?%s=%s' % (filter_name, filter_value) + req = fakes.HTTPRequest.blank(url, use_admin_context=True) + res_dict = self.controller.detail(req) + + self.assertEqual(1, len(res_dict['snapshots'])) + self.assertEqual(snapshot1.id, res_dict['snapshots'][0]['id']) + def test_snapshot_list_with_sort_name(self): self._create_snapshot(name='test1') self._create_snapshot(name='test2') diff --git a/cinder/tests/unit/utils.py b/cinder/tests/unit/utils.py index 3b9abadf4ce..4288f39e6ba 100644 --- a/cinder/tests/unit/utils.py +++ b/cinder/tests/unit/utils.py @@ -70,6 +70,7 @@ def create_volume(ctxt, testcase_instance=None, id=None, metadata=None, + cluster_name=None, **kwargs): """Create a volume object in the DB.""" vol = {} @@ -102,6 +103,8 @@ def create_volume(ctxt, vol['replication_driver_data'] = replication_driver_data if previous_status: vol['previous_status'] = previous_status + if cluster_name: + vol['cluster_name'] = cluster_name if id: with mock.patch('cinder.objects.Volume.obj_attr_is_set',