forked from aws-cloudformation/cfn-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForEach.py
53 lines (43 loc) · 1.76 KB
/
ForEach.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 logging
from cfnlint._typing import RuleMatches
from cfnlint.rules import CloudFormationLintRule, RuleMatch
from cfnlint.template import Template
LOGGER = logging.getLogger("cfnlint")
class ForEach(CloudFormationLintRule):
id = "E1032"
shortdesc = "Validates ForEach functions"
description = "Validates that ForEach parameters have a valid configuration"
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html"
tags = ["functions", "foreach"]
def validate_transform_is_declared(
self, has_language_extensions_transform, tree, intrinsic_function
):
matches = []
if not has_language_extensions_transform:
message = (
"Missing Transform: Declare the AWS::LanguageExtensions Transform"
" globally to enable use of the intrinsic function "
+ intrinsic_function
+ " at {0}"
)
matches.append(RuleMatch(tree[:], message.format("/".join(map(str, tree)))))
return matches
# pylint: disable=unused-argument
def match(self, cfn: Template) -> RuleMatches:
matches = []
intrinsic_function = "Fn::ForEach"
for_eaches = cfn.transform_pre["Fn::ForEach"]
for for_each in for_eaches:
has_language_extensions_transform = cfn.has_language_extensions_transform()
matches.extend(
self.validate_transform_is_declared(
has_language_extensions_transform,
for_each[:-1],
intrinsic_function,
)
)
return matches