Skip to content

resource_missing_tags: Ignore non-existent provider aliases #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions rules/aws_resource_missing_tags.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rules

import (
"errors"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -169,21 +168,11 @@ func (r *AwsResourceMissingTagsRule) Check(runner tflint.Runner) error {
// Override the provider alias if defined
if val, ok := resource.Body.Attributes[providerAttributeName]; ok {
provider, diagnostics := aws.DecodeProviderConfigRef(val.Expr, "provider")
providerAlias = provider.Alias

if _, hasProvider := providerTagsMap[providerAlias]; !hasProvider {
errString := fmt.Sprintf(
"The aws provider with alias \"%s\" doesn't exist.",
providerAlias,
)
logger.Error("Error querying provider tags: %s", errString)
return errors.New(errString)
}

if diagnostics.HasErrors() {
logger.Error("error decoding provider: %w", diagnostics)
return diagnostics
}
providerAlias = provider.Alias
}

// If the resource has a tags attribute
Expand Down
59 changes: 35 additions & 24 deletions rules/aws_resource_missing_tags_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rules

import (
"errors"
"testing"

hcl "github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -383,29 +382,6 @@ rule "aws_resource_missing_tags" {
},
},
},
{
Name: "Provider reference no existent",
Content: `provider "aws" {
alias = "zoom"
default_tags {
tags = {
"Foo": "Bar"
}
}
}

resource "aws_instance" "ec2_instance" {
provider = aws.west
instance_type = "t2.micro"
}`,
Config: `
rule "aws_resource_missing_tags" {
enabled = true
tags = ["Foo"]
}`,
Expected: helper.Issues{},
RaiseErr: errors.New("The aws provider with alias \"west\" doesn't exist."),
},
{
Name: "Provider reference existent without tags definition",
Content: `provider "aws" {
Expand Down Expand Up @@ -514,6 +490,41 @@ rule "aws_resource_missing_tags" {
}`,
Expected: helper.Issues{},
},
{
// In child modules, the provider declarations are passed implicitly or explicitly from the root module.
// In this case, it is not possible to refer to the provider's default_tags.
// Here, the strategy is to ignore default_tags rather than skip inspection of the tags.
Name: "provider aliases within child modules",
Content: `
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
configuration_aliases = [ aws.foo ]
}
}
}

resource "aws_instance" "ec2_instance" {
provider = aws.foo
}`,
Config: `
rule "aws_resource_missing_tags" {
enabled = true
tags = ["Foo", "Bar"]
}`,
Expected: helper.Issues{
{
Rule: NewAwsResourceMissingTagsRule(),
Message: "The resource is missing the following tags: \"Bar\", \"Foo\".",
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{Line: 11, Column: 1},
End: hcl.Pos{Line: 11, Column: 39},
},
},
},
},
}

rule := NewAwsResourceMissingTagsRule()
Expand Down