Merge "Changes for HOT hello world template processing"
This commit is contained in:
commit
df20c7527b
|
@ -27,6 +27,14 @@ def _construct_yaml_str(self, node):
|
||||||
return self.construct_scalar(node)
|
return self.construct_scalar(node)
|
||||||
yaml.Loader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
|
yaml.Loader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
|
||||||
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
|
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:str', _construct_yaml_str)
|
||||||
|
# Unquoted dates like 2013-05-23 in yaml files get loaded as objects of type
|
||||||
|
# datetime.data which causes problems in API layer when being processed by
|
||||||
|
# openstack.common.jsonutils. Therefore, make unicode string out of timestamps
|
||||||
|
# until jsonutils can handle dates.
|
||||||
|
yaml.Loader.add_constructor(u'tag:yaml.org,2002:timestamp',
|
||||||
|
_construct_yaml_str)
|
||||||
|
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:timestamp',
|
||||||
|
_construct_yaml_str)
|
||||||
|
|
||||||
|
|
||||||
def parse(tmpl_str, add_template_sections=True):
|
def parse(tmpl_str, add_template_sections=True):
|
||||||
|
@ -45,7 +53,7 @@ def parse(tmpl_str, add_template_sections=True):
|
||||||
else:
|
else:
|
||||||
if tpl is None:
|
if tpl is None:
|
||||||
tpl = {}
|
tpl = {}
|
||||||
if add_template_sections:
|
if add_template_sections and u'heat_template_version' not in tpl:
|
||||||
default_for_missing(tpl, u'HeatTemplateFormatVersion',
|
default_for_missing(tpl, u'HeatTemplateFormatVersion',
|
||||||
HEAT_VERSIONS)
|
HEAT_VERSIONS)
|
||||||
return tpl
|
return tpl
|
||||||
|
|
|
@ -0,0 +1,193 @@
|
||||||
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
|
||||||
|
from heat.common import exception
|
||||||
|
from heat.engine import template
|
||||||
|
from heat.openstack.common import log as logging
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
SECTIONS = (VERSION, DESCRIPTION, PARAMETERS,
|
||||||
|
RESOURCES, OUTPUTS, UNDEFINED) = \
|
||||||
|
('heat_template_version', 'description', 'parameters',
|
||||||
|
'resources', 'outputs', '__undefined__')
|
||||||
|
|
||||||
|
_CFN_TO_HOT_SECTIONS = {template.VERSION: VERSION,
|
||||||
|
template.DESCRIPTION: DESCRIPTION,
|
||||||
|
template.PARAMETERS: PARAMETERS,
|
||||||
|
template.MAPPINGS: UNDEFINED,
|
||||||
|
template.RESOURCES: RESOURCES,
|
||||||
|
template.OUTPUTS: OUTPUTS}
|
||||||
|
|
||||||
|
|
||||||
|
class HOTemplate(template.Template):
|
||||||
|
"""
|
||||||
|
A Heat Orchestration Template format stack template.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __getitem__(self, section):
|
||||||
|
""""Get the relevant section in the template."""
|
||||||
|
#first translate from CFN into HOT terminology if necessary
|
||||||
|
section = HOTemplate._translate(section, _CFN_TO_HOT_SECTIONS, section)
|
||||||
|
|
||||||
|
if section not in SECTIONS:
|
||||||
|
raise KeyError('"%s" is not a valid template section' % section)
|
||||||
|
|
||||||
|
if section == VERSION:
|
||||||
|
return self.t[section]
|
||||||
|
|
||||||
|
if section == UNDEFINED:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
if section == DESCRIPTION:
|
||||||
|
default = 'No description'
|
||||||
|
else:
|
||||||
|
default = {}
|
||||||
|
|
||||||
|
the_section = self.t.get(section, default)
|
||||||
|
|
||||||
|
# In some cases (e.g. parameters), also translate each entry of
|
||||||
|
# a section into CFN format (case, naming, etc) so the rest of the
|
||||||
|
# engine can cope with it.
|
||||||
|
# This is a shortcut for now and might be changed in the future.
|
||||||
|
|
||||||
|
if section == PARAMETERS:
|
||||||
|
return self._translate_parameters(the_section)
|
||||||
|
|
||||||
|
if section == RESOURCES:
|
||||||
|
return self._translate_resources(the_section)
|
||||||
|
|
||||||
|
if section == OUTPUTS:
|
||||||
|
return self._translate_outputs(the_section)
|
||||||
|
|
||||||
|
return the_section
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _translate(value, mapping, default=None):
|
||||||
|
if value in mapping:
|
||||||
|
return mapping[value]
|
||||||
|
|
||||||
|
return default
|
||||||
|
|
||||||
|
def _translate_parameters(self, parameters):
|
||||||
|
"""Get the parameters of the template translated into CFN format."""
|
||||||
|
HOT_TO_CFN_ATTRS = {'type': 'Type',
|
||||||
|
'description': 'Description'}
|
||||||
|
|
||||||
|
HOT_TO_CFN_TYPES = {'string': 'String'}
|
||||||
|
|
||||||
|
cfn_params = {}
|
||||||
|
|
||||||
|
for param_name, attrs in parameters.iteritems():
|
||||||
|
cfn_param = {}
|
||||||
|
|
||||||
|
for attr, attr_value in attrs.iteritems():
|
||||||
|
cfn_attr = self._translate(attr, HOT_TO_CFN_ATTRS, attr)
|
||||||
|
|
||||||
|
if attr == 'type':
|
||||||
|
# try to translate type; if no translation found, keep
|
||||||
|
# original value and let engine throw an error for an
|
||||||
|
# unsupported type
|
||||||
|
attr_value = self._translate(attr_value, HOT_TO_CFN_TYPES,
|
||||||
|
attr_value)
|
||||||
|
|
||||||
|
cfn_param[cfn_attr] = attr_value
|
||||||
|
|
||||||
|
if len(cfn_param) > 0:
|
||||||
|
cfn_params[param_name] = cfn_param
|
||||||
|
|
||||||
|
return cfn_params
|
||||||
|
|
||||||
|
def _translate_resources(self, resources):
|
||||||
|
"""Get the resources of the template translated into CFN format."""
|
||||||
|
HOT_TO_CFN_ATTRS = {'type': 'Type',
|
||||||
|
'properties': 'Properties'}
|
||||||
|
|
||||||
|
cfn_resources = {}
|
||||||
|
|
||||||
|
for resource_name, attrs in resources.iteritems():
|
||||||
|
cfn_resource = {}
|
||||||
|
|
||||||
|
for attr, attr_value in attrs.iteritems():
|
||||||
|
cfn_attr = self._translate(attr, HOT_TO_CFN_ATTRS, attr)
|
||||||
|
cfn_resource[cfn_attr] = attr_value
|
||||||
|
|
||||||
|
cfn_resources[resource_name] = cfn_resource
|
||||||
|
|
||||||
|
return cfn_resources
|
||||||
|
|
||||||
|
def _translate_outputs(self, outputs):
|
||||||
|
"""Get the outputs of the template translated into CFN format."""
|
||||||
|
HOT_TO_CFN_ATTRS = {'description': 'Description',
|
||||||
|
'value': 'Value'}
|
||||||
|
|
||||||
|
cfn_outputs = {}
|
||||||
|
|
||||||
|
for output_name, attrs in outputs.iteritems():
|
||||||
|
cfn_output = {}
|
||||||
|
|
||||||
|
for attr, attr_value in attrs.iteritems():
|
||||||
|
cfn_attr = self._translate(attr, HOT_TO_CFN_ATTRS, attr)
|
||||||
|
cfn_output[cfn_attr] = attr_value
|
||||||
|
|
||||||
|
cfn_outputs[output_name] = cfn_output
|
||||||
|
|
||||||
|
return cfn_outputs
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def resolve_param_refs(s, parameters):
|
||||||
|
"""
|
||||||
|
Resolve constructs of the form { get_param: my_param }
|
||||||
|
"""
|
||||||
|
def match_param_ref(key, value):
|
||||||
|
return (key == 'get_param' and
|
||||||
|
isinstance(value, basestring) and
|
||||||
|
value in parameters)
|
||||||
|
|
||||||
|
def handle_param_ref(ref):
|
||||||
|
try:
|
||||||
|
return parameters[ref]
|
||||||
|
except (KeyError, ValueError):
|
||||||
|
raise exception.UserParameterMissing(key=ref)
|
||||||
|
|
||||||
|
return template._resolve(match_param_ref, handle_param_ref, s)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def resolve_attributes(s, resources):
|
||||||
|
"""
|
||||||
|
Resolve constructs of the form { get_attr: [my_resource, my_attr] }
|
||||||
|
"""
|
||||||
|
def match_get_attr(key, value):
|
||||||
|
return (key == 'get_attr' and
|
||||||
|
isinstance(value, list) and
|
||||||
|
len(value) == 2 and
|
||||||
|
isinstance(value[0], basestring) and
|
||||||
|
value[0] in resources)
|
||||||
|
|
||||||
|
def handle_get_attr(args):
|
||||||
|
resource, att = args
|
||||||
|
try:
|
||||||
|
r = resources[resource]
|
||||||
|
if r.state in (
|
||||||
|
(r.CREATE, r.IN_PROGRESS),
|
||||||
|
(r.CREATE, r.COMPLETE),
|
||||||
|
(r.UPDATE, r.IN_PROGRESS),
|
||||||
|
(r.UPDATE, r.COMPLETE)):
|
||||||
|
return r.FnGetAtt(att)
|
||||||
|
except KeyError:
|
||||||
|
raise exception.InvalidTemplateAttribute(resource=resource,
|
||||||
|
key=att)
|
||||||
|
|
||||||
|
return template._resolve(match_get_attr, handle_get_attr, s)
|
|
@ -34,7 +34,10 @@ class Template(collections.Mapping):
|
||||||
|
|
||||||
if cls == Template:
|
if cls == Template:
|
||||||
if 'heat_template_version' in template:
|
if 'heat_template_version' in template:
|
||||||
return HOTemplate(template, *args, **kwargs)
|
# deferred import of HOT module to avoid circular dependency
|
||||||
|
# at load time
|
||||||
|
from heat.engine import hot
|
||||||
|
return hot.HOTemplate(template, *args, **kwargs)
|
||||||
|
|
||||||
return super(Template, cls).__new__(cls)
|
return super(Template, cls).__new__(cls)
|
||||||
|
|
||||||
|
@ -359,27 +362,6 @@ class Template(collections.Mapping):
|
||||||
return _resolve(lambda k, v: k == 'Fn::Base64', handle_base64, s)
|
return _resolve(lambda k, v: k == 'Fn::Base64', handle_base64, s)
|
||||||
|
|
||||||
|
|
||||||
class HOTemplate(Template):
|
|
||||||
'''
|
|
||||||
A Heat Orchestration Template format stack template.
|
|
||||||
'''
|
|
||||||
|
|
||||||
def __getitem__(self, section):
|
|
||||||
'''Get the relevant section in the template.'''
|
|
||||||
if section not in SECTIONS:
|
|
||||||
raise KeyError('"%s" is not a valid template section' % section)
|
|
||||||
|
|
||||||
if section == VERSION:
|
|
||||||
return self.t['heat_template_version']
|
|
||||||
|
|
||||||
if section == DESCRIPTION:
|
|
||||||
default = 'No description'
|
|
||||||
else:
|
|
||||||
default = {}
|
|
||||||
|
|
||||||
return self.t.get(section.lower(), default)
|
|
||||||
|
|
||||||
|
|
||||||
def _resolve(match, handle, snippet):
|
def _resolve(match, handle, snippet):
|
||||||
'''
|
'''
|
||||||
Resolve constructs in a snippet of a template. The supplied match function
|
Resolve constructs in a snippet of a template. The supplied match function
|
||||||
|
|
|
@ -0,0 +1,167 @@
|
||||||
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
||||||
|
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
|
# not use this file except in compliance with the License. You may obtain
|
||||||
|
# a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
# License for the specific language governing permissions and limitations
|
||||||
|
# under the License.
|
||||||
|
from heat.common import template_format
|
||||||
|
from heat.common import exception
|
||||||
|
from heat.engine import parser
|
||||||
|
from heat.engine import hot
|
||||||
|
from heat.engine import template
|
||||||
|
|
||||||
|
from heat.tests.common import HeatTestCase
|
||||||
|
from heat.tests import test_parser
|
||||||
|
from heat.tests.utils import stack_delete_after
|
||||||
|
|
||||||
|
|
||||||
|
hot_tpl_empty = template_format.parse('''
|
||||||
|
heat_template_version: 2013-05-23
|
||||||
|
''')
|
||||||
|
|
||||||
|
|
||||||
|
class HOTemplateTest(HeatTestCase):
|
||||||
|
"""Test processing of HOT templates."""
|
||||||
|
|
||||||
|
def test_defaults(self):
|
||||||
|
"""Test default content behavior of HOT template."""
|
||||||
|
|
||||||
|
tmpl = parser.Template(hot_tpl_empty)
|
||||||
|
# check if we get the right class
|
||||||
|
self.assertTrue(isinstance(tmpl, hot.HOTemplate))
|
||||||
|
try:
|
||||||
|
# test getting an invalid section
|
||||||
|
tmpl['foobar']
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.fail('Expected KeyError for invalid section')
|
||||||
|
|
||||||
|
# test defaults for valid sections
|
||||||
|
self.assertEquals(tmpl[hot.VERSION], '2013-05-23')
|
||||||
|
self.assertEquals(tmpl[hot.DESCRIPTION], 'No description')
|
||||||
|
self.assertEquals(tmpl[hot.PARAMETERS], {})
|
||||||
|
self.assertEquals(tmpl[hot.RESOURCES], {})
|
||||||
|
self.assertEquals(tmpl[hot.OUTPUTS], {})
|
||||||
|
|
||||||
|
def test_translate_parameters(self):
|
||||||
|
"""Test translation of parameters into internal engine format."""
|
||||||
|
|
||||||
|
hot_tpl = template_format.parse('''
|
||||||
|
heat_template_version: 2013-05-23
|
||||||
|
parameters:
|
||||||
|
param1:
|
||||||
|
description: foo
|
||||||
|
type: string
|
||||||
|
''')
|
||||||
|
|
||||||
|
expected = {'param1': {'Description': 'foo', 'Type': 'String'}}
|
||||||
|
|
||||||
|
tmpl = parser.Template(hot_tpl)
|
||||||
|
self.assertEqual(tmpl[hot.PARAMETERS], expected)
|
||||||
|
|
||||||
|
def test_translate_parameters_unsupported_type(self):
|
||||||
|
"""Test translation of parameters into internal engine format
|
||||||
|
|
||||||
|
This tests if parameters with a type not yet supported by engine
|
||||||
|
are also parsed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
hot_tpl = template_format.parse('''
|
||||||
|
heat_template_version: 2013-05-23
|
||||||
|
parameters:
|
||||||
|
param1:
|
||||||
|
description: foo
|
||||||
|
type: unsupported_type
|
||||||
|
''')
|
||||||
|
|
||||||
|
expected = {'param1': {'Description': 'foo',
|
||||||
|
'Type': 'unsupported_type'}}
|
||||||
|
|
||||||
|
tmpl = parser.Template(hot_tpl)
|
||||||
|
self.assertEqual(tmpl[hot.PARAMETERS], expected)
|
||||||
|
|
||||||
|
def test_translate_resources(self):
|
||||||
|
"""Test translation of resources into internal engine format."""
|
||||||
|
|
||||||
|
hot_tpl = template_format.parse('''
|
||||||
|
heat_template_version: 2013-05-23
|
||||||
|
resources:
|
||||||
|
resource1:
|
||||||
|
type: AWS::EC2::Instance
|
||||||
|
properties:
|
||||||
|
property1: value1
|
||||||
|
''')
|
||||||
|
|
||||||
|
expected = {'resource1': {'Type': 'AWS::EC2::Instance',
|
||||||
|
'Properties': {'property1': 'value1'}}}
|
||||||
|
|
||||||
|
tmpl = parser.Template(hot_tpl)
|
||||||
|
self.assertEqual(tmpl[hot.RESOURCES], expected)
|
||||||
|
|
||||||
|
def test_translate_outputs(self):
|
||||||
|
"""Test translation of outputs into internal engine format."""
|
||||||
|
|
||||||
|
hot_tpl = template_format.parse('''
|
||||||
|
heat_template_version: 2013-05-23
|
||||||
|
outputs:
|
||||||
|
output1:
|
||||||
|
description: output1
|
||||||
|
value: value1
|
||||||
|
''')
|
||||||
|
|
||||||
|
expected = {'output1': {'Description': 'output1', 'Value': 'value1'}}
|
||||||
|
|
||||||
|
tmpl = parser.Template(hot_tpl)
|
||||||
|
self.assertEqual(tmpl[hot.OUTPUTS], expected)
|
||||||
|
|
||||||
|
def test_param_refs(self):
|
||||||
|
"""Test if parameter references work."""
|
||||||
|
params = {'foo': 'bar', 'blarg': 'wibble'}
|
||||||
|
snippet = {'properties': {'key1': {'get_param': 'foo'},
|
||||||
|
'key2': {'get_param': 'blarg'}}}
|
||||||
|
snippet_resolved = {'properties': {'key1': 'bar',
|
||||||
|
'key2': 'wibble'}}
|
||||||
|
tmpl = parser.Template(hot_tpl_empty)
|
||||||
|
self.assertEqual(tmpl.resolve_param_refs(snippet, params),
|
||||||
|
snippet_resolved)
|
||||||
|
|
||||||
|
|
||||||
|
class StackTest(test_parser.StackTest):
|
||||||
|
"""Test stack function when stack was created from HOT template."""
|
||||||
|
|
||||||
|
@stack_delete_after
|
||||||
|
def test_get_attr(self):
|
||||||
|
"""Test resolution of get_attr occurrences in HOT template."""
|
||||||
|
|
||||||
|
hot_tpl = template_format.parse('''
|
||||||
|
heat_template_version: 2013-05-23
|
||||||
|
resources:
|
||||||
|
resource1:
|
||||||
|
type: GenericResourceType
|
||||||
|
''')
|
||||||
|
|
||||||
|
self.stack = parser.Stack(self.ctx, 'test_get_attr',
|
||||||
|
template.Template(hot_tpl))
|
||||||
|
self.stack.store()
|
||||||
|
self.stack.create()
|
||||||
|
self.assertEqual(self.stack.state,
|
||||||
|
(parser.Stack.CREATE, parser.Stack.COMPLETE))
|
||||||
|
|
||||||
|
snippet = {'Value': {'get_attr': ['resource1', 'foo']}}
|
||||||
|
resolved = hot.HOTemplate.resolve_attributes(snippet, self.stack)
|
||||||
|
# GenericResourceType has an attribute 'foo' which yields the resource
|
||||||
|
# name.
|
||||||
|
self.assertEqual(resolved, {'Value': 'resource1'})
|
||||||
|
# test invalid reference
|
||||||
|
self.assertRaises(exception.InvalidTemplateAttribute,
|
||||||
|
hot.HOTemplate.resolve_attributes,
|
||||||
|
{'Value': {'get_attr': ['resource1', 'NotThere']}},
|
||||||
|
self.stack)
|
Loading…
Reference in New Issue