Merge "Allow str_replace to reference parameters/attributes" into stable/liberty

This commit is contained in:
Jenkins 2016-03-02 04:15:42 +00:00 committed by Gerrit Code Review
commit df3a50fc53
4 changed files with 68 additions and 4 deletions

View File

@ -402,8 +402,8 @@ class Replace(function.Function):
super(Replace, self).__init__(stack, fn_name, args)
self._mapping, self._string = self._parse_args()
if not isinstance(self._mapping, collections.Mapping):
if not isinstance(self._mapping,
(collections.Mapping, function.Function)):
raise TypeError(_('"%s" parameters must be a mapping') %
self.fn_name)

View File

@ -164,6 +164,8 @@ class HeatTestCase(testscenarios.WithScenarios,
generic_rsrc.ResourceWithRequiredPropsAndEmptyAttrs)
resource._register_class('ResourceWithPropsAndAttrs',
generic_rsrc.ResourceWithPropsAndAttrs)
resource._register_class('ResWithStringPropAndAttr',
generic_rsrc.ResWithStringPropAndAttr),
resource._register_class('ResWithComplexPropsAndAttrs',
generic_rsrc.ResWithComplexPropsAndAttrs)
resource._register_class('ResourceWithCustomConstraint',

View File

@ -71,7 +71,21 @@ class ResWithShowAttr(GenericResource):
'Another': self.name}
class ResWithComplexPropsAndAttrs(GenericResource):
class ResWithStringPropAndAttr(GenericResource):
properties_schema = {
'a_string': properties.Schema(properties.Schema.STRING)}
attributes_schema = {'string': attributes.Schema('A string')}
def _resolve_attribute(self, name):
try:
return self.properties["a_%s" % name]
except KeyError:
return None
class ResWithComplexPropsAndAttrs(ResWithStringPropAndAttr):
properties_schema = {
'a_string': properties.Schema(properties.Schema.STRING),

View File

@ -18,6 +18,7 @@ import six
from heat.common import exception
from heat.common import identifier
from heat.common import template_format
from heat.engine.cfn import functions as cfn_functions
from heat.engine import environment
from heat.engine import function
from heat.engine.hot import functions as hot_functions
@ -610,7 +611,54 @@ class HOTemplateTest(common.HeatTestCase):
snippet = {'str_replace': {'template': 'Template var1 string var2',
'params': ['var1', 'foo', 'var2', 'bar']}}
self.assertRaises(TypeError, self.resolve, snippet, tmpl)
ex = self.assertRaises(TypeError, self.resolve, snippet, tmpl)
self.assertIn('parameters must be a mapping', six.text_type(ex))
def test_str_replace_invalid_param_type_init(self):
"""Test str_replace function parameter values.
Pass parameter values of wrong type to function and verify that we get
a TypeError in the constructor.
"""
args = [['var1', 'foo', 'var2', 'bar'],
'Template var1 string var2']
ex = self.assertRaises(
TypeError,
cfn_functions.Replace,
None, 'Fn::Replace', args)
self.assertIn('parameters must be a mapping', six.text_type(ex))
def test_str_replace_ref_get_param(self):
"""Test str_replace referencing parameters."""
hot_tpl = template_format.parse('''
heat_template_version: 2015-04-30
parameters:
p_template:
type: string
default: foo-replaceme
p_params:
type: json
default:
replaceme: success
resources:
rsrc:
type: ResWithStringPropAndAttr
properties:
a_string:
str_replace:
template: {get_param: p_template}
params: {get_param: p_params}
outputs:
replaced:
value: {get_attr: [rsrc, string]}
''')
tmpl = template.Template(hot_tpl)
self.stack = parser.Stack(utils.dummy_context(), 'test_stack', tmpl)
self.stack.store()
self.stack.create()
self.assertEqual((parser.Stack.CREATE, parser.Stack.COMPLETE),
self.stack.state)
self.assertEqual('foo-success', self.stack.output('replaced'))
def test_get_file(self):
"""Test get_file function."""