]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Allow partial template parsing
authorZane Bitter <zbitter@redhat.com>
Wed, 11 Jul 2012 14:37:47 +0000 (10:37 -0400)
committerZane Bitter <zbitter@redhat.com>
Wed, 11 Jul 2012 15:14:31 +0000 (11:14 -0400)
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 <zbitter@redhat.com>
heat/engine/resources.py
heat/tests/test_resource.py

index 55a0d3ffc9d393dcf16cb4b3a088739bfde7c3dc..54564bfcc02f96776f957dccb4476fd486b97a8d 100644 (file)
@@ -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)
index cc3011d3a2af85cd0122ca32838589e69ce5f101..a5bf9db4bae7548555d7f746cda66b508e807fe5 100644 (file)
@@ -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__':