Stack identity reads tenant from stack not context

This refactors the way stack identity reads tenant_id information so
that it uses the information already in the stack, instead of getting it
from the context every time.

This allows for unscoped list of stacks to show correct project
information (the project that each stack belongs to), instead of the
current one in the context.

Co-Authored-By: Richard Lee <rblee88@gmail.com>
Closes-Bug: 1294385
Change-Id: I976e7f7dea4432b97a2b97382bf05eb3bd1ca565
This commit is contained in:
Anderson Mesquita 2014-03-18 17:34:10 -05:00
parent 976801ecdd
commit f2622f04f3
2 changed files with 32 additions and 7 deletions

View File

@ -61,7 +61,7 @@ class Stack(collections.Mapping):
disable_rollback=True, parent_resource=None, owner_id=None,
adopt_stack_data=None, stack_user_project_id=None,
created_time=None, updated_time=None,
user_creds_id=None):
user_creds_id=None, tenant_id=None):
'''
Initialise from a context, name, Template object and (optionally)
Environment object. The database ID may also be initialised, if the
@ -96,6 +96,10 @@ class Stack(collections.Mapping):
self.updated_time = updated_time
self.user_creds_id = user_creds_id
# This will use the provided tenant ID when loading the stack
# from the DB or get it from the context for new stacks.
self.tenant_id = tenant_id or self.context.tenant_id
resources.initialise()
self.env = env or environment.Environment({})
@ -190,7 +194,7 @@ class Stack(collections.Mapping):
stack_user_project_id=stack.stack_user_project_id,
created_time=stack.created_at,
updated_time=stack.updated_at,
user_creds_id=stack.user_creds_id)
user_creds_id=stack.user_creds_id, tenant_id=stack.tenant)
return stack
@ -205,7 +209,7 @@ class Stack(collections.Mapping):
'parameters': self.env.user_env_as_dict(),
'owner_id': self.owner_id,
'username': self.context.username,
'tenant': self.context.tenant_id,
'tenant': self.tenant_id,
'action': self.action,
'status': self.status,
'status_reason': self.status_reason,
@ -243,8 +247,7 @@ class Stack(collections.Mapping):
'''
Return an identifier for this stack.
'''
return identifier.HeatIdentifier(self.context.tenant_id,
self.name, self.id)
return identifier.HeatIdentifier(self.tenant_id, self.name, self.id)
def __iter__(self):
'''

View File

@ -739,6 +739,17 @@ class StackTest(HeatTestCase):
self.m.ReplayAll()
def test_stack_reads_tenant(self):
stack = parser.Stack(self.ctx, 'test_stack', parser.Template({}),
tenant_id='bar')
self.assertEqual('bar', stack.tenant_id)
def test_stack_reads_tenant_from_context_if_empty(self):
self.ctx.tenant_id = 'foo'
stack = parser.Stack(self.ctx, 'test_stack', parser.Template({}),
tenant_id=None)
self.assertEqual('foo', stack.tenant_id)
def test_stack_string_repr(self):
stack = parser.Stack(self.ctx, 'test_stack', parser.Template({}))
expected = 'Stack "%s" [%s]' % (stack.name, stack.id)
@ -888,7 +899,8 @@ class StackTest(HeatTestCase):
stack_user_project_id=None,
created_time=IgnoreArg(),
updated_time=None,
user_creds_id=stack.user_creds_id)
user_creds_id=stack.user_creds_id,
tenant_id='test_tenant_id')
self.m.ReplayAll()
parser.Stack.load(self.ctx, stack_id=self.stack.id,
@ -904,7 +916,7 @@ class StackTest(HeatTestCase):
parser.Template({}))
self.stack.store()
identifier = self.stack.identifier()
self.assertEqual(self.ctx.tenant_id, identifier.tenant)
self.assertEqual(self.stack.tenant_id, identifier.tenant)
self.assertEqual('identifier_test', identifier.stack_name)
self.assertTrue(identifier.stack_id)
self.assertFalse(identifier.path)
@ -1005,6 +1017,16 @@ class StackTest(HeatTestCase):
newstack = parser.Stack.load(self.ctx, stack_id=self.stack.id)
self.assertEqual(identifier.arn(), newstack.parameters['AWS::StackId'])
@utils.stack_delete_after
def test_load_reads_tenant_id(self):
self.ctx.tenant_id = 'foobar'
self.stack = parser.Stack(self.ctx, 'stack_name', parser.Template({}))
self.stack.store()
stack_id = self.stack.id
self.ctx.tenant_id = None
stack = parser.Stack.load(self.ctx, stack_id=stack_id)
self.assertEqual('foobar', stack.tenant_id)
@utils.stack_delete_after
def test_created_time(self):
self.stack = parser.Stack(self.ctx, 'creation_time_test',