Skip to content

Commit

Permalink
jsonconfig: add implicitly created provider configs
Browse files Browse the repository at this point in the history
  • Loading branch information
nozaq committed Feb 18, 2022
1 parent 0ce0404 commit 3c32f7a
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 24 deletions.
38 changes: 20 additions & 18 deletions internal/command/jsonconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,27 +239,29 @@ func marshalProviderConfigs(
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
}
// Providers could be implicitly created or inherited from the parent module
// when no requirements and configuration block defined.
for req := range reqs {
// Only default providers could implicitly exist,
// 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, req.String(), m),
}
p := providerConfig{
Name: req.Type,
FullName: req.String(),
ModuleAddress: c.Path.String(),
}

m[key] = p
// In child modules, providers defined in the parent module can be implicitly used.
if c.Parent != nil {
parentKey := opaqueProviderKey(req.Type, c.Parent.Path.String())
p.parentKey = findSourceProviderKey(parentKey, p.FullName, m)
}

m[key] = p
}

// Must also visit our child modules, recursively.
Expand Down
6 changes: 6 additions & 0 deletions internal/command/testdata/show-json/basic-delete/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"outputs": {
"test": {
Expand Down
6 changes: 6 additions & 0 deletions internal/command/testdata/show-json/basic-update/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"outputs": {
"test": {
Expand Down
6 changes: 6 additions & 0 deletions internal/command/testdata/show-json/drift/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"resources": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
}
],
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"resources": [
{
Expand Down
5 changes: 5 additions & 0 deletions internal/command/testdata/show-json/modules/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@
"module_address": "module.module_test_foo",
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
},
"module.module_test_bar:test": {
"module_address": "module.module_test_bar",
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions internal/command/testdata/show-json/moved-drift/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"resources": [
{
Expand Down
6 changes: 6 additions & 0 deletions internal/command/testdata/show-json/moved/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"resources": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"outputs": {
"test": {
Expand Down
15 changes: 9 additions & 6 deletions internal/command/testdata/show-json/nested-modules/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
"name": "test",
"provider_name": "registry.terraform.io/hashicorp/test",
"change": {
"actions": [
"create"
],
"actions": ["create"],
"before": null,
"after": {
"ami": "bar-var"
Expand All @@ -53,6 +51,13 @@
}
],
"configuration": {
"provider_config": {
"module.my_module.module.more:test": {
"module_address": "module.my_module.module.more",
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"module_calls": {
"my_module": {
Expand All @@ -71,9 +76,7 @@
"provider_config_key": "module.my_module.module.more:test",
"expressions": {
"ami": {
"references": [
"var.test_var"
]
"references": ["var.test_var"]
}
},
"schema_version": 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"resources": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
}
},
"configuration": {
"provider_config": {
"test": {
"name": "test",
"full_name": "registry.terraform.io/hashicorp/test"
}
},
"root_module": {
"outputs": {
"test": {
Expand Down

0 comments on commit 3c32f7a

Please sign in to comment.