Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new language extension foreach capabilities #3033

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cfnlint/template/transforms/_language_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,14 @@ def _walk(self, item: Any, params: MutableMapping[str, Any], cfn: Any):
def _replace_string_params(
self, s: str, params: Mapping[str, Any]
) -> Tuple[bool, str]:
pattern = r"\${[a-zA-Z0-9\.:]+}"
pattern = r"(\$|&){[a-zA-Z0-9\.:]+}"
if not re.search(pattern, s):
return (True, s)

new_s = deepcopy(s)
for k, v in params.items():
new_s = re.sub(rf"\$\{{{k}\}}", v, new_s)
new_s = re.sub(rf"\&\{{{k}\}}", re.sub("[^0-9a-zA-Z]+", "", v), new_s)

if isinstance(s, str_node):
new_s = str_node(new_s, s.start_mark, s.end_mark)
Expand Down
31 changes: 29 additions & 2 deletions test/unit/module/template/transforms/test_language_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,21 @@ def setUp(self) -> None:
},
}
},
]
],
"Fn::ForEach::SpecialCharacters": [
"Identifier",
["a-b", "c-d"],
{
"S3Bucket&{Identifier}": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {
"Fn::Sub": "bucket-name-&{Identifier}"
},
},
}
},
],
},
"Outputs": {
"Fn::ForEach::BucketOutputs": [
Expand Down Expand Up @@ -318,6 +332,18 @@ def setUp(self) -> None:
},
"Type": "AWS::S3::Bucket",
},
"S3Bucketab": {
"Properties": {
"BucketName": "bucket-name-ab",
},
"Type": "AWS::S3::Bucket",
},
"S3Bucketcd": {
"Properties": {
"BucketName": "bucket-name-cd",
},
"Type": "AWS::S3::Bucket",
},
},
"Transforms": ["AWS::LanguageExtensions"],
}
Expand All @@ -331,6 +357,7 @@ def test_transform(self):
self.assertDictEqual(
template,
self.result,
template,
)

def test_transform_findinmap_function(self):
Expand Down Expand Up @@ -373,7 +400,7 @@ def test_bad_collection_ref(self):
cfn = Template(filename="", template=template_obj, regions=["us-east-1"])
matches, template = language_extension(cfn)
self.assertListEqual(matches, [])
self.assertTrue(len(template["Resources"]) == 2)
self.assertTrue(len(template["Resources"]) == 4)
self.assertTrue("S3BucketA" in template["Resources"])

def test_duplicate_key(self):
Expand Down