Account for stack_user_project_id in stack_get_by_name
stack_get compares the context tenant to the stack tenant and the stack_user_project_id. stack_get_by_name only filters by tenant, so identify_stack fails when looking up by stack name and the stack_user_project_id is the context tenant (such as when there is a cfn API describe resource call). This change searches for tenant or stack_user_project_id for stack_get_by_name and stack_get_by_name_and_owner_id. It also extends the tests to ensure a stack is returned when stack_user_project_id matches, and no stack is returned when tenant doesn't match. Closes-Bug: #1288523 Change-Id: I875e232a791c71a4ad55a0e4e5a1423ec3546119
This commit is contained in:
parent
c92aa9b5c5
commit
81b8d45b33
@ -246,7 +246,10 @@ def resource_get_all_by_stack(context, stack_id):
|
||||
|
||||
def stack_get_by_name_and_owner_id(context, stack_name, owner_id):
|
||||
query = soft_delete_aware_query(context, models.Stack).\
|
||||
filter_by(tenant=context.tenant_id).\
|
||||
filter(sqlalchemy.or_(
|
||||
models.Stack.tenant == context.tenant_id,
|
||||
models.Stack.stack_user_project_id == context.tenant_id
|
||||
)).\
|
||||
filter_by(name=stack_name).\
|
||||
filter_by(owner_id=owner_id)
|
||||
|
||||
@ -255,7 +258,10 @@ def stack_get_by_name_and_owner_id(context, stack_name, owner_id):
|
||||
|
||||
def stack_get_by_name(context, stack_name):
|
||||
query = soft_delete_aware_query(context, models.Stack).\
|
||||
filter_by(tenant=context.tenant_id).\
|
||||
filter(sqlalchemy.or_(
|
||||
models.Stack.tenant == context.tenant_id,
|
||||
models.Stack.stack_user_project_id == context.tenant_id
|
||||
)).\
|
||||
filter_by(name=stack_name)
|
||||
|
||||
return query.first()
|
||||
|
@ -96,13 +96,15 @@ class SqlAlchemyTest(HeatTestCase):
|
||||
def tearDown(self):
|
||||
super(SqlAlchemyTest, self).tearDown()
|
||||
|
||||
def _setup_test_stack(self, stack_name, stack_id=None, owner_id=None):
|
||||
def _setup_test_stack(self, stack_name, stack_id=None, owner_id=None,
|
||||
stack_user_project_id=None):
|
||||
t = template_format.parse(wp_template)
|
||||
template = parser.Template(t)
|
||||
stack_id = stack_id or str(uuid.uuid4())
|
||||
stack = parser.Stack(self.ctx, stack_name, template,
|
||||
environment.Environment({'KeyName': 'test'}),
|
||||
owner_id=owner_id)
|
||||
owner_id=owner_id,
|
||||
stack_user_project_id=stack_user_project_id)
|
||||
with utils.UUIDStub(stack_id):
|
||||
stack.store()
|
||||
return (t, stack)
|
||||
@ -280,11 +282,20 @@ class SqlAlchemyTest(HeatTestCase):
|
||||
db_api.resource_data_get, rsrc, 'test')
|
||||
|
||||
def test_stack_get_by_name(self):
|
||||
stack = self._setup_test_stack('stack', UUID1)[1]
|
||||
stack = self._setup_test_stack('stack', UUID1,
|
||||
stack_user_project_id=UUID2)[1]
|
||||
|
||||
st = db_api.stack_get_by_name(self.ctx, 'stack')
|
||||
self.assertEqual(UUID1, st.id)
|
||||
|
||||
self.ctx.tenant_id = UUID3
|
||||
st = db_api.stack_get_by_name(self.ctx, 'stack')
|
||||
self.assertIsNone(st)
|
||||
|
||||
self.ctx.tenant_id = UUID2
|
||||
st = db_api.stack_get_by_name(self.ctx, 'stack')
|
||||
self.assertEqual(UUID1, st.id)
|
||||
|
||||
stack.delete()
|
||||
|
||||
st = db_api.stack_get_by_name(self.ctx, 'stack')
|
||||
@ -304,9 +315,11 @@ class SqlAlchemyTest(HeatTestCase):
|
||||
self.assertIsNone(result)
|
||||
|
||||
def test_stack_get_by_name_and_owner_id(self):
|
||||
stack1 = self._setup_test_stack('stack1', UUID1)[1]
|
||||
stack1 = self._setup_test_stack('stack1', UUID1,
|
||||
stack_user_project_id=UUID3)[1]
|
||||
stack2 = self._setup_test_stack('stack2', UUID2,
|
||||
owner_id=stack1.id)[1]
|
||||
owner_id=stack1.id,
|
||||
stack_user_project_id=UUID3)[1]
|
||||
|
||||
result = db_api.stack_get_by_name_and_owner_id(self.ctx, 'stack2',
|
||||
None)
|
||||
@ -317,6 +330,17 @@ class SqlAlchemyTest(HeatTestCase):
|
||||
|
||||
self.assertEqual(UUID2, result.id)
|
||||
|
||||
self.ctx.tenant_id = str(uuid.uuid4())
|
||||
result = db_api.stack_get_by_name_and_owner_id(self.ctx, 'stack2',
|
||||
None)
|
||||
self.assertIsNone(result)
|
||||
|
||||
self.ctx.tenant_id = UUID3
|
||||
result = db_api.stack_get_by_name_and_owner_id(self.ctx, 'stack2',
|
||||
stack1.id)
|
||||
|
||||
self.assertEqual(UUID2, result.id)
|
||||
|
||||
stack2.delete()
|
||||
|
||||
result = db_api.stack_get_by_name_and_owner_id(self.ctx, 'stack2',
|
||||
|
Loading…
Reference in New Issue
Block a user