From c4c5dfdb6938ce1744a1cfd39cd97aff99479cfc Mon Sep 17 00:00:00 2001 From: huangtianhua Date: Fri, 16 Dec 2016 14:56:58 +0800 Subject: [PATCH] Allow admins to get resource by physical resource id This change allows admin users to get resources from other projects by physical resource id. Partial-Bug: #1649759 Change-Id: I6e58ae8fd339bc50d41a5027cf610eb63dbd220c --- heat/db/sqlalchemy/api.py | 2 +- heat/tests/db/test_sqlalchemy_api.py | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/heat/db/sqlalchemy/api.py b/heat/db/sqlalchemy/api.py index aa2955171f..23af087e9c 100644 --- a/heat/db/sqlalchemy/api.py +++ b/heat/db/sqlalchemy/api.py @@ -206,7 +206,7 @@ def resource_get_all_by_physical_resource_id(context, physical_resource_id): .all()) for result in results: - if context is None or context.tenant_id in ( + if context is None or context.is_admin or context.tenant_id in ( result.stack.tenant, result.stack.stack_user_project_id): yield result diff --git a/heat/tests/db/test_sqlalchemy_api.py b/heat/tests/db/test_sqlalchemy_api.py index 6a762ed931..087ce3d45e 100644 --- a/heat/tests/db/test_sqlalchemy_api.py +++ b/heat/tests/db/test_sqlalchemy_api.py @@ -1397,9 +1397,12 @@ def create_stack(ctx, template, user_creds, **kwargs): def create_resource(ctx, stack, **kwargs): + phy_res_id = UUID1 + if 'phys_res_id' in kwargs: + phy_res_id = kwargs.pop('phys_res_id') values = { 'name': 'test_resource_name', - 'physical_resource_id': UUID1, + 'physical_resource_id': phy_res_id, 'action': 'create', 'status': 'complete', 'status_reason': 'create_complete', @@ -2330,13 +2333,30 @@ class DBAPIResourceTest(common.HeatTestCase): ret_res = db_api.resource_get_all_by_physical_resource_id(self.ctx, UUID1) ret_list = list(ret_res) - self.assertTrue(ret_list) + self.assertEqual(2, len(ret_list)) for res in ret_list: self.assertEqual(UUID1, res.physical_resource_id) mt = db_api.resource_get_all_by_physical_resource_id(self.ctx, UUID2) self.assertFalse(list(mt)) + def test_resource_get_all_by_with_admin_context(self): + admin_ctx = utils.dummy_context(is_admin=True, + tenant_id='admin_tenant') + create_resource(self.ctx, self.stack, phys_res_id=UUID1) + create_resource(self.ctx, self.stack, phys_res_id=UUID2) + + ret_res = db_api.resource_get_all_by_physical_resource_id(admin_ctx, + UUID1) + ret_list = list(ret_res) + self.assertEqual(1, len(ret_list)) + self.assertEqual(UUID1, ret_list[0].physical_resource_id) + + mt = db_api.resource_get_all_by_physical_resource_id(admin_ctx, UUID2) + ret_list = list(mt) + self.assertEqual(1, len(ret_list)) + self.assertEqual(UUID2, ret_list[0].physical_resource_id) + def test_resource_get_all(self): values = [ {'name': 'res1'},