Merge "Add a Fn::Split function to aid provider templates"

This commit is contained in:
Jenkins 2013-06-18 21:03:56 +00:00 committed by Gerrit Code Review
commit 3a980a0ca0
3 changed files with 44 additions and 0 deletions

View File

@ -575,6 +575,7 @@ def resolve_runtime_data(template, resources, snippet):
resources=resources),
functools.partial(template.resolve_attributes,
resources=resources),
template.resolve_split,
template.resolve_select,
template.resolve_joins,
template.resolve_replace,

View File

@ -281,6 +281,31 @@ class Template(collections.Mapping):
return _resolve(lambda k, v: k == 'Fn::Join', handle_join, s)
@staticmethod
def resolve_split(s):
'''
Split strings in Fn::Split to a list of sub strings
eg the following
{ "Fn::Split" : [ ",", "str1,str2,str3,str4"]}
is reduced to
{["str1", "str2", "str3", "str4"]}
'''
def handle_split(args):
if not isinstance(args, (list, tuple)):
raise TypeError('Arguments to "Fn::Split" must be a list')
example = '"Fn::Split" : [ ",", "str1, str2"]]'
try:
delim, strings = args
except ValueError as ex:
raise ValueError('Incorrect arguments to "Fn::Split" %s: %s' %
('should be', example))
if not isinstance(strings, basestring):
raise TypeError('Incorrect arguments to "Fn::Split" %s: %s' %
('should be', example))
return strings.split(delim)
return _resolve(lambda k, v: k == 'Fn::Split', handle_split, s)
@staticmethod
def resolve_replace(s):
"""

View File

@ -332,6 +332,24 @@ class TemplateTest(HeatTestCase):
self.assertRaises(TypeError, parser.Template.resolve_joins,
join3)
def test_split_ok(self):
data = {"Fn::Split": [";", "foo; bar; achoo"]}
self.assertEqual(parser.Template.resolve_split(data),
['foo', ' bar', ' achoo'])
def test_split_no_delim_in_str(self):
data = {"Fn::Split": [";", "foo, bar, achoo"]}
self.assertEqual(parser.Template.resolve_split(data),
['foo, bar, achoo'])
def test_split_no_delim(self):
data = {"Fn::Split": ["foo, bar, achoo"]}
self.assertRaises(ValueError, parser.Template.resolve_split, data)
def test_split_no_list(self):
data = {"Fn::Split": "foo, bar, achoo"}
self.assertRaises(TypeError, parser.Template.resolve_split, data)
def test_base64(self):
snippet = {"Fn::Base64": "foobar"}
# For now, the Base64 function just returns the original text, and