Skip to content

Commit 07a8f8c

Browse files
authored
terraform_required_providers: fix regression with configuration_aliases (#1454)
* add failing test * add variable with provider name when evaluating required provider
1 parent 10be233 commit 07a8f8c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

rules/terraformrules/terraform_required_providers.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66

7+
"github.com/hashicorp/hcl/v2"
78
tfaddr "github.com/hashicorp/terraform-registry-address"
89
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
910
sdk "github.com/terraform-linters/tflint-plugin-sdk/tflint"
@@ -126,7 +127,14 @@ func (r *TerraformRequiredProvidersRule) Check(runner *tflint.Runner) error {
126127
continue
127128
}
128129

129-
val, diags := provider.Expr.Value(nil)
130+
val, diags := provider.Expr.Value(&hcl.EvalContext{
131+
Variables: map[string]cty.Value{
132+
// configuration_aliases can declare additional provider instances
133+
// required provider "foo" could have: configuration_aliases = [foo.a, foo.b]
134+
// @see https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules
135+
name: cty.DynamicVal,
136+
},
137+
})
130138
if diags.HasErrors() {
131139
return diags
132140
}

rules/terraformrules/terraform_required_providers_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,26 @@ provider "template" {
205205
},
206206
},
207207
},
208+
{
209+
Name: "version set with configuration_aliases",
210+
Content: `
211+
terraform {
212+
required_providers {
213+
template = {
214+
source = "hashicorp/template"
215+
version = "~> 2"
216+
217+
configuration_aliases = [template.alias]
218+
}
219+
}
220+
}
221+
222+
data "template_file" "foo" {
223+
provider = template.alias
224+
}
225+
`,
226+
Expected: tflint.Issues{},
227+
},
208228
{
209229
Name: "version set with alias",
210230
Content: `

0 commit comments

Comments
 (0)