Generate local TemplateResource class
Create a class with properties_schema and attributes_schema when accessing a local template resource. Partial-Bug: #1287164 Change-Id: I4b341a369046543c8e3ee1adf3906568d4d8161a
This commit is contained in:
parent
4a8cae5e28
commit
4585f1eff8
|
@ -114,7 +114,8 @@ class TemplateResourceInfo(ResourceInfo):
|
|||
|
||||
def get_class(self):
|
||||
from heat.engine.resources import template_resource
|
||||
return template_resource.TemplateResource
|
||||
return template_resource.generate_class(str(self.name),
|
||||
self.template_name)
|
||||
|
||||
|
||||
class MapResourceInfo(ResourceInfo):
|
||||
|
|
|
@ -30,6 +30,22 @@ from heat.openstack.common import log as logging
|
|||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def generate_class(name, template_name):
|
||||
try:
|
||||
data = urlfetch.get(template_name, allowed_schemes=('file',))
|
||||
except IOError:
|
||||
return TemplateResource
|
||||
tmpl = template.Template(template_format.parse(data))
|
||||
properties_schema = properties.Properties.schema_from_params(
|
||||
tmpl.param_schemata())
|
||||
attributes_schema = attributes.Attributes.schema_from_outputs(
|
||||
tmpl[tmpl.OUTPUTS])
|
||||
cls = type(name, (TemplateResource,),
|
||||
{"properties_schema": properties_schema,
|
||||
"attributes_schema": attributes_schema})
|
||||
return cls
|
||||
|
||||
|
||||
class TemplateResource(stack_resource.StackResource):
|
||||
'''
|
||||
A resource implemented by a nested stack.
|
||||
|
|
|
@ -360,6 +360,26 @@ class ProviderTemplateTest(HeatTestCase):
|
|||
cls = env.get_class('OS::ResourceType', 'fred')
|
||||
self.assertEqual(template_resource.TemplateResource, cls)
|
||||
|
||||
def test_get_template_resource_class(self):
|
||||
test_templ_name = 'file:///etc/heatr/frodo.yaml'
|
||||
minimal_temp = json.dumps({'HeatTemplateFormatVersion': '2012-12-12',
|
||||
'Parameters': {},
|
||||
'Resources': {}})
|
||||
self.m.StubOutWithMock(urlfetch, "get")
|
||||
urlfetch.get(test_templ_name,
|
||||
allowed_schemes=('file',)).AndReturn(minimal_temp)
|
||||
self.m.ReplayAll()
|
||||
|
||||
env_str = {'resource_registry': {'resources': {'fred': {
|
||||
"OS::ResourceType": test_templ_name}}}}
|
||||
env = environment.Environment(env_str)
|
||||
cls = env.get_class('OS::ResourceType', 'fred')
|
||||
self.assertNotEqual(template_resource.TemplateResource, cls)
|
||||
self.assertTrue(issubclass(cls, template_resource.TemplateResource))
|
||||
self.assertTrue(hasattr(cls, "properties_schema"))
|
||||
self.assertTrue(hasattr(cls, "attributes_schema"))
|
||||
self.m.VerifyAll()
|
||||
|
||||
def test_template_as_resource(self):
|
||||
"""
|
||||
Test that the resulting resource has the right prop and attrib schema.
|
||||
|
@ -378,6 +398,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,
|
||||
allowed_schemes=('file',)).AndRaise(IOError)
|
||||
urlfetch.get(test_templ_name,
|
||||
allowed_schemes=('http', 'https')).AndReturn(test_templ)
|
||||
parsed_test_templ = template_format.parse(test_templ)
|
||||
|
|
Loading…
Reference in New Issue