]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
TemplateResources need to download from local "file://" urls
authorAngus Salkeld <asalkeld@redhat.com>
Mon, 26 Aug 2013 23:12:21 +0000 (09:12 +1000)
committerAngus Salkeld <asalkeld@redhat.com>
Wed, 28 Aug 2013 01:00:48 +0000 (11:00 +1000)
This is ONLY when the template is from the global environment
(not user provided).

Change-Id: Id554b098fdeaaf04e7bb1a8e2e80abe6d484ae9c

heat/engine/resources/template_resource.py
heat/tests/test_provider_template.py

index 30c20f489415fd51c6d940390747c282bb18c5cb..f336af32b8e0712aa1d630f42465bea6d72b3b11 100644 (file)
@@ -45,6 +45,10 @@ class TemplateResource(stack_resource.StackResource):
             json_snippet['Type'],
             registry_type=environment.TemplateResourceInfo)
         self.template_name = tri.template_name
+        if tri.user_resource:
+            self.allowed_schemes = ('http', 'https')
+        else:
+            self.allowed_schemes = ('http', 'https', 'file')
 
         tmpl = template.Template(self.parsed_nested)
         self.properties_schema = (properties.Properties
@@ -96,7 +100,8 @@ class TemplateResource(stack_resource.StackResource):
         t_data = self.stack.t.files.get(self.template_name)
         if not t_data and self.template_name.endswith((".yaml", ".template")):
             try:
-                t_data = urlfetch.get(self.template_name)
+                t_data = urlfetch.get(self.template_name,
+                                      allowed_schemes=self.allowed_schemes)
             except (exceptions.RequestException, IOError) as r_exc:
                 raise ValueError("Could not fetch remote template '%s': %s" %
                                  (self.template_name, str(r_exc)))
index d59d7542fcd5f3b2a325a47c69579a99fb796e66..fe8431cc55fe56fc375509ef2f51fe64a25369cb 100644 (file)
@@ -23,6 +23,7 @@ from heat.engine import environment
 from heat.engine import parser
 from heat.engine import properties
 from heat.engine import resource
+from heat.engine import resources
 from heat.engine.resources import template_resource
 
 from heat.openstack.common import uuidutils
@@ -365,7 +366,8 @@ class ProviderTemplateTest(HeatTestCase):
             test_templ = test_templ_file.read()
         self.assertTrue(test_templ, "Empty test template")
         self.m.StubOutWithMock(urlfetch, "get")
-        urlfetch.get(test_templ_name).AndReturn(test_templ)
+        urlfetch.get(test_templ_name,
+                     allowed_schemes=('http', 'https')).AndReturn(test_templ)
         parsed_test_templ = template_format.parse(test_templ)
         self.m.ReplayAll()
         json_snippet = {
@@ -392,3 +394,45 @@ class ProviderTemplateTest(HeatTestCase):
             self.assertIn(attrib, templ_resource.attributes)
         for k, v in json_snippet.get("Properties").items():
             self.assertEqual(v, templ_resource.properties[k])
+
+    def test_system_template_retrieve_by_file(self):
+        # make sure that a TemplateResource defined in the global environment
+        # can be created and the template retrieved using the "file:"
+        # scheme.
+        g_env = resources.global_env()
+        test_templ_name = 'file:///etc/heatr/frodo.yaml'
+        g_env.load({'resource_registry':
+                   {'Test::Frodo': test_templ_name}})
+        stack = parser.Stack(utils.dummy_context(), 'test_stack',
+                             parser.Template({}),
+                             stack_id=uuidutils.generate_uuid())
+
+        minimal_temp = json.dumps({'Parameters': {}, 'Resources': {}})
+        self.m.StubOutWithMock(urlfetch, "get")
+        urlfetch.get(test_templ_name,
+                     allowed_schemes=('http', 'https',
+                                      'file')).AndReturn(minimal_temp)
+        self.m.ReplayAll()
+
+        temp_res = template_resource.TemplateResource('test_t_res',
+                                                      {"Type": 'Test::Frodo'},
+                                                      stack)
+        self.assertEqual(None, temp_res.validate())
+        self.m.VerifyAll()
+
+    def test_user_template_not_retrieved_by_file(self):
+        # make sure that a TemplateResource defined in the user environment
+        # can NOT be retrieved using the "file:" scheme.
+        env = environment.Environment()
+        test_templ_name = 'file:///etc/heatr/flippy.yaml'
+        env.load({'resource_registry':
+                  {'Test::Flippy': test_templ_name}})
+        stack = parser.Stack(utils.dummy_context(), 'test_stack',
+                             parser.Template({}), env=env,
+                             stack_id=uuidutils.generate_uuid())
+
+        self.assertRaises(ValueError,
+                          template_resource.TemplateResource,
+                          'test_t_res',
+                          {"Type": 'Test::Flippy'},
+                          stack)