-
Notifications
You must be signed in to change notification settings - Fork 606
/
Copy pathUsingMerge.py
48 lines (40 loc) · 1.61 KB
/
UsingMerge.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
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
from __future__ import annotations
from typing import Any
from cfnlint.rules import CloudFormationLintRule, RuleMatch
from cfnlint.template import Template
class UsingMerge(CloudFormationLintRule):
id = "W1100"
shortdesc = "Validate if the template is using YAML merge"
description = (
"The CloudFormation service does not support YAML anchors, "
"aliases, or merging. This rule validates if the "
"merge capability is being used"
)
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-formats.html"
tags = ["yaml"]
def _nested_obj(self, obj: Any, path: list[str | int]) -> list[RuleMatch]:
matches = []
if isinstance(obj, dict):
if hasattr(obj, "using_merge") and obj.using_merge:
matches.append(
RuleMatch(
path=path,
message=(
"This code is using yaml marge capabilities "
"and can only be deployed using the "
"'package' cli command"
),
)
)
for k, v in obj.items():
matches.extend(self._nested_obj(v, path + [k]))
elif isinstance(obj, list):
for i, e in enumerate(obj):
matches.extend(self._nested_obj(e, path + [i]))
return matches
def match(self, cfn: Template):
return self._nested_obj(cfn.template, [])