Find root stack ID with database operations

This change adds a (currently unused) database function
stack_get_root_id to find the root stack ID for any stack.

Scheduler hints are moved to using Stack.root_stack_id() in this change.
Remaining uses of Stack.root_stack() will switch to using
Stack.root_stack_id() later in the series.

Change-Id: I9914b0df5af119edea6346db0d7c62124fbb1313
Partial-Bug: #1455589
This commit is contained in:
Steve Baker 2015-05-19 10:11:25 -07:00
parent 60300b0149
commit 71a1e26140
9 changed files with 43 additions and 4 deletions

View File

@ -198,6 +198,10 @@ def stack_lock_release(stack_id, engine_id):
return IMPL.stack_lock_release(stack_id, engine_id)
def stack_get_root_id(context, stack_id):
return IMPL.stack_get_root_id(context, stack_id)
def user_creds_create(context):
return IMPL.user_creds_create(context)

View File

@ -576,6 +576,13 @@ def stack_lock_release(stack_id, engine_id):
return True
def stack_get_root_id(context, stack_id):
s = stack_get(context, stack_id)
while s.owner_id:
s = stack_get(context, s.owner_id)
return s.id
def user_creds_create(context):
values = context.to_dict()
user_creds_ref = models.UserCreds()

View File

@ -528,7 +528,7 @@ class Instance(resource.Resource):
if cfg.CONF.stack_scheduler_hints:
if scheduler_hints is None:
scheduler_hints = {}
scheduler_hints['heat_root_stack_id'] = self.stack.root_stack.id
scheduler_hints['heat_root_stack_id'] = self.stack.root_stack_id()
scheduler_hints['heat_stack_id'] = self.stack.id
scheduler_hints['heat_stack_name'] = self.stack.name
scheduler_hints['heat_path_in_stack'] = self.stack.path_in_stack()

View File

@ -670,7 +670,7 @@ class Server(stack_user.StackUser):
if cfg.CONF.stack_scheduler_hints:
if scheduler_hints is None:
scheduler_hints = {}
scheduler_hints['heat_root_stack_id'] = self.stack.root_stack.id
scheduler_hints['heat_root_stack_id'] = self.stack.root_stack_id()
scheduler_hints['heat_stack_id'] = self.stack.id
scheduler_hints['heat_stack_name'] = self.stack.name
scheduler_hints['heat_path_in_stack'] = self.stack.path_in_stack()

View File

@ -257,6 +257,11 @@ class Stack(collections.Mapping):
def reset_dependencies(self):
self._dependencies = None
def root_stack_id(self):
if not self.owner_id:
return self.id
return stack_object.Stack.get_root_id(self.context, self.id)
@property
def root_stack(self):
'''

View File

@ -80,6 +80,10 @@ class Stack(
stack.obj_reset_changes()
return stack
@classmethod
def get_root_id(cls, context, stack_id):
return db_api.stack_get_root_id(context, stack_id)
@classmethod
def get_by_id(cls, context, stack_id, **kwargs):
db_stack = db_api.stack_get(context, stack_id, **kwargs)

View File

@ -550,7 +550,7 @@ class InstancesTest(common.HeatTestCase):
limit=instance.physical_resource_name_limit),
security_groups=None,
userdata=mox.IgnoreArg(),
scheduler_hints={'heat_root_stack_id': stack.root_stack.id,
scheduler_hints={'heat_root_stack_id': stack.root_stack_id(),
'heat_stack_id': stack.id,
'heat_stack_name': stack.name,
'heat_path_in_stack': [(None, stack.name)],

View File

@ -1801,6 +1801,25 @@ class DBAPIStackTest(common.HeatTestCase):
self.assertIsNone(db_api.stack_get(ctx, stacks[s].id,
show_deleted=True))
def test_stack_get_root_id(self):
root = create_stack(self.ctx, self.template, self.user_creds,
name='root stack')
child_1 = create_stack(self.ctx, self.template, self.user_creds,
name='child 1 stack', owner_id=root.id)
child_2 = create_stack(self.ctx, self.template, self.user_creds,
name='child 2 stack', owner_id=child_1.id)
child_3 = create_stack(self.ctx, self.template, self.user_creds,
name='child 3 stack', owner_id=child_2.id)
self.assertEqual(root.id, db_api.stack_get_root_id(
self.ctx, child_3.id))
self.assertEqual(root.id, db_api.stack_get_root_id(
self.ctx, child_2.id))
self.assertEqual(root.id, db_api.stack_get_root_id(
self.ctx, root.id))
self.assertEqual(root.id, db_api.stack_get_root_id(
self.ctx, child_1.id))
class DBAPIResourceTest(common.HeatTestCase):
def setUp(self):

View File

@ -905,7 +905,7 @@ class ServersTest(common.HeatTestCase):
name=server_name,
security_groups=[],
userdata=mox.IgnoreArg(),
scheduler_hints={'heat_root_stack_id': stack.root_stack.id,
scheduler_hints={'heat_root_stack_id': stack.root_stack_id(),
'heat_stack_id': stack.id,
'heat_stack_name': stack.name,
'heat_path_in_stack': [(None, stack.name)],