From: Zane Bitter Date: Wed, 11 Jul 2012 14:37:47 +0000 (-0400) Subject: Allow partial template parsing X-Git-Tag: 2014.1~1617 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=5aac736bb60cfd72887aef2555c3c0cbe24ea6e1;p=openstack-build%2Fheat-build.git Allow partial template parsing Make it possible to only parse the section we need when generating the parsed template for a resource. Change-Id: Ib4216d8d7bfdbca81f43690b6e8c53c15c71a232 Signed-off-by: Zane Bitter --- diff --git a/heat/engine/resources.py b/heat/engine/resources.py index 55a0d3ff..54564bfc 100644 --- a/heat/engine/resources.py +++ b/heat/engine/resources.py @@ -93,8 +93,17 @@ class Resource(object): self._nova = {} self._keystone = None - def parsed_template(self): - return self.stack.resolve_runtime_data(self.t) + def parsed_template(self, section=None, default={}): + ''' + Return the parsed template data for the resource. May be limited to + only one section of the data, in which case a default value may also + be supplied. + ''' + if section is None: + template = self.t + else: + template = self.t.get(section, default) + return self.stack.resolve_runtime_data(template) def __str__(self): return '%s "%s"' % (self.__class__.__name__, self.name) diff --git a/heat/tests/test_resource.py b/heat/tests/test_resource.py index cc3011d3..a5bf9db4 100644 --- a/heat/tests/test_resource.py +++ b/heat/tests/test_resource.py @@ -47,6 +47,26 @@ class ResourceTest(unittest.TestCase): res.state_set('blarg', 'wibble') self.assertEqual(res.state_description, 'wibble') + def test_parsed_template(self): + tmpl = { + 'Type': 'Foo', + 'foo': {'Fn::Join': [' ', ['bar', 'baz', 'quux']]} + } + res = resources.GenericResource('test_resource', tmpl, self.stack) + + parsed_tmpl = res.parsed_template() + self.assertEqual(parsed_tmpl['Type'], 'Foo') + self.assertEqual(parsed_tmpl['foo'], 'bar baz quux') + + self.assertEqual(res.parsed_template('foo'), 'bar baz quux') + self.assertEqual(res.parsed_template('foo', 'bar'), 'bar baz quux') + + def test_parsed_template_default(self): + tmpl = {'Type': 'Foo'} + res = resources.GenericResource('test_resource', tmpl, self.stack) + self.assertEqual(res.parsed_template('foo'), {}) + self.assertEqual(res.parsed_template('foo', 'bar'), 'bar') + # allows testing of the test directly, shown below if __name__ == '__main__':