From 9ed8c61ec5745f7e07e7eb78888e3e76fcd5b289 Mon Sep 17 00:00:00 2001 From: prashkre Date: Wed, 15 Mar 2017 17:28:57 -0400 Subject: [PATCH] Consistency groups API is not returning project_id filter groups. This fixes issue with consistency groups API which is always returning 'context.project_id' specific consistency groups from cinder.groups instead of required project specific groups i.e. irrespective of filters ('all_tenants','project_id') being passed it is returning context.project_id groups. Closes-Bug: #1671220 Change-Id: I553aab33c01f1e5b0053172e86d276ccfabf4952 --- cinder/api/contrib/consistencygroups.py | 6 +- .../api/contrib/test_consistencygroups.py | 64 ++++++++++++++++++- .../notes/bug-1671220-4d521be71d0b8aa4.yaml | 6 ++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/bug-1671220-4d521be71d0b8aa4.yaml diff --git a/cinder/api/contrib/consistencygroups.py b/cinder/api/contrib/consistencygroups.py index d1568ed0a18..729c64ee9dd 100644 --- a/cinder/api/contrib/consistencygroups.py +++ b/cinder/api/contrib/consistencygroups.py @@ -131,6 +131,10 @@ class ConsistencyGroupsController(wsgi.Controller): """Returns a list of consistency groups through view builder.""" context = req.environ['cinder.context'] filters = req.params.copy() + + # make another copy of filters, since it is being modified in + # consistencygroup_api while getting consistencygroups + group_filters = req.params.copy() marker, limit, offset = common.get_pagination_params(filters) sort_keys, sort_dirs = common.get_sort_params(filters) @@ -139,7 +143,7 @@ class ConsistencyGroupsController(wsgi.Controller): offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) groups = self.group_api.get_all( - context, filters=filters, marker=marker, limit=limit, + context, filters=group_filters, marker=marker, limit=limit, offset=offset, sort_keys=sort_keys, sort_dirs=sort_dirs) if is_detail: diff --git a/cinder/tests/unit/api/contrib/test_consistencygroups.py b/cinder/tests/unit/api/contrib/test_consistencygroups.py index 6ae77038b78..9fa588252a5 100644 --- a/cinder/tests/unit/api/contrib/test_consistencygroups.py +++ b/cinder/tests/unit/api/contrib/test_consistencygroups.py @@ -54,6 +54,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase): self, ctxt=None, name='test_consistencygroup', + user_id=fake.USER_ID, + project_id=fake.PROJECT_ID, description='this is a test consistency group', volume_type_id=fake.VOLUME_TYPE_ID, availability_zone='az1', @@ -63,8 +65,8 @@ class ConsistencyGroupsAPITestCase(test.TestCase): """Create a consistency group object.""" ctxt = ctxt or self.ctxt consistencygroup = objects.ConsistencyGroup(ctxt) - consistencygroup.user_id = fake.USER_ID - consistencygroup.project_id = fake.PROJECT_ID + consistencygroup.user_id = user_id + consistencygroup.project_id = project_id consistencygroup.availability_zone = availability_zone consistencygroup.name = name consistencygroup.description = description @@ -75,6 +77,33 @@ class ConsistencyGroupsAPITestCase(test.TestCase): consistencygroup.create() return consistencygroup + def _create_group( + self, + ctxt=None, + name='test_group', + user_id=fake.USER_ID, + project_id=fake.PROJECT_ID, + description='this is a test group', + group_type_id=fake.VOLUME_TYPE_ID, + availability_zone='az1', + host='fakehost', + status=fields.GroupStatus.CREATING, + **kwargs): + """Create a consistency group object.""" + ctxt = ctxt or self.ctxt + group = objects.Group(ctxt) + group.user_id = user_id + group.project_id = project_id + group.availability_zone = availability_zone + group.name = name + group.description = description + group.group_type_id = group_type_id + group.host = host + group.status = status + group.update(kwargs) + group.create() + return group + def test_show_consistencygroup(self): consistencygroup = self._create_consistencygroup() req = webob.Request.blank('/v2/%s/consistencygroups/%s' % @@ -294,6 +323,37 @@ class ConsistencyGroupsAPITestCase(test.TestCase): consistencygroup2.destroy() consistencygroup3.destroy() + @ddt.data(False, True) + def test_list_consistencygroups_with_project_id(self, is_detail): + consistencygroup1 = self._create_consistencygroup() + consistencygroup2 = self._create_consistencygroup( + name="group", project_id=fake.PROJECT2_ID) + + group1 = self._create_group() + group2 = self._create_group(name="group", project_id=fake.PROJECT2_ID) + url = ('/v2/%s/consistencygroups?' + 'all_tenants=True&project_id=%s') % (fake.PROJECT_ID, + fake.PROJECT2_ID) + if is_detail: + url = ('/v2/%s/consistencygroups/detail?' + 'all_tenants=True&project_id=%s') % (fake.PROJECT_ID, + fake.PROJECT2_ID) + req = webob.Request.blank(url) + req.method = 'GET' + req.headers['Content-Type'] = 'application/json' + res = req.get_response(fakes.wsgi_app(fake_auth_context=self.ctxt)) + res_dict = jsonutils.loads(res.body) + self.assertEqual(200, res.status_int) + self.assertEqual(2, len(res_dict['consistencygroups'])) + self.assertEqual("group", + res_dict['consistencygroups'][0]['name']) + self.assertEqual("group", + res_dict['consistencygroups'][1]['name']) + consistencygroup1.destroy() + consistencygroup2.destroy() + group1.destroy() + group2.destroy() + @ddt.data(False, True) def test_list_consistencygroups_with_sort(self, is_detail): consistencygroup1 = self._create_consistencygroup() diff --git a/releasenotes/notes/bug-1671220-4d521be71d0b8aa4.yaml b/releasenotes/notes/bug-1671220-4d521be71d0b8aa4.yaml new file mode 100644 index 00000000000..1de87c09930 --- /dev/null +++ b/releasenotes/notes/bug-1671220-4d521be71d0b8aa4.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed consistency groups API which was always returning groups + scoped to project ID from user context instead of given input + project ID.