Skip to content

Commit

Permalink
Update language extensions for empty lists
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong committed Oct 18, 2024
1 parent 0d7df03 commit 35dd48c
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/cfnlint/template/transforms/_language_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,23 @@ def value(
except _ResolveError:
try:
t_map[2].value(cfn, params)
max_length = -1
for k, v in mapping.items():
if isinstance(v, dict):
if t_map[2].value(cfn, params, only_params) in v:
if (
len(v[t_map[2].value(cfn, params, only_params)])
<= max_length
):
continue
if isinstance(t_map[1], _ForEachValueRef):
if t_map[1]._ref._value == "AWS::AccountId":
global _ACCOUNT_ID
_ACCOUNT_ID = k
t_map[1] = _ForEachValue.create(k)
break
max_length = len(
v[t_map[2].value(cfn, params, only_params)]
)
except _ResolveError:
pass

Expand Down Expand Up @@ -529,7 +537,7 @@ def values(
if self._fn:
try:
values = self._fn.value(cfn, {}, False)
if values:
if values is not None:
if isinstance(values, list):
for value in values:
if isinstance(value, (str, dict)):
Expand Down
188 changes: 188 additions & 0 deletions test/unit/module/template/transforms/test_language_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,3 +971,191 @@ def test_transform(self):
self.result,
template,
)


class TestTransformValueEmptyList(TestCase):
def setUp(self) -> None:

cfnlint.template.transforms._language_extensions._ACCOUNT_ID = None

self.template_obj = convert_dict(
{
"Transform": ["AWS::LanguageExtensions"],
"Mappings": {
"Accounts": {
"111111111111": {"AppName": []},
},
},
"Resources": {
"Fn::ForEach::Regions": [
"AppName",
{
"Fn::FindInMap": [
"Accounts",
{"Ref": "AWS::AccountId"},
"AppName",
]
},
{
"${AppName}Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": {"Ref": "AppName"},
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": ["ec2.amazonaws.com"]
},
"Action": ["sts:AssumeRole"],
}
],
},
"Path": "/",
},
}
},
],
},
}
)

self.result = {
"Mappings": {
"Accounts": {
"111111111111": {"AppName": []},
},
},
"Resources": {},
"Transform": ["AWS::LanguageExtensions"],
}

def test_transform(self):
self.maxDiff = None
with mock.patch(
"cfnlint.template.transforms._language_extensions._ACCOUNT_ID", None
):
cfn = Template(
filename="", template=self.template_obj, regions=["us-east-1"]
)
matches, template = language_extension(cfn)
self.assertListEqual(matches, [])
self.assertDictEqual(
template,
self.result,
template,
)


class TestTransformValueOneEmpty(TestCase):
def setUp(self) -> None:
self.template_obj = convert_dict(
{
"Transform": ["AWS::LanguageExtensions"],
"Mappings": {
"Accounts": {
"111111111111": {"AppName": []},
"222222222222": {"AppName": ["C", "D"]},
"333333333333": {"AppName": []},
},
},
"Resources": {
"Fn::ForEach::Regions": [
"AppName",
{
"Fn::FindInMap": [
"Accounts",
{"Ref": "AWS::AccountId"},
"AppName",
]
},
{
"${AppName}Role": {
"Type": "AWS::IAM::Role",
"Properties": {
"RoleName": {"Ref": "AppName"},
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": ["ec2.amazonaws.com"]
},
"Action": ["sts:AssumeRole"],
}
],
},
"Path": "/",
},
}
},
],
},
}
)

self.result = {
"Mappings": {
"Accounts": {
"111111111111": {"AppName": []},
"222222222222": {"AppName": ["C", "D"]},
"333333333333": {"AppName": []},
},
},
"Resources": {
"CRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": ["sts:AssumeRole"],
"Effect": "Allow",
"Principal": {"Service": ["ec2.amazonaws.com"]},
}
],
"Version": "2012-10-17",
},
"Path": "/",
"RoleName": "C",
},
"Type": "AWS::IAM::Role",
},
"DRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": ["sts:AssumeRole"],
"Effect": "Allow",
"Principal": {"Service": ["ec2.amazonaws.com"]},
}
],
"Version": "2012-10-17",
},
"Path": "/",
"RoleName": "D",
},
"Type": "AWS::IAM::Role",
},
},
"Transform": ["AWS::LanguageExtensions"],
}

def test_transform(self):
self.maxDiff = None
with mock.patch(
"cfnlint.template.transforms._language_extensions._ACCOUNT_ID", None
):
cfn = Template(
filename="", template=self.template_obj, regions=["us-east-1"]
)
matches, template = language_extension(cfn)
self.assertListEqual(matches, [])
self.assertDictEqual(
template,
self.result,
template,
)

0 comments on commit 35dd48c

Please sign in to comment.