Skip to content

Commit ca61e4e

Browse files
committed
terraform: fix ProviderConfigTransformer
The ProviderConfigTransformer was using only the provider FQN to attach a provider configuration to the provider, but what it needs to do is find the local name for the given provider FQN (which may not match the type name) and use that when searching for matching provider configuration. Fixes #26556 This will also be backported to the v0.13 branch.
1 parent 8a4c493 commit ca61e4e

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

terraform/context_validate_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,38 @@ func TestContext2Validate_providerConfig_good(t *testing.T) {
650650
}
651651
}
652652

653+
// In this test there is a mismatch between the provider's fqn (hashicorp/test)
654+
// and it's local name set in required_providers (arbitrary).
655+
func TestContext2Validate_requiredProviderConfig(t *testing.T) {
656+
m := testModule(t, "validate-required-provider-config")
657+
p := testProvider("aws")
658+
659+
p.GetSchemaReturn = &ProviderSchema{
660+
Provider: &configschema.Block{
661+
Attributes: map[string]*configschema.Attribute{
662+
"required_attribute": {Type: cty.String, Required: true},
663+
},
664+
},
665+
ResourceTypes: map[string]*configschema.Block{
666+
"aws_instance": {
667+
Attributes: map[string]*configschema.Attribute{},
668+
},
669+
},
670+
}
671+
672+
c := testContext2(t, &ContextOpts{
673+
Config: m,
674+
Providers: map[addrs.Provider]providers.Factory{
675+
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
676+
},
677+
})
678+
679+
diags := c.Validate()
680+
if diags.HasErrors() {
681+
t.Fatalf("unexpected error: %s", diags.Err())
682+
}
683+
}
684+
653685
func TestContext2Validate_provisionerConfig_bad(t *testing.T) {
654686
m := testModule(t, "validate-bad-prov-conf")
655687
p := testProvider("aws")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This test verifies that the provider local name, local config and fqn map
2+
# together properly when the local name does not match the type.
3+
4+
terraform {
5+
required_providers {
6+
arbitrary = {
7+
source = "hashicorp/aws"
8+
}
9+
}
10+
}
11+
12+
# hashicorp/test has required provider config attributes. This "arbitrary"
13+
# provider configuration block should map to hashicorp/test.
14+
provider "arbitrary" {
15+
required_attribute = "bloop"
16+
}
17+
18+
resource "aws_instance" "test" {
19+
provider = "arbitrary"
20+
}

terraform/transform_provider.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,12 @@ func (t *ProviderConfigTransformer) attachProviderConfigs(g *Graph) error {
721721
continue
722722
}
723723

724+
// Find the localName for the provider fqn
725+
localName := mc.Module.LocalNameForProvider(addr.Provider)
726+
724727
// Go through the provider configs to find the matching config
725728
for _, p := range mc.Module.ProviderConfigs {
726-
if p.Name == addr.Provider.Type && p.Alias == addr.Alias {
729+
if p.Name == localName && p.Alias == addr.Alias {
727730
log.Printf("[TRACE] ProviderConfigTransformer: attaching to %q provider configuration from %s", dag.VertexName(v), p.DeclRange)
728731
apn.AttachProvider(p)
729732
break

0 commit comments

Comments
 (0)