Skip to content

Commit

Permalink
Fix issues caused by multiple terraform blocks
Browse files Browse the repository at this point in the history
See also #205

Some rules that reference terraform blocks do not assume that
blocks can be defined multiple times and can lead to inconsistent results in that case.
  • Loading branch information
wata727 committed Oct 16, 2024
1 parent d6b8c4c commit 0b0ab60
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
4 changes: 3 additions & 1 deletion rules/terraform_required_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ func (r *TerraformRequiredProvidersRule) Check(rr tflint.Runner) error {
requiredProviders := hclext.Attributes{}
for _, terraform := range body.Blocks {
for _, requiredProvidersBlock := range terraform.Body.Blocks {
requiredProviders = requiredProvidersBlock.Body.Attributes
for name, attr := range requiredProvidersBlock.Body.Attributes {
requiredProviders[name] = attr
}
}
}

Expand Down
102 changes: 102 additions & 0 deletions rules/terraform_required_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,108 @@ output "foo" {
},
},
},
{
Name: "multiple required providers",
Content: `
terraform {
required_providers {
template = "~> 2"
}
required_providers {
aws = "~> 5.0"
}
}
provider "template" {}
provider "aws" {}
provider "google" {}
terraform {
required_providers {
google = "~> 6.0"
}
}
`,
Expected: helper.Issues{
// FIXME: ignore order of issues
{
Rule: NewTerraformRequiredProvidersRule(),
Message: "Legacy version constraint for provider \"template\" in `required_providers`",
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 4,
Column: 16,
},
End: hcl.Pos{
Line: 4,
Column: 22,
},
},
},
{
Rule: NewTerraformRequiredProvidersRule(),
Message: "Legacy version constraint for provider \"aws\" in `required_providers`",
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 8,
Column: 11,
},
End: hcl.Pos{
Line: 8,
Column: 19,
},
},
},
{
Rule: NewTerraformRequiredProvidersRule(),
Message: "Legacy version constraint for provider \"google\" in `required_providers`",
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{
Line: 18,
Column: 14,
},
End: hcl.Pos{
Line: 18,
Column: 22,
},
},
},
},
Fixed: `
terraform {
required_providers {
template = {
source = "hashicorp/template"
version = "~> 2"
}
}
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "template" {}
provider "aws" {}
provider "google" {}
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 6.0"
}
}
}
`,
},
}

rule := NewTerraformRequiredProvidersRule()
Expand Down
1 change: 1 addition & 0 deletions rules/terraform_unused_required_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (r *TerraformUnusedRequiredProvidersRule) Check(rr tflint.Runner) error {

for _, block := range content.Blocks {
var attrDiags hcl.Diagnostics
// FIXME: required providers should be merged
requiredProviders, attrDiags = block.Body.JustAttributes()
diags = diags.Extend(attrDiags)
if diags.HasErrors() {
Expand Down
1 change: 1 addition & 0 deletions rules/terraform_workspace_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (r *TerraformWorkspaceRemoteRule) Check(runner tflint.Runner) error {
for _, terraform := range body.Blocks {
for _, requiredVersion := range terraform.Body.Attributes {
err := runner.EvaluateExpr(requiredVersion.Expr, func(v string) error {
// FIXME: Should constraints defined in multiple blocks be merged?
constraints, err := version.NewConstraint(v)
if err != nil {
return err
Expand Down

0 comments on commit 0b0ab60

Please sign in to comment.