Skip to content
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
32 changes: 32 additions & 0 deletions terraform/context_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,38 @@ func TestContext2Validate_providerConfig_good(t *testing.T) {
}
}

// In this test there is a mismatch between the provider's fqn (hashicorp/test)
// and it's local name set in required_providers (arbitrary).
func TestContext2Validate_requiredProviderConfig(t *testing.T) {
m := testModule(t, "validate-required-provider-config")
p := testProvider("aws")

p.GetSchemaReturn = &ProviderSchema{
Provider: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"required_attribute": {Type: cty.String, Required: true},
},
},
ResourceTypes: map[string]*configschema.Block{
"aws_instance": {
Attributes: map[string]*configschema.Attribute{},
},
},
}

c := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
},
})

diags := c.Validate()
if diags.HasErrors() {
t.Fatalf("unexpected error: %s", diags.Err())
}
}

func TestContext2Validate_provisionerConfig_bad(t *testing.T) {
m := testModule(t, "validate-bad-prov-conf")
p := testProvider("aws")
Expand Down
20 changes: 20 additions & 0 deletions terraform/testdata/validate-required-provider-config/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This test verifies that the provider local name, local config and fqn map
# together properly when the local name does not match the type.

terraform {
required_providers {
arbitrary = {
source = "hashicorp/aws"
}
}
}

# hashicorp/test has required provider config attributes. This "arbitrary"
# provider configuration block should map to hashicorp/test.
provider "arbitrary" {
required_attribute = "bloop"
}

resource "aws_instance" "test" {
provider = "arbitrary"
}
5 changes: 4 additions & 1 deletion terraform/transform_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,9 +721,12 @@ func (t *ProviderConfigTransformer) attachProviderConfigs(g *Graph) error {
continue
}

// Find the localName for the provider fqn
localName := mc.Module.LocalNameForProvider(addr.Provider)

// Go through the provider configs to find the matching config
for _, p := range mc.Module.ProviderConfigs {
if p.Name == addr.Provider.Type && p.Alias == addr.Alias {
if p.Name == localName && p.Alias == addr.Alias {
log.Printf("[TRACE] ProviderConfigTransformer: attaching to %q provider configuration from %s", dag.VertexName(v), p.DeclRange)
apn.AttachProvider(p)
break
Expand Down