From d8b024b370cd91e62c43000f0184898ce0c07904 Mon Sep 17 00:00:00 2001 From: Angus Salkeld Date: Tue, 18 Jun 2013 10:15:41 +1000 Subject: [PATCH] Add a Fn::Split function to aid provider templates This needed to convert a parameter in CommaDelimedList into a property of type List. part of blueprint provider-resource Change-Id: Id77a2c6be47427360a2be89ca1fea3c097807bac --- heat/engine/parser.py | 1 + heat/engine/template.py | 25 +++++++++++++++++++++++++ heat/tests/test_parser.py | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/heat/engine/parser.py b/heat/engine/parser.py index 974b8eb1..c3b7c079 100644 --- a/heat/engine/parser.py +++ b/heat/engine/parser.py @@ -574,6 +574,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, diff --git a/heat/engine/template.py b/heat/engine/template.py index 77ca03f2..182b33d2 100644 --- a/heat/engine/template.py +++ b/heat/engine/template.py @@ -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): """ diff --git a/heat/tests/test_parser.py b/heat/tests/test_parser.py index d628d576..8bba4734 100644 --- a/heat/tests/test_parser.py +++ b/heat/tests/test_parser.py @@ -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 -- 2.45.2