Skip to content

Commit

Permalink
Support new language extension foreach capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Jan 23, 2024
1 parent ff1d1d5 commit 5c8b949
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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

0 comments on commit 5c8b949

Please sign in to comment.