Skip to content

Commit 30f3297

Browse files
committed
Move to single aws_resource_tags rule
1 parent ecd2aaf commit 30f3297

File tree

2 files changed

+275
-86
lines changed

2 files changed

+275
-86
lines changed

rules/aws_resource_tags.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import (
1717
)
1818

1919
const (
20-
defaultTagsBlockName = "default_tags"
21-
tagsAttributeName = "tags"
22-
tagBlockName = "tag"
23-
providerAttributeName = "provider"
20+
defaultTagsBlockName = "default_tags"
21+
tagsAttributeName = "tags"
22+
tagBlockName = "tag"
23+
providerAttributeName = "provider"
24+
autoScalingGroupResourceName = "aws_autoscaling_group"
2425
)
2526

2627
// AwsResourceTagsRule checks whether resources are tagged with valid values
@@ -32,6 +33,7 @@ type awsResourceTagsRuleConfig struct {
3233
Required []string `hclext:"required,optional"`
3334
Values map[string][]string `hclext:"values,optional"`
3435
Exclude []string `hclext:"exclude,optional"`
36+
Enabled bool `hclext:"enabled,optional"`
3537
}
3638

3739
// awsAutoscalingGroupTag is used by go-cty to evaluate tags in aws_autoscaling_group resources
@@ -283,7 +285,7 @@ func (r *AwsResourceTagsRule) checkAwsAutoScalingGroups(runner tflint.Runner, co
283285
func (r *AwsResourceTagsRule) checkAwsAutoScalingGroupsTag(runner tflint.Runner, resourceBlock *hclext.Block) (map[string]string, hcl.Range, error) {
284286
tags := map[string]string{}
285287

286-
resources, err := runner.GetResourceContent("aws_autoscaling_group", &hclext.BodySchema{
288+
resources, err := runner.GetResourceContent(autoScalingGroupResourceName, &hclext.BodySchema{
287289
Blocks: []hclext.BlockSchema{
288290
{
289291
Type: tagBlockName,
@@ -335,7 +337,7 @@ func (r *AwsResourceTagsRule) checkAwsAutoScalingGroupsTag(runner tflint.Runner,
335337
func (r *AwsResourceTagsRule) checkAwsAutoScalingGroupsTags(runner tflint.Runner, resourceBlock *hclext.Block) (map[string]string, hcl.Range, error) {
336338
tags := map[string]string{}
337339

338-
resources, err := runner.GetResourceContent("aws_autoscaling_group", &hclext.BodySchema{
340+
resources, err := runner.GetResourceContent(autoScalingGroupResourceName, &hclext.BodySchema{
339341
Attributes: []hclext.AttributeSchema{
340342
{Name: tagsAttributeName},
341343
},
@@ -382,19 +384,28 @@ func (r *AwsResourceTagsRule) emitIssue(runner tflint.Runner, tags map[string]st
382384
}
383385
tagsToMatch.Sort()
384386

385-
str := ""
387+
errors := []string{}
388+
389+
// Check the provided tags are valid
386390
for _, tagName := range tagsToMatch {
387391
allowedValues, ok := config.Values[tagName]
388392
// if the tag has a rule configuration then check
389393
if ok {
390394
valueProvided := tags[tagName]
391395
if !slices.Contains(allowedValues, valueProvided) {
392-
str = str + fmt.Sprintf("Received '%s' for tag '%s', expected one of '%s'. ", valueProvided, tagName, strings.Join(allowedValues, ","))
396+
errors = append(errors, fmt.Sprintf("Received '%s' for tag '%s', expected one of '%s'.", valueProvided, tagName, strings.Join(allowedValues, ", ")))
393397
}
394398
}
395399
}
396400

397-
if len(str) > 0 {
398-
runner.EmitIssue(r, strings.TrimSpace(str), location)
401+
// Check all required tags are present
402+
for _, requiredTagName := range config.Required {
403+
if !stringInSlice(requiredTagName, tagsToMatch) {
404+
errors = append(errors, fmt.Sprintf("Tag '%s' is required.", requiredTagName))
405+
}
406+
}
407+
408+
if len(errors) > 0 {
409+
runner.EmitIssue(r, strings.Join(errors, " "), location)
399410
}
400411
}

0 commit comments

Comments
 (0)