From 1df6d2aa7984035015391abe13f86b8980035b92 Mon Sep 17 00:00:00 2001 From: ricolin Date: Wed, 6 Apr 2016 14:00:44 +0800 Subject: [PATCH] Support string index for list in get_param Accept for string index like '1' for list in get_param function For example, with lengths equal to [4,5,6]. We can write down something like this: `length: {get_param: [lengths,'1']}`. Closes-Bug: #1566321 Change-Id: I52447dea93ff2e6a32d597a632093661feed7c71 --- heat/engine/hot/functions.py | 8 ++++++++ heat/tests/test_hot.py | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/heat/engine/hot/functions.py b/heat/engine/hot/functions.py index 5356ee51da..241fd9c49d 100644 --- a/heat/engine/hot/functions.py +++ b/heat/engine/hot/functions.py @@ -80,6 +80,14 @@ class GetParam(function.Function): raise TypeError(_('Path components in "%s" ' 'must be strings') % self.fn_name) + if isinstance(collection, collections.Sequence + ) and isinstance(key, six.string_types): + try: + key = int(key) + except ValueError: + raise TypeError(_("Path components in '%s' " + "must be a integer " + "parsable strings.") % self.fn_name) return collection[key] try: diff --git a/heat/tests/test_hot.py b/heat/tests/test_hot.py index d12378ce67..06b3bc370e 100644 --- a/heat/tests/test_hot.py +++ b/heat/tests/test_hot.py @@ -1661,6 +1661,10 @@ class StackParametersTest(common.HeatTestCase): dict(params={'list': 'foo,bar'}, snippet={'properties': {'prop1': {'get_param': ['list', 1]}}}, expected={'properties': {'prop1': 'bar'}})), + ('get_list_attr_string_index', + dict(params={'list': 'foo,bar'}, + snippet={'properties': {'prop1': {'get_param': ['list', '1']}}}, + expected={'properties': {'prop1': 'bar'}})), ('get_flat_dict_attr', dict(params={'flat_dict': {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}},