Skip to content

Commit

Permalink
When using a list param in foreach pass back select statements when n…
Browse files Browse the repository at this point in the history
…o allowed value
  • Loading branch information
kddejong committed Apr 29, 2024
1 parent 6918363 commit 0938fbc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/cfnlint/decode/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ class node_class(cls):
def __init__(
self, x, start_mark: Mark | None = None, end_mark: Mark | None = None
):
LOGGER.debug(type(start_mark))
try:
cls.__init__(self, x)
except TypeError:
Expand Down
7 changes: 6 additions & 1 deletion src/cfnlint/template/transforms/_language_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ def _replace_string_params(

new_s = deepcopy(s)
for k, v in params.items():
if isinstance(v, dict):
v = "".join(random.choices(string.ascii_letters, k=_N)) # nosec

Check warning on line 205 in src/cfnlint/template/transforms/_language_extensions.py

View check run for this annotation

Codecov / codecov/patch

src/cfnlint/template/transforms/_language_extensions.py#L205

Added line #L205 was not covered by tests
new_s = re.sub(rf"\$\{{{k}\}}", v, new_s)
new_s = re.sub(rf"\&\{{{k}\}}", re.sub("[^0-9a-zA-Z]+", "", v), new_s)

Expand Down Expand Up @@ -453,6 +455,9 @@ def value(
return [x.strip() for x in allowed_values[0].split(",")]
return allowed_values[0]

if "List" in t:
return [{"Fn::Select": [0, {"Ref": v}]}, {"Fn::Select": [1, {"Ref": v}]}]

raise _ResolveError("Can't resolve Fn::Ref", self._obj)


Expand Down Expand Up @@ -490,7 +495,7 @@ def values(
if values:
if isinstance(values, list):
for value in values:
if isinstance(value, str):
if isinstance(value, (str, dict)):
yield value
else:
raise _ValueError(
Expand Down
41 changes: 40 additions & 1 deletion test/unit/module/template/transforms/test_language_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from cfnlint.template import Template
from cfnlint.template.transforms._language_extensions import (
_ForEach,
_ForEachCollection,
_ForEachValue,
_ForEachValueFnFindInMap,
_ForEachValueRef,
_ResolveError,
_Transform,
_TypeError,
Expand All @@ -27,6 +27,7 @@ def test_valid(self):
_ForEach(
"key", [{"Ref": "Parameter"}, {"Ref": "AWS::NotificationArns"}, {}], {}
)
_ForEach("key", ["AccountId", {"Ref": "AccountIds"}, {}], {})

def test_wrong_type(self):
with self.assertRaises(_TypeError):
Expand Down Expand Up @@ -54,6 +55,32 @@ def test_output_type(self):
_ForEach("key", ["foo", ["bar"], []], {})


class TestForEach(TestCase):
def setUp(self) -> None:
super().setUp()
self.cfn = Template(
"",
{
"Parameters": {
"AccountIds": {
"Type": "CommaDelimitedList",
},
},
},
regions=["us-west-2"],
)

def test_valid(self):
fec = _ForEachCollection({"Ref": "AccountIds"})
self.assertListEqual(
list(fec.values(self.cfn, {})),
[
{"Fn::Select": [0, {"Ref": "AccountIds"}]},
{"Fn::Select": [1, {"Ref": "AccountIds"}]},
],
)


class TestRef(TestCase):
def setUp(self) -> None:
self.template_obj = convert_dict(
Expand All @@ -75,6 +102,9 @@ def setUp(self) -> None:
"Type": "List<AWS::EC2::Subnet::Id>",
"AllowedValues": ["sg-12345678, sg-87654321"],
},
"AccountIds": {
"Type": "CommaDelimitedList",
},
},
}
)
Expand Down Expand Up @@ -115,6 +145,15 @@ def test_ref(self):
fe = _ForEachValue.create({"Ref": "SecurityGroups"})
self.assertEqual(fe.value(self.cfn), ["sg-12345678", "sg-87654321"])

fe = _ForEachValue.create({"Ref": "AccountIds"})
self.assertEqual(
fe.value(self.cfn),
[
{"Fn::Select": [0, {"Ref": "AccountIds"}]},
{"Fn::Select": [1, {"Ref": "AccountIds"}]},
],
)


class TestFindInMap(TestCase):
def setUp(self) -> None:
Expand Down

0 comments on commit 0938fbc

Please sign in to comment.