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

@ -171,7 +171,8 @@ func testFixturePath(name string) string {
func metaOverridesForProvider(p providers.Interface) *testingOverrides {
return &testingOverrides{
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 {
parentKey := opaqueProviderKey(pr.Name, c.Parent.Path.String())
p.parentKey = findSourceProviderKey(parentKey, m)
p.parentKey = findSourceProviderKey(parentKey, p.FullName, m)
}
m[key] = p
@ -255,7 +255,7 @@ func marshalProviderConfigs(
Name: req.Type,
FullName: req.String(),
ModuleAddress: c.Path.String(),
parentKey: findSourceProviderKey(parentKey, m),
parentKey: findSourceProviderKey(parentKey, req.String(), m),
}
m[key] = p
@ -287,7 +287,7 @@ func marshalProviderConfigs(
key := opaqueProviderKey(moduleProviderName, cc.Path.String())
parentKey := opaqueProviderKey(parentProviderName, cc.Parent.Path.String())
p.parentKey = findSourceProviderKey(parentKey, m)
p.parentKey = findSourceProviderKey(parentKey, p.FullName, m)
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
// 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 == "" {
func findSourceProviderKey(startKey string, fullName string, m map[string]providerConfig) string {
var parentKey string
key := startKey
for key != "" {
parent, exists := m[key]
if !exists || parent.FullName != fullName {
break
}
parentKey = parent.parentKey
parentKey = key
key = parent.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

@ -493,7 +493,8 @@ func TestShow_json_output(t *testing.T) {
expectError := strings.Contains(entry.Name(), "error")
providerSource, close := newMockProviderSource(t, map[string][]string{
"test": {"1.2.3"},
"test": {"1.2.3"},
"hashicorp2/test": {"1.2.3"},
})
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

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

View File

@ -35,23 +35,6 @@
],
"address": "module.child",
"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": [
{
@ -68,6 +51,23 @@
}
],
"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": {
"source": "./nested",
"depends_on": ["module.no_requirements"],
"module": {
"resources": [
{