From: Angus Salkeld Date: Mon, 26 Aug 2013 23:12:21 +0000 (+1000) Subject: TemplateResources need to download from local "file://" urls X-Git-Tag: 2014.1~112^2 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=0fb5cb47eec59290fc3f1ead4cbae0747304c043;p=openstack-build%2Fheat-build.git TemplateResources need to download from local "file://" urls This is ONLY when the template is from the global environment (not user provided). Change-Id: Id554b098fdeaaf04e7bb1a8e2e80abe6d484ae9c --- diff --git a/heat/engine/resources/template_resource.py b/heat/engine/resources/template_resource.py index 30c20f48..f336af32 100644 --- a/heat/engine/resources/template_resource.py +++ b/heat/engine/resources/template_resource.py @@ -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))) diff --git a/heat/tests/test_provider_template.py b/heat/tests/test_provider_template.py index d59d7542..fe8431cc 100644 --- a/heat/tests/test_provider_template.py +++ b/heat/tests/test_provider_template.py @@ -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)