@@ -28,6 +28,7 @@ type awsResourceTagsRuleConfig struct {
28
28
}
29
29
30
30
const (
31
+ defaultTagsBlockName = "default_tags"
31
32
tagsAttributeName = "tags"
32
33
tagBlockName = "tag"
33
34
providerAttributeName = "provider"
@@ -68,7 +69,7 @@ func (r *AwsResourceMissingTagsRule) getProviderLevelTags(runner tflint.Runner)
68
69
},
69
70
Blocks : []hclext.BlockSchema {
70
71
{
71
- Type : "default_tags" ,
72
+ Type : defaultTagsBlockName ,
72
73
Body : & hclext.BodySchema {Attributes : []hclext.AttributeSchema {{Name : tagsAttributeName }}},
73
74
},
74
75
},
@@ -89,8 +90,9 @@ func (r *AwsResourceMissingTagsRule) getProviderLevelTags(runner tflint.Runner)
89
90
providerAlias = "default"
90
91
} else {
91
92
err := runner .EvaluateExpr (providerAttr .Expr , func (alias string ) error {
93
+ logger .Debug ("Walk `%s` provider" , providerAlias )
92
94
providerAlias = alias
93
- // Init the provider reference even if it doesn't have tags
95
+ // Init the provider reference even if it doesn't have tags
94
96
allProviderTags [alias ] = nil
95
97
return nil
96
98
}, nil )
@@ -106,7 +108,22 @@ func (r *AwsResourceMissingTagsRule) getProviderLevelTags(runner tflint.Runner)
106
108
continue
107
109
}
108
110
109
- err := runner .EvaluateExpr (attr .Expr , func (tags map [string ]string ) error {
111
+ err := runner .EvaluateExpr (attr .Expr , func (val cty.Value ) error {
112
+ // Skip unknown values
113
+ if ! val .IsKnown () || val .IsNull () {
114
+ logger .Warn ("The missing aws tags rule can only evaluate provided variables, skipping %s." , provider .Labels [0 ]+ "." + providerAlias + "." + defaultTagsBlockName + "." + tagsAttributeName )
115
+ return nil
116
+ }
117
+
118
+ valMap := val .AsValueMap ()
119
+ var tags map [string ]string = make (map [string ]string , len (valMap ))
120
+ for k , v := range valMap {
121
+ tags [k ] = ""
122
+ if ! v .IsNull () && v .Type ().FriendlyName () == "string" {
123
+ tags [k ] = v .AsString ()
124
+ }
125
+ }
126
+ logger .Debug ("Walk `%s` provider with tags `%v`" , providerAlias , tags )
110
127
providerTags = tags
111
128
return nil
112
129
}, nil )
@@ -188,17 +205,27 @@ func (r *AwsResourceMissingTagsRule) Check(runner tflint.Runner) error {
188
205
"Walk `%s` attribute" ,
189
206
resource .Labels [0 ]+ "." + resource .Labels [1 ]+ "." + tagsAttributeName ,
190
207
)
191
- // Since the evlaluateExpr, overrides k/v pairs, we need to re-copy the tags
192
- resourceTagsAux := make (map [string ]string )
193
208
194
- err := runner .EvaluateExpr (attribute .Expr , func (val map [string ]string ) error {
195
- resourceTagsAux = val
209
+ err := runner .EvaluateExpr (attribute .Expr , func (val cty.Value ) error {
210
+ // Skip unknown values
211
+ if ! val .IsKnown () || val .IsNull () {
212
+ logger .Warn ("The missing aws tags rule can only evaluate provided variables, skipping %s." , resource .Labels [0 ]+ "." + resource .Labels [1 ]+ "." + tagsAttributeName )
213
+ return nil
214
+ }
215
+ valMap := val .AsValueMap ()
216
+ // Since the evlaluateExpr, overrides k/v pairs, we need to re-copy the tags
217
+ var evaluatedTags map [string ]string = make (map [string ]string , len (valMap ))
218
+ for k , v := range valMap {
219
+ evaluatedTags [k ] = ""
220
+ if ! v .IsNull () && v .Type ().FriendlyName () == "string" {
221
+ evaluatedTags [k ] = v .AsString ()
222
+ }
223
+ }
224
+ maps .Copy (resourceTags , evaluatedTags )
225
+ r .emitIssue (runner , resourceTags , config , attribute .Expr .Range ())
196
226
return nil
197
227
}, nil )
198
228
199
- maps .Copy (resourceTags , resourceTagsAux )
200
- r .emitIssue (runner , resourceTags , config , attribute .Expr .Range ())
201
-
202
229
if err != nil {
203
230
return err
204
231
}
0 commit comments