jsonconfig: fix provider mappings with same names

This commit is contained in:
nozaq 2022-02-19 00:19:39 +09:00
parent 7f4019e1f6
commit 0ce040405c
No known key found for this signature in database
GPG Key ID: 99A56FF51B467245
9 changed files with 302 additions and 29 deletions

View File

@ -172,6 +172,7 @@ func metaOverridesForProvider(p providers.Interface) *testingOverrides {
return &testingOverrides{ return &testingOverrides{
Providers: map[addrs.Provider]providers.Factory{ Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): providers.FactoryFixed(p), addrs.NewDefaultProvider("test"): providers.FactoryFixed(p),
addrs.NewProvider(addrs.DefaultProviderRegistryHost, "hashicorp2", "test"): providers.FactoryFixed(p),
}, },
} }
} }

View File

@ -233,7 +233,7 @@ func marshalProviderConfigs(
if c.Parent != nil { if c.Parent != nil {
parentKey := opaqueProviderKey(pr.Name, c.Parent.Path.String()) parentKey := opaqueProviderKey(pr.Name, c.Parent.Path.String())
p.parentKey = findSourceProviderKey(parentKey, m) p.parentKey = findSourceProviderKey(parentKey, p.FullName, m)
} }
m[key] = p m[key] = p
@ -255,7 +255,7 @@ func marshalProviderConfigs(
Name: req.Type, Name: req.Type,
FullName: req.String(), FullName: req.String(),
ModuleAddress: c.Path.String(), ModuleAddress: c.Path.String(),
parentKey: findSourceProviderKey(parentKey, m), parentKey: findSourceProviderKey(parentKey, req.String(), m),
} }
m[key] = p m[key] = p
@ -287,7 +287,7 @@ func marshalProviderConfigs(
key := opaqueProviderKey(moduleProviderName, cc.Path.String()) key := opaqueProviderKey(moduleProviderName, cc.Path.String())
parentKey := opaqueProviderKey(parentProviderName, cc.Parent.Path.String()) parentKey := opaqueProviderKey(parentProviderName, cc.Parent.Path.String())
p.parentKey = findSourceProviderKey(parentKey, m) p.parentKey = findSourceProviderKey(parentKey, p.FullName, m)
m[key] = p m[key] = p
} }
@ -545,14 +545,18 @@ func opaqueProviderKey(provider string, addr string) (key string) {
// configuration which has no linked parent config. This is then // configuration which has no linked parent config. This is then
// the source of the configuration used in this module call, so // the source of the configuration used in this module call, so
// we link to it directly // we link to it directly
func findSourceProviderKey(startKey string, m map[string]providerConfig) string { func findSourceProviderKey(startKey string, fullName string, m map[string]providerConfig) string {
parentKey := startKey var parentKey string
for {
parent, exists := m[parentKey] key := startKey
if !exists || parent.parentKey == "" { for key != "" {
parent, exists := m[key]
if !exists || parent.FullName != fullName {
break break
} }
parentKey = parent.parentKey
parentKey = key
key = parent.parentKey
} }
return parentKey return parentKey

View File

@ -0,0 +1,100 @@
package jsonconfig
import (
"testing"
)
func TestFindSourceProviderConfig(t *testing.T) {
tests := []struct {
StartKey string
FullName string
ProviderMap map[string]providerConfig
Want string
}{
{
StartKey: "null",
FullName: "hashicorp/null",
ProviderMap: map[string]providerConfig{},
Want: "",
},
{
StartKey: "null",
FullName: "hashicorp/null",
ProviderMap: map[string]providerConfig{
"null": {
Name: "null",
FullName: "hashicorp/null",
ModuleAddress: "",
},
},
Want: "null",
},
{
StartKey: "null2",
FullName: "hashicorp/null",
ProviderMap: map[string]providerConfig{
"null": {
Name: "null",
FullName: "hashicorp/null",
ModuleAddress: "",
},
},
Want: "",
},
{
StartKey: "null",
FullName: "hashicorp2/null",
ProviderMap: map[string]providerConfig{
"null": {
Name: "null",
FullName: "hashicorp/null",
ModuleAddress: "",
},
},
Want: "",
},
{
StartKey: "module.a:null",
FullName: "hashicorp/null",
ProviderMap: map[string]providerConfig{
"null": {
Name: "null",
FullName: "hashicorp/null",
ModuleAddress: "",
},
"module.a:null": {
Name: "module.a:null",
FullName: "hashicorp/null",
ModuleAddress: "module.a",
parentKey: "null",
},
},
Want: "null",
},
{
StartKey: "module.a:null",
FullName: "hashicorp2/null",
ProviderMap: map[string]providerConfig{
"null": {
Name: "null",
FullName: "hashicorp/null",
ModuleAddress: "",
},
"module.a:null": {
Name: "module.a:null",
FullName: "hashicorp2/null",
ModuleAddress: "module.a",
parentKey: "null",
},
},
Want: "module.a:null",
},
}
for _, test := range tests {
got := findSourceProviderKey(test.StartKey, test.FullName, test.ProviderMap)
if got != test.Want {
t.Errorf("wrong result:\nGot: %#v\nWant: %#v\n", got, test.Want)
}
}
}

View File

@ -494,6 +494,7 @@ func TestShow_json_output(t *testing.T) {
providerSource, close := newMockProviderSource(t, map[string][]string{ providerSource, close := newMockProviderSource(t, map[string][]string{
"test": {"1.2.3"}, "test": {"1.2.3"},
"hashicorp2/test": {"1.2.3"},
}) })
defer close() defer close()

View File

@ -0,0 +1,11 @@
terraform {
required_providers {
test = {
source = "hashicorp2/test"
}
}
}
resource "test_instance" "test" {
ami = "bar"
}

View File

@ -0,0 +1,11 @@
provider "test" {
region = "somewhere"
}
resource "test_instance" "test" {
ami = "foo"
}
module "child" {
source = "./child"
}

View File

@ -0,0 +1,143 @@
{
"format_version": "1.0",
"terraform_version": "1.1.0-dev",
"planned_values": {
"root_module": {
"resources": [
{
"address": "test_instance.test",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/test",
"schema_version": 0,
"values": {
"ami": "foo"
},
"sensitive_values": {}
}
],
"child_modules": [
{
"resources": [
{
"address": "module.child.test_instance.test",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp2/test",
"schema_version": 0,
"values": {
"ami": "bar"
},
"sensitive_values": {}
}
],
"address": "module.child"
}
]
}
},
"resource_changes": [
{
"address": "module.child.test_instance.test",
"module_address": "module.child",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp2/test",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"ami": "bar"
},
"after_unknown": {
"id": true
},
"before_sensitive": false,
"after_sensitive": {}
}
},
{
"address": "test_instance.test",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/test",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"ami": "foo"
},
"after_unknown": {
"id": true
},
"before_sensitive": false,
"after_sensitive": {}
}
}
],
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test",
"expressions": {
"region": {
"constant_value": "somewhere"
}
}
},
"module.child:test": {
"module_address": "module.child",
"name": "test",
"full_name": "registry.terraform.io/hashicorp2/test"
}
},
"root_module": {
"resources": [
{
"address": "test_instance.test",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_config_key": "test",
"expressions": {
"ami": {
"constant_value": "foo"
}
},
"schema_version": 0
}
],
"module_calls": {
"child": {
"source": "./child",
"module": {
"resources": [
{
"address": "test_instance.test",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_config_key": "module.child:test",
"expressions": {
"ami": {
"constant_value": "bar"
}
},
"schema_version": 0
}
]
}
}
}
}
}
}

View File

@ -12,6 +12,7 @@ resource "test_instance" "test" {
module "with_requirement" { module "with_requirement" {
source = "./nested" source = "./nested"
depends_on = [module.no_requirements]
} }
module "no_requirements" { module "no_requirements" {

View File

@ -35,23 +35,6 @@
], ],
"address": "module.child", "address": "module.child",
"child_modules": [ "child_modules": [
{
"resources": [
{
"address": "module.child.module.with_requirement.test_instance.test",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/test",
"schema_version": 0,
"values": {
"ami": "baz"
},
"sensitive_values": {}
}
],
"address": "module.child.module.with_requirement"
},
{ {
"resources": [ "resources": [
{ {
@ -68,6 +51,23 @@
} }
], ],
"address": "module.child.module.no_requirements" "address": "module.child.module.no_requirements"
},
{
"resources": [
{
"address": "module.child.module.with_requirement.test_instance.test",
"mode": "managed",
"type": "test_instance",
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/test",
"schema_version": 0,
"values": {
"ami": "baz"
},
"sensitive_values": {}
}
],
"address": "module.child.module.with_requirement"
} }
] ]
} }
@ -243,6 +243,7 @@
}, },
"with_requirement": { "with_requirement": {
"source": "./nested", "source": "./nested",
"depends_on": ["module.no_requirements"],
"module": { "module": {
"resources": [ "resources": [
{ {