@@ -17,10 +17,11 @@ import (
17
17
)
18
18
19
19
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"
24
25
)
25
26
26
27
// AwsResourceTagsRule checks whether resources are tagged with valid values
@@ -32,6 +33,7 @@ type awsResourceTagsRuleConfig struct {
32
33
Required []string `hclext:"required,optional"`
33
34
Values map [string ][]string `hclext:"values,optional"`
34
35
Exclude []string `hclext:"exclude,optional"`
36
+ Enabled bool `hclext:"enabled,optional"`
35
37
}
36
38
37
39
// 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
283
285
func (r * AwsResourceTagsRule ) checkAwsAutoScalingGroupsTag (runner tflint.Runner , resourceBlock * hclext.Block ) (map [string ]string , hcl.Range , error ) {
284
286
tags := map [string ]string {}
285
287
286
- resources , err := runner .GetResourceContent ("aws_autoscaling_group" , & hclext.BodySchema {
288
+ resources , err := runner .GetResourceContent (autoScalingGroupResourceName , & hclext.BodySchema {
287
289
Blocks : []hclext.BlockSchema {
288
290
{
289
291
Type : tagBlockName ,
@@ -335,7 +337,7 @@ func (r *AwsResourceTagsRule) checkAwsAutoScalingGroupsTag(runner tflint.Runner,
335
337
func (r * AwsResourceTagsRule ) checkAwsAutoScalingGroupsTags (runner tflint.Runner , resourceBlock * hclext.Block ) (map [string ]string , hcl.Range , error ) {
336
338
tags := map [string ]string {}
337
339
338
- resources , err := runner .GetResourceContent ("aws_autoscaling_group" , & hclext.BodySchema {
340
+ resources , err := runner .GetResourceContent (autoScalingGroupResourceName , & hclext.BodySchema {
339
341
Attributes : []hclext.AttributeSchema {
340
342
{Name : tagsAttributeName },
341
343
},
@@ -382,19 +384,28 @@ func (r *AwsResourceTagsRule) emitIssue(runner tflint.Runner, tags map[string]st
382
384
}
383
385
tagsToMatch .Sort ()
384
386
385
- str := ""
387
+ errors := []string {}
388
+
389
+ // Check the provided tags are valid
386
390
for _ , tagName := range tagsToMatch {
387
391
allowedValues , ok := config .Values [tagName ]
388
392
// if the tag has a rule configuration then check
389
393
if ok {
390
394
valueProvided := tags [tagName ]
391
395
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 , ", " ) ))
393
397
}
394
398
}
395
399
}
396
400
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 )
399
410
}
400
411
}
0 commit comments