Skip to content

Commit 0f212c5

Browse files
authored
Ignore non-existent provider aliases (#519)
1 parent 5406ce5 commit 0f212c5

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

rules/aws_resource_missing_tags.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package rules
22

33
import (
4-
"errors"
54
"fmt"
65
"sort"
76
"strings"
@@ -169,21 +168,11 @@ func (r *AwsResourceMissingTagsRule) Check(runner tflint.Runner) error {
169168
// Override the provider alias if defined
170169
if val, ok := resource.Body.Attributes[providerAttributeName]; ok {
171170
provider, diagnostics := aws.DecodeProviderConfigRef(val.Expr, "provider")
172-
providerAlias = provider.Alias
173-
174-
if _, hasProvider := providerTagsMap[providerAlias]; !hasProvider {
175-
errString := fmt.Sprintf(
176-
"The aws provider with alias \"%s\" doesn't exist.",
177-
providerAlias,
178-
)
179-
logger.Error("Error querying provider tags: %s", errString)
180-
return errors.New(errString)
181-
}
182-
183171
if diagnostics.HasErrors() {
184172
logger.Error("error decoding provider: %w", diagnostics)
185173
return diagnostics
186174
}
175+
providerAlias = provider.Alias
187176
}
188177

189178
// If the resource has a tags attribute

rules/aws_resource_missing_tags_test.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package rules
22

33
import (
4-
"errors"
54
"testing"
65

76
hcl "github.com/hashicorp/hcl/v2"
@@ -383,29 +382,6 @@ rule "aws_resource_missing_tags" {
383382
},
384383
},
385384
},
386-
{
387-
Name: "Provider reference no existent",
388-
Content: `provider "aws" {
389-
alias = "zoom"
390-
default_tags {
391-
tags = {
392-
"Foo": "Bar"
393-
}
394-
}
395-
}
396-
397-
resource "aws_instance" "ec2_instance" {
398-
provider = aws.west
399-
instance_type = "t2.micro"
400-
}`,
401-
Config: `
402-
rule "aws_resource_missing_tags" {
403-
enabled = true
404-
tags = ["Foo"]
405-
}`,
406-
Expected: helper.Issues{},
407-
RaiseErr: errors.New("The aws provider with alias \"west\" doesn't exist."),
408-
},
409385
{
410386
Name: "Provider reference existent without tags definition",
411387
Content: `provider "aws" {
@@ -514,6 +490,41 @@ rule "aws_resource_missing_tags" {
514490
}`,
515491
Expected: helper.Issues{},
516492
},
493+
{
494+
// In child modules, the provider declarations are passed implicitly or explicitly from the root module.
495+
// In this case, it is not possible to refer to the provider's default_tags.
496+
// Here, the strategy is to ignore default_tags rather than skip inspection of the tags.
497+
Name: "provider aliases within child modules",
498+
Content: `
499+
terraform {
500+
required_providers {
501+
aws = {
502+
source = "hashicorp/aws"
503+
configuration_aliases = [ aws.foo ]
504+
}
505+
}
506+
}
507+
508+
resource "aws_instance" "ec2_instance" {
509+
provider = aws.foo
510+
}`,
511+
Config: `
512+
rule "aws_resource_missing_tags" {
513+
enabled = true
514+
tags = ["Foo", "Bar"]
515+
}`,
516+
Expected: helper.Issues{
517+
{
518+
Rule: NewAwsResourceMissingTagsRule(),
519+
Message: "The resource is missing the following tags: \"Bar\", \"Foo\".",
520+
Range: hcl.Range{
521+
Filename: "module.tf",
522+
Start: hcl.Pos{Line: 11, Column: 1},
523+
End: hcl.Pos{Line: 11, Column: 39},
524+
},
525+
},
526+
},
527+
},
517528
}
518529

519530
rule := NewAwsResourceMissingTagsRule()

0 commit comments

Comments
 (0)