forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
53 lines (42 loc) · 2.16 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import re
from cfnlint.helpers import LIMITS, REGEX_ALPHANUMERIC
from cfnlint.rules import RuleMatch
def approaching_name_limit(cfn, section):
matches = []
for name in cfn.template.get(section, {}):
if LIMITS['threshold'] * LIMITS[section]['name'] < len(name) <= LIMITS[section]['name']:
message = 'The length of ' + section[:-1] + ' name ({0}) is approaching the limit ({1})'
matches.append(RuleMatch([section, name], message.format(len(name), LIMITS[section]['name'])))
return matches
def approaching_number_limit(cfn, section):
matches = []
number = cfn.get_resources() if section == 'Resources' else cfn.template.get(section, {})
if LIMITS['threshold'] * LIMITS[section]['number'] < len(number) <= LIMITS[section]['number']:
message = 'The number of ' + section + ' ({0}) is approaching the limit ({1})'
matches.append(RuleMatch([section], message.format(len(number), LIMITS[section]['number'])))
return matches
def name_limit(cfn, section):
matches = []
for name in cfn.template.get(section, {}):
if len(name) > LIMITS[section]['name']:
message = 'The length of ' + section[:-1] + ' name ({0}) exceeds the limit ({1})'
matches.append(RuleMatch([section, name], message.format(len(name), LIMITS[section]['name'])))
return matches
def number_limit(cfn, section):
matches = []
number = cfn.get_resources() if section == 'Resources' else cfn.template.get(section, {})
if len(number) > LIMITS[section]['number']:
message = 'The number of ' + section + ' ({0}) exceeds the limit ({1})'
matches.append(RuleMatch([section], message.format(len(number), LIMITS[section]['number'])))
return matches
def alphanumeric_name(cfn, section):
matches = []
for name, _ in cfn.template.get(section, {}).items():
if not re.match(REGEX_ALPHANUMERIC, name):
message = section[:-1] + ' {0} has invalid name. Name has to be alphanumeric.'
matches.append(RuleMatch([section, name], message.format(name)))
return matches