Skip to content

Commit dc195e0

Browse files
authored
refactoring limit rules (aws-cloudformation#1558)
1 parent 5b889a3 commit dc195e0

28 files changed

+106
-282
lines changed

src/cfnlint/helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,22 @@
135135
]
136136

137137
LIMITS = {
138-
'mappings': {
138+
'Mappings': {
139139
'number': 100,
140140
'attributes': 64,
141141
'name': 255 # in characters
142142
},
143-
'outputs': {
143+
'Outputs': {
144144
'number': 60,
145145
'name': 255, # in characters
146146
'description': 1024 # in bytes
147147
},
148-
'parameters': {
148+
'Parameters': {
149149
'number': 60,
150150
'name': 255, # in characters
151151
'value': 4096 # in bytes
152152
},
153-
'resources': {
153+
'Resources': {
154154
'number': 200,
155155
'name': 255 # in characters
156156
},

src/cfnlint/rules/limits.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
SPDX-License-Identifier: MIT-0
4+
"""
5+
from cfnlint.helpers import LIMITS
6+
from cfnlint.rules import RuleMatch
7+
8+
9+
def approaching_name_limit(cfn, section):
10+
"""approaching name limit"""
11+
matches = []
12+
for name in cfn.template.get(section, {}):
13+
if LIMITS['threshold'] * LIMITS[section]['name'] < len(name) <= LIMITS[section]['name']:
14+
message = 'The length of ' + section[:-1] + ' name ({0}) is approaching the limit ({1})'
15+
matches.append(RuleMatch([section, name], message.format(len(name), LIMITS[section]['name'])))
16+
return matches
17+
18+
19+
def approaching_number_limit(cfn, section):
20+
"""approaching number limit"""
21+
matches = []
22+
number = cfn.template.get(section, {})
23+
if LIMITS['threshold'] * LIMITS[section]['number'] < len(number) <= LIMITS[section]['number']:
24+
message = 'The number of ' + section + ' ({0}) is approaching the limit ({1})'
25+
matches.append(RuleMatch([section], message.format(len(number), LIMITS[section]['number'])))
26+
return matches
27+
28+
29+
def name_limit(cfn, section):
30+
"""exceeding name limit"""
31+
matches = []
32+
for name in cfn.template.get(section, {}):
33+
if len(name) > LIMITS[section]['name']:
34+
message = 'The length of ' + section[:-1] + ' name ({0}) exceeds the limit ({1})'
35+
matches.append(RuleMatch([section, name], message.format(len(name), LIMITS[section]['name'])))
36+
return matches
37+
38+
39+
def number_limit(cfn, section):
40+
"""exceeding number limit"""
41+
matches = []
42+
number = cfn.template.get(section, {})
43+
if len(number) > LIMITS[section]['number']:
44+
message = 'The number of ' + section + ' ({0}) exceeds the limit ({1})'
45+
matches.append(RuleMatch([section], message.format(len(number), LIMITS[section]['number'])))
46+
return matches

src/cfnlint/rules/mappings/ApproachingLimitAttributes.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,11 @@ class LimitAttributes(CloudFormationLintRule):
1717

1818
def match(self, cfn):
1919
"""Check CloudFormation Mappings"""
20-
2120
matches = []
22-
23-
mappings = cfn.template.get('Mappings', {})
24-
25-
for mapping_name, mapping in mappings.items():
26-
21+
for mapping_name, mapping in cfn.template.get('Mappings', {}).items():
2722
for mapping_attribute_name, mapping_attribute in mapping.items():
2823
path = ['Mappings', mapping_name, mapping_attribute_name]
29-
if LIMITS['threshold'] * LIMITS['mappings']['attributes'] < len(mapping_attribute) <= LIMITS['mappings']['attributes']:
24+
if LIMITS['threshold'] * LIMITS['Mappings']['attributes'] < len(mapping_attribute) <= LIMITS['Mappings']['attributes']:
3025
message = 'The amount of mapping attributes ({0}) is approaching the limit ({1})'
31-
matches.append(RuleMatch(path, message.format(
32-
len(mapping_attribute), LIMITS['mappings']['attributes'])))
33-
26+
matches.append(RuleMatch(path, message.format(len(mapping_attribute), LIMITS['Mappings']['attributes'])))
3427
return matches

src/cfnlint/rules/mappings/ApproachingLimitName.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55
from cfnlint.rules import CloudFormationLintRule
6-
from cfnlint.rules import RuleMatch
7-
from cfnlint.helpers import LIMITS
6+
from cfnlint.rules.limits import approaching_name_limit
87

98

109
class LimitName(CloudFormationLintRule):
@@ -17,16 +16,4 @@ class LimitName(CloudFormationLintRule):
1716

1817
def match(self, cfn):
1918
"""Check CloudFormation Mappings"""
20-
21-
matches = []
22-
23-
mappings = cfn.template.get('Mappings', {})
24-
25-
for mapping_name in mappings:
26-
path = ['Mappings', mapping_name]
27-
if LIMITS['threshold'] * LIMITS['mappings']['name'] < len(mapping_name) <= LIMITS['mappings']['name']:
28-
message = 'The length of mapping name ({0}) is approaching the limit ({1})'
29-
matches.append(RuleMatch(path, message.format(
30-
len(mapping_name), LIMITS['mappings']['name'])))
31-
32-
return matches
19+
return approaching_name_limit(cfn, 'Mappings')

src/cfnlint/rules/mappings/ApproachingLimitNumber.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55
from cfnlint.rules import CloudFormationLintRule
6-
from cfnlint.rules import RuleMatch
7-
from cfnlint.helpers import LIMITS
6+
from cfnlint.rules.limits import approaching_number_limit
87

98

109
class LimitNumber(CloudFormationLintRule):
@@ -17,14 +16,4 @@ class LimitNumber(CloudFormationLintRule):
1716

1817
def match(self, cfn):
1918
"""Check CloudFormation Mappings"""
20-
21-
matches = []
22-
23-
# Check number of mappings against the defined limit
24-
mappings = cfn.template.get('Mappings', {})
25-
if LIMITS['threshold'] * LIMITS['mappings']['number'] < len(mappings) <= LIMITS['mappings']['number']:
26-
message = 'The number of mappings ({0}) is approaching the limit ({1})'
27-
matches.append(RuleMatch(['Mappings'], message.format(
28-
len(mappings), LIMITS['mappings']['number'])))
29-
30-
return matches
19+
return approaching_number_limit(cfn, 'Mappings')

src/cfnlint/rules/mappings/LimitAttributes.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,11 @@ class LimitAttributes(CloudFormationLintRule):
1717

1818
def match(self, cfn):
1919
"""Check CloudFormation Mappings"""
20-
2120
matches = []
22-
23-
mappings = cfn.template.get('Mappings', {})
24-
25-
for mapping_name, mapping in mappings.items():
26-
21+
for mapping_name, mapping in cfn.template.get('Mappings', {}).items():
2722
for mapping_attribute_name, mapping_attribute in mapping.items():
2823
path = ['Mappings', mapping_name, mapping_attribute_name]
29-
if len(mapping_attribute) > LIMITS['mappings']['attributes']:
24+
if len(mapping_attribute) > LIMITS['Mappings']['attributes']:
3025
message = 'The amount of mapping attributes ({0}) exceeds the limit ({1})'
31-
matches.append(RuleMatch(path, message.format(
32-
len(mapping_attribute), LIMITS['mappings']['attributes'])))
33-
26+
matches.append(RuleMatch(path, message.format(len(mapping_attribute), LIMITS['Mappings']['attributes'])))
3427
return matches

src/cfnlint/rules/mappings/LimitName.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55
from cfnlint.rules import CloudFormationLintRule
6-
from cfnlint.rules import RuleMatch
7-
from cfnlint.helpers import LIMITS
6+
from cfnlint.rules.limits import name_limit
87

98

109
class LimitName(CloudFormationLintRule):
@@ -17,16 +16,4 @@ class LimitName(CloudFormationLintRule):
1716

1817
def match(self, cfn):
1918
"""Check CloudFormation Mappings"""
20-
21-
matches = []
22-
23-
mappings = cfn.template.get('Mappings', {})
24-
25-
for mapping_name in mappings:
26-
path = ['Mappings', mapping_name]
27-
if len(mapping_name) > LIMITS['mappings']['name']:
28-
message = 'The length of mapping name ({0}) exceeds the limit ({1})'
29-
matches.append(RuleMatch(path, message.format(
30-
len(mapping_name), LIMITS['mappings']['name'])))
31-
32-
return matches
19+
return name_limit(cfn, 'Mappings')

src/cfnlint/rules/mappings/LimitNumber.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55
from cfnlint.rules import CloudFormationLintRule
6-
from cfnlint.rules import RuleMatch
7-
from cfnlint.helpers import LIMITS
6+
from cfnlint.rules.limits import number_limit
87

98

109
class LimitNumber(CloudFormationLintRule):
@@ -17,14 +16,4 @@ class LimitNumber(CloudFormationLintRule):
1716

1817
def match(self, cfn):
1918
"""Check CloudFormation Mappings"""
20-
21-
matches = []
22-
23-
# Check number of mappings against the defined limit
24-
mappings = cfn.template.get('Mappings', {})
25-
if len(mappings) > LIMITS['mappings']['number']:
26-
message = 'The number of mappings ({0}) exceeds the limit ({1})'
27-
matches.append(RuleMatch(['Mappings'], message.format(
28-
len(mappings), LIMITS['mappings']['number'])))
29-
30-
return matches
19+
return number_limit(cfn, 'Mappings')

src/cfnlint/rules/outputs/ApproachingLimitDescription.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,12 @@ class LimitDescription(CloudFormationLintRule):
1717

1818
def match(self, cfn):
1919
"""Check CloudFormation Outputs"""
20-
2120
matches = []
22-
23-
outputs = cfn.template.get('Outputs', {})
24-
25-
for output_name, output_value in outputs.items():
21+
for output_name, output_value in cfn.template.get('Outputs', {}).items():
2622
description = output_value.get('Description')
2723
if description:
2824
path = ['Outputs', output_name, 'Description']
29-
if LIMITS['threshold'] * LIMITS['outputs']['description'] < len(description) <= LIMITS['outputs']['description']:
25+
if LIMITS['threshold'] * LIMITS['Outputs']['description'] < len(description) <= LIMITS['Outputs']['description']:
3026
message = 'The length of output description ({0}) is approaching the limit ({1})'
31-
matches.append(RuleMatch(path, message.format(
32-
len(description), LIMITS['outputs']['description'])))
33-
27+
matches.append(RuleMatch(path, message.format(len(description), LIMITS['Outputs']['description'])))
3428
return matches

src/cfnlint/rules/outputs/ApproachingLimitName.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
SPDX-License-Identifier: MIT-0
44
"""
55
from cfnlint.rules import CloudFormationLintRule
6-
from cfnlint.rules import RuleMatch
7-
from cfnlint.helpers import LIMITS
6+
from cfnlint.rules.limits import approaching_name_limit
87

98

109
class LimitName(CloudFormationLintRule):
@@ -17,16 +16,4 @@ class LimitName(CloudFormationLintRule):
1716

1817
def match(self, cfn):
1918
"""Check CloudFormation Outputs"""
20-
21-
matches = []
22-
23-
outputs = cfn.template.get('Outputs', {})
24-
25-
for output_name in outputs:
26-
path = ['Outputs', output_name]
27-
if LIMITS['threshold'] * LIMITS['outputs']['name'] < len(output_name) <= LIMITS['outputs']['name']:
28-
message = 'The length of output name ({0}) is approaching the limit ({1})'
29-
matches.append(RuleMatch(path, message.format(
30-
len(output_name), LIMITS['outputs']['name'])))
31-
32-
return matches
19+
return approaching_name_limit(cfn, 'Outputs')

0 commit comments

Comments
 (0)