Ignore dependency errors with conditions

If a resource is conditionally defined, depending on it raises an error
in the case it's not defined. This patch fixes that by checking if the
resource is present in the template regardless of the conditions.

Change-Id: Iefae1fcea720bee4ed69ad1a5fe403d52d54433c
Closes-Bug: #1649900
This commit is contained in:
Thomas Herve 2017-01-12 19:20:35 +01:00
parent e4c751fef8
commit e1aeabaa03
3 changed files with 39 additions and 4 deletions

View File

@ -207,6 +207,10 @@ class ResourceDefinition(object):
def get_resource(res_name):
if res_name not in stack:
if res_name in stack.t.get(stack.t.RESOURCES):
# The resource is conditionally defined, allow dependencies
# on it
return
raise exception.InvalidTemplateReference(resource=res_name,
key=self.name)
return stack[res_name]
@ -230,8 +234,9 @@ class ResourceDefinition(object):
)
return itertools.chain()
return itertools.chain((get_resource(dep) for dep in explicit_depends),
prop_deps, metadata_deps)
return itertools.chain(
filter(None, (get_resource(dep) for dep in explicit_depends)),
prop_deps, metadata_deps)
def properties(self, schema, context=None):
"""Return a Properties object representing the resource properties.

View File

@ -1806,6 +1806,25 @@ conditions:
stack = parser.Stack(utils.dummy_context(), 'test_stack', tmpl)
self.assertRaises(TypeError, self.resolve, snippet, tmpl, stack=stack)
def test_depends_condition(self):
hot_tpl = template_format.parse('''
heat_template_version: 2016-10-14
resources:
one:
type: OS::Heat::None
two:
type: OS::Heat::None
condition: False
three:
type: OS::Heat::None
depends_on: two
''')
tmpl = template.Template(hot_tpl)
stack = parser.Stack(utils.dummy_context(), 'test_stack', tmpl)
stack.validate()
self.assertEqual({'one', 'three'}, set(stack.resources))
class HotStackTest(common.HeatTestCase):
"""Test stack function when stack was created from HOT template."""

View File

@ -34,6 +34,16 @@ resources:
type: OS::Heat::TestResource
'''
TEMPLATE_WITH_INVALID_EXPLICIT_DEPEND = '''
heat_template_version: 2016-10-14
resources:
test1:
type: OS::Heat::TestResource
test3:
type: OS::Heat::TestResource
depends_on: test2
'''
class ResourceDefinitionTest(common.HeatTestCase):
@ -104,8 +114,9 @@ class ResourceDefinitionTest(common.HeatTestCase):
self.assertEqual([], list(rsrc.t.dependencies(stack)))
def test_dependencies_explicit_invalid(self):
rd = rsrc_defn.ResourceDefinition('rsrc', 'SomeType', depends=['baz'])
stack = {'foo': 'FOO', 'bar': 'BAR'}
t = template_format.parse(TEMPLATE_WITH_INVALID_EXPLICIT_DEPEND)
stack = utils.parse_stack(t)
rd = stack.t.resource_definitions(stack)['test3']
self.assertRaises(exception.InvalidTemplateReference,
lambda: list(rd.dependencies(stack)))