Support 'list_concat_unique'

Adds function 'list_concat_unique' to concat
several lists using python's extend function
and make sure without repeating items.

Blueprint: support-list-concat-unique-function
Change-Id: Ia0e35eb578ebe91543c644155a86030b3258118b
This commit is contained in:
huangtianhua 2017-04-24 10:59:14 +08:00
parent 97d236062c
commit 812e786efb
5 changed files with 71 additions and 2 deletions

View File

@ -295,8 +295,10 @@ for the ``heat_template_version`` key:
document is a HOT template and it may contain features added and/or removed
up until the Pike release. This version adds the ``make_url`` function for
assembling URLs, the ``list_concat`` function for combining multiple
lists, and ``string_replace_vstrict`` which raises errors for
missing and empty params. The complete list of supported functions is::
lists, the ``list_concat_unique`` function for combining multiple
lists without repeating items, and the``string_replace_vstrict`` which
raises errors for missing and empty params. The complete list of
supported functions is::
digest
filter
@ -307,6 +309,7 @@ for the ``heat_template_version`` key:
list_join
make_url
list_concat
list_concat_unique
map_merge
map_replace
repeat
@ -1931,3 +1934,17 @@ For example
Will resolve to the list ``['v1', 'v2', 'v3', 'v4']``.
Null values will be ignored.
list_concat_unique
------------------
The ``list_concat_unique`` function behaves identically to the function
``list_concat``, only removes the repeating items of lists.
For example
.. code-block:: yaml
list_concat_unique: [['v1', 'v2'], ['v2', 'v43']]
Will resolve to the list ``['v1', 'v2', 'v3']``.

View File

@ -1480,6 +1480,8 @@ class ListConcat(function.Function):
"""
_unique = False
def __init__(self, stack, fn_name, args):
super(ListConcat, self).__init__(stack, fn_name, args)
example = (_('"%s" : [ [ <value 1>, <value 2> ], '
@ -1508,4 +1510,20 @@ class ListConcat(function.Function):
ret_list = []
for m in args:
ret_list.extend(ensure_list(m))
if self._unique:
for i in ret_list:
while ret_list.count(i) > 1:
del ret_list[ret_list.index(i)]
return ret_list
class ListConcatUnique(ListConcat):
"""A function for extending lists with unique items.
list_concat_unique is identical to the list_concat function, only
contains unique items in retuning list.
"""
_unique = True

View File

@ -592,6 +592,7 @@ class HOTemplate20170901(HOTemplate20170224):
'make_url': hot_funcs.MakeURL,
'list_concat': hot_funcs.ListConcat,
'str_replace_vstrict': hot_funcs.ReplaceJsonVeryStrict,
'list_concat_unique': hot_funcs.ListConcatUnique,
# functions removed from 2015-10-15
'Fn::Select': hot_funcs.Removed,

View File

@ -2125,6 +2125,34 @@ conditions:
resolved = self.resolve(snippet, tmpl)
self.assertEqual(snippet_resolved, resolved)
def test_list_concat_repeat_dict_item(self):
snippet = {'list_concat': [[{'v1': 'v2'}], [{'v1': 'v2'}]]}
snippet_resolved = [{'v1': 'v2'}, {'v1': 'v2'}]
tmpl = template.Template(hot_pike_tpl_empty)
resolved = self.resolve(snippet, tmpl)
self.assertEqual(snippet_resolved, resolved)
def test_list_concat_repeat_item(self):
snippet = {'list_concat': [['v1', 'v2'], ['v2', 'v3']]}
snippet_resolved = ['v1', 'v2', 'v2', 'v3']
tmpl = template.Template(hot_pike_tpl_empty)
resolved = self.resolve(snippet, tmpl)
self.assertEqual(snippet_resolved, resolved)
def test_list_concat_unique_dict_item(self):
snippet = {'list_concat_unique': [[{'v1': 'v2'}], [{'v1': 'v2'}]]}
snippet_resolved = [{'v1': 'v2'}]
tmpl = template.Template(hot_pike_tpl_empty)
resolved = self.resolve(snippet, tmpl)
self.assertEqual(snippet_resolved, resolved)
def test_list_concat_unique(self):
snippet = {'list_concat_unique': [['v1', 'v2'], ['v2', 'v3']]}
snippet_resolved = ['v1', 'v2', 'v3']
tmpl = template.Template(hot_pike_tpl_empty)
resolved = self.resolve(snippet, tmpl)
self.assertEqual(snippet_resolved, resolved)
def _test_list_concat_invalid(self, snippet):
tmpl = template.Template(hot_pike_tpl_empty)
msg = 'Incorrect arguments'

View File

@ -0,0 +1,5 @@
---
features:
- The list_concat_unique function was added, which behaves identically
to the function ``list_concat`` to concat several lists using
python's extend function and make sure without repeating items.