1"""
2Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3SPDX-License-Identifier: MIT-0
4"""
5from cfnlint.rules import CloudFormationLintRule
6from cfnlint.rules import RuleMatch
7from cfnlint.helpers import LIMITS
8
9
10class LimitAttributes(CloudFormationLintRule):
11    """Check maximum Mapping attribute limit"""
12    id = 'I7012'
13    shortdesc = 'Mapping attribute limit'
14    description = 'Check if the amount of Mapping attributes in the template is approaching the upper limit'
15    source_url = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-limits.html'
16    tags = ['mappings', 'limits']
17
18    def match(self, cfn):
19        matches = []
20        for mapping_name, mapping in cfn.template.get('Mappings', {}).items():
21            for mapping_attribute_name, mapping_attribute in mapping.items():
22                path = ['Mappings', mapping_name, mapping_attribute_name]
23                if LIMITS['threshold'] * LIMITS['Mappings']['attributes'] < len(mapping_attribute) <= LIMITS['Mappings']['attributes']:
24                    message = 'The amount of mapping attributes ({0}) is approaching the limit ({1})'
25                    matches.append(RuleMatch(path, message.format(len(mapping_attribute), LIMITS['Mappings']['attributes'])))
26        return matches
27