Skip to content

Commit

Permalink
Fix tagging examples
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-k committed Jun 28, 2024
1 parent 625d38a commit 1c1389f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
26 changes: 10 additions & 16 deletions examples/rules/PropertiesTagsIncluded.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from cfnlint.rules import CloudFormationLintRule, RuleMatch
from cfnlint.schema import PROVIDER_SCHEMA_MANAGER


class PropertiesTagsIncluded(CloudFormationLintRule):
Expand All @@ -14,33 +15,26 @@ 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"""

matches = []

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(
Expand Down
8 changes: 7 additions & 1 deletion examples/rules/PropertiesTagsRequired.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SPDX-License-Identifier: MIT-0
"""

from cfnlint.decode.node import dict_node, list_node
from cfnlint.rules import CloudFormationLintRule, RuleMatch


Expand All @@ -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}"
Expand Down

0 comments on commit 1c1389f

Please sign in to comment.