From abe36914755ad89b58677c8523f60e3e15281f56 Mon Sep 17 00:00:00 2001 From: Michael K Date: Fri, 28 Jun 2024 23:04:34 +0200 Subject: [PATCH] Fix tagging examples (#3448) --- examples/rules/PropertiesTagsIncluded.py | 26 +++++++++--------------- examples/rules/PropertiesTagsRequired.py | 8 +++++++- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/examples/rules/PropertiesTagsIncluded.py b/examples/rules/PropertiesTagsIncluded.py index 3ee1c8be43..f3d9b53a61 100644 --- a/examples/rules/PropertiesTagsIncluded.py +++ b/examples/rules/PropertiesTagsIncluded.py @@ -4,6 +4,7 @@ """ from cfnlint.rules import CloudFormationLintRule, RuleMatch +from cfnlint.schema import PROVIDER_SCHEMA_MANAGER class PropertiesTagsIncluded(CloudFormationLintRule): @@ -14,20 +15,6 @@ class PropertiesTagsIncluded(CloudFormationLintRule): description = "Check Tags for resources" tags = ["resources", "tags"] - def get_resources_with_tags(self, region): - """Get resource types that support tags""" - resourcespecs = {} - resourcetypes = resourcespecs["ResourceTypes"] - - matches = [] - for resourcetype, resourceobj in resourcetypes.items(): - propertiesobj = resourceobj.get("Properties") - if propertiesobj: - if "Tags" in propertiesobj: - matches.append(resourcetype) - - return matches - def match(self, cfn): """Check Tags for required keys""" @@ -35,12 +22,19 @@ def match(self, cfn): all_tags = cfn.search_deep_keys("Tags") all_tags = [x for x in all_tags if x[0] == "Resources"] - resources_tags = self.get_resources_with_tags(cfn.regions[0]) resources = cfn.get_resources() for resource_name, resource_obj in resources.items(): resource_type = resource_obj.get("Type", "") resource_properties = resource_obj.get("Properties", {}) - if resource_type in resources_tags: + if any( + "Tags" in schema.schema["properties"] + for ( + _region, + schema, + ) in PROVIDER_SCHEMA_MANAGER.get_resource_schemas_by_regions( + resource_type, cfn.regions + ) + ): if "Tags" not in resource_properties: message = "Missing Tags Properties for {0}" matches.append( diff --git a/examples/rules/PropertiesTagsRequired.py b/examples/rules/PropertiesTagsRequired.py index 2d7a0a32f7..b62c7a098b 100644 --- a/examples/rules/PropertiesTagsRequired.py +++ b/examples/rules/PropertiesTagsRequired.py @@ -3,6 +3,7 @@ SPDX-License-Identifier: MIT-0 """ +from cfnlint.decode.node import dict_node, list_node from cfnlint.rules import CloudFormationLintRule, RuleMatch @@ -24,7 +25,12 @@ def match(self, cfn): all_tags = cfn.search_deep_keys("Tags") all_tags = [x for x in all_tags if x[0] == "Resources"] for all_tag in all_tags: - all_keys = [d.get("Key") for d in all_tag[-1]] + if isinstance(all_tag[-1], list_node): + all_keys = [d.get("Key") for d in all_tag[-1]] + elif isinstance(all_tag[-1], dict_node): + all_keys = all_tag[-1].keys() + else: + continue for required_tag in required_tags: if required_tag not in all_keys: message = "Missing Tag {0} at {1}"