Skip to content
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

jsonconfig: fix keys for default providers #30525

Merged
merged 3 commits into from Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 46 additions & 16 deletions internal/command/jsonconfig/config.go
Expand Up @@ -231,9 +231,37 @@ func marshalProviderConfigs(
p.VersionConstraint = getproviders.VersionConstraintsString(vc)
}

if c.Parent != nil {
parentKey := opaqueProviderKey(pr.Name, c.Parent.Path.String())
p.parentKey = findSourceProviderKey(parentKey, m)
}

m[key] = p
}

// In child modules, providers defined in the parent module can be implicitly used.
// Such providers could have no requirements and configuration blocks defined.
if c.Parent != nil {
for req := range reqs {
// Implicit inheritance only applies to the default provider,
// so the provider name must be same as the provider type.
key := opaqueProviderKey(req.Type, c.Path.String())
if _, exists := m[key]; exists {
continue
}

parentKey := opaqueProviderKey(req.Type, c.Parent.Path.String())
p := providerConfig{
Name: req.Type,
FullName: req.String(),
ModuleAddress: c.Path.String(),
parentKey: findSourceProviderKey(parentKey, m),
}

m[key] = p
}
}

// Must also visit our child modules, recursively.
for name, mc := range c.Module.ModuleCalls {
// Keys in c.Children are guaranteed to match those in c.Module.ModuleCalls
Expand All @@ -259,22 +287,7 @@ func marshalProviderConfigs(

key := opaqueProviderKey(moduleProviderName, cc.Path.String())
parentKey := opaqueProviderKey(parentProviderName, cc.Parent.Path.String())

// Traverse up the module call tree until we find the provider
// configuration which has no linked parent config. This is then
// the source of the configuration used in this module call, so
// we link to it directly
for {
parent, exists := m[parentKey]
if !exists {
break
}
p.parentKey = parentKey
parentKey = parent.parentKey
if parentKey == "" {
break
}
}
p.parentKey = findSourceProviderKey(parentKey, m)

m[key] = p
}
Expand Down Expand Up @@ -527,3 +540,20 @@ func opaqueProviderKey(provider string, addr string) (key string) {
}
return key
}

// Traverse up the module call tree until we find the provider
// configuration which has no linked parent config. This is then
// the source of the configuration used in this module call, so
// we link to it directly
func findSourceProviderKey(startKey string, m map[string]providerConfig) string {
parentKey := startKey
for {
parent, exists := m[parentKey]
if !exists || parent.parentKey == "" {
break
}
parentKey = parent.parentKey
}

return parentKey
}
@@ -0,0 +1,19 @@
terraform {
required_providers {
test = {
source = "hashicorp/test"
}
}
}

resource "test_instance" "test" {
ami = "bar"
}

module "with_requirement" {
source = "./nested"
}

module "no_requirements" {
source = "./nested-no-requirements"
}
@@ -0,0 +1,3 @@
resource "test_instance" "test" {
ami = "qux"
}
@@ -0,0 +1,11 @@
terraform {
required_providers {
test = {
source = "hashicorp/test"
}
}
}

resource "test_instance" "test" {
ami = "baz"
}
@@ -0,0 +1,19 @@
provider "test" {
region = "somewhere"
}

provider "test" {
alias = "backup"
region = "elsewhere"
}

resource "test_instance" "test" {
ami = "foo"
}

module "child" {
source = "./child"
providers = {
test = test.backup
}
}