Skip to content

Commit 500e912

Browse files
authored
fix: Fixed the error 'Provider doesn't exist' and the provider exist (#508)
1 parent 4f62a14 commit 500e912

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

rules/aws_resource_missing_tags.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,24 @@ func (r *AwsResourceMissingTagsRule) getProviderLevelTags(runner tflint.Runner)
8383
allProviderTags := make(map[string]map[string]string)
8484
var providerAlias string
8585
for _, provider := range providerBody.Blocks.OfType(providerAttributeName) {
86-
providerTags := make(map[string]string)
86+
// Get the alias attribute, in terraform when there is a single aws provider its called "default"
87+
providerAttr, ok := provider.Body.Attributes["alias"]
88+
if !ok {
89+
providerAlias = "default"
90+
} else {
91+
err := runner.EvaluateExpr(providerAttr.Expr, func(alias string) error {
92+
providerAlias = alias
93+
// Init the provider reference even if it doesn't have tags
94+
allProviderTags[alias] = nil
95+
return nil
96+
}, nil)
97+
if err != nil {
98+
return nil, err
99+
}
100+
}
101+
87102
for _, block := range provider.Body.Blocks {
103+
providerTags := make(map[string]string)
88104
attr, ok := block.Body.Attributes[tagsAttributeName]
89105
if !ok {
90106
continue
@@ -99,22 +115,7 @@ func (r *AwsResourceMissingTagsRule) getProviderLevelTags(runner tflint.Runner)
99115
return nil, err
100116
}
101117

102-
// Get the alias attribute, in terraform when there is a single aws provider its called "default"
103-
providerAttr, ok := provider.Body.Attributes["alias"]
104-
if !ok {
105-
providerAlias = "default"
106-
allProviderTags[providerAlias] = providerTags
107-
} else {
108-
err := runner.EvaluateExpr(providerAttr.Expr, func(alias string) error {
109-
providerAlias = alias
110-
return nil
111-
}, nil)
112-
// Assign default provider
113-
allProviderTags[providerAlias] = providerTags
114-
if err != nil {
115-
return nil, err
116-
}
117-
}
118+
allProviderTags[providerAlias] = providerTags
118119
}
119120
}
120121
return allProviderTags, nil

rules/aws_resource_missing_tags_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,29 @@ rule "aws_resource_missing_tags" {
406406
Expected: helper.Issues{},
407407
RaiseErr: errors.New("The aws provider with alias \"west\" doesn't exist."),
408408
},
409+
{
410+
Name: "Provider reference existent without tags definition",
411+
Content: `provider "aws" {
412+
alias = "west"
413+
region = "us-west-2"
414+
}
415+
416+
resource "aws_ssm_parameter" "param" {
417+
provider = aws.west
418+
name = "test"
419+
type = "String"
420+
value = "test"
421+
tags = {
422+
Foo = "Bar"
423+
}
424+
}`,
425+
Config: `
426+
rule "aws_resource_missing_tags" {
427+
enabled = true
428+
tags = ["Foo"]
429+
}`,
430+
Expected: helper.Issues{},
431+
},
409432
}
410433

411434
rule := NewAwsResourceMissingTagsRule()

0 commit comments

Comments
 (0)