Allow resource name as reference if using depends_on

We allow using 'depends_on' instead of 'get_resource'
function to explictly indicate the dependency, and some
resources override the get_reference_id() method to return
the physical_resource_name, in this case the resource name
won't match the physical_resource_name, so we should allow
to find resource in stack with resource name as reference.

Change-Id: Icd37e0d7b341c996f62d78d42cc81da00ec0e7d3
Closes-Bug: #1554372
This commit is contained in:
huangtianhua 2016-03-08 15:20:00 +08:00
parent 6d6a2956c8
commit ef27c7b582
2 changed files with 39 additions and 3 deletions

View File

@ -670,14 +670,15 @@ class Stack(collections.Mapping):
not found.
"""
for r in six.itervalues(self):
if r.state in (
if (r.state in (
(r.INIT, r.COMPLETE),
(r.CREATE, r.IN_PROGRESS),
(r.CREATE, r.COMPLETE),
(r.RESUME, r.IN_PROGRESS),
(r.RESUME, r.COMPLETE),
(r.UPDATE, r.IN_PROGRESS),
(r.UPDATE, r.COMPLETE)) and r.FnGetRefId() == refid:
(r.UPDATE, r.COMPLETE)) and
(r.FnGetRefId() == refid or r.name == refid)):
return r
def register_access_allowed_handler(self, credential_id, handler):

View File

@ -963,7 +963,6 @@ class StackTest(common.HeatTestCase):
self.assertIn('AResource', self.stack)
rsrc = self.stack['AResource']
rsrc.resource_id_set('aaaa')
self.assertIsNotNone(resource)
for action, status in (
(rsrc.INIT, rsrc.COMPLETE),
@ -983,6 +982,42 @@ class StackTest(common.HeatTestCase):
finally:
rsrc.state_set(rsrc.CREATE, rsrc.COMPLETE)
def test_resource_name_ref_by_depends_on(self):
tmpl = {'HeatTemplateFormatVersion': '2012-12-12',
'Resources': {
'AResource': {'Type': 'GenericResourceType'},
'BResource': {'Type': 'ResourceWithPropsType',
'Properties': {'Foo': 'AResource'},
'DependsOn': 'AResource'}}}
self.stack = stack.Stack(self.ctx, 'resource_by_name_ref_stack',
template.Template(tmpl))
self.stack.store()
self.stack.create()
self.assertEqual((stack.Stack.CREATE, stack.Stack.COMPLETE),
self.stack.state)
self.assertIn('AResource', self.stack)
self.assertIn('BResource', self.stack)
rsrc = self.stack['AResource']
rsrc.resource_id_set('aaaa')
b_rsrc = self.stack['BResource']
b_rsrc.resource_id_set('bbbb')
b_foo_ref = b_rsrc.properties.get('Foo')
for action, status in (
(rsrc.INIT, rsrc.COMPLETE),
(rsrc.CREATE, rsrc.IN_PROGRESS),
(rsrc.CREATE, rsrc.COMPLETE),
(rsrc.RESUME, rsrc.IN_PROGRESS),
(rsrc.RESUME, rsrc.COMPLETE),
(rsrc.UPDATE, rsrc.IN_PROGRESS),
(rsrc.UPDATE, rsrc.COMPLETE)):
rsrc.state_set(action, status)
ref_rsrc = self.stack.resource_by_refid(b_foo_ref)
self.assertEqual(rsrc, ref_rsrc)
self.assertIn(b_rsrc.name, ref_rsrc.required_by())
def test_create_failure_recovery(self):
"""Check that rollback still works with dynamic metadata.