Skip to content

Commit

Permalink
jsonconfig: Add provider configs to module calls
Browse files Browse the repository at this point in the history
Add provider alias map to the JSON config module call output, allowing
consumers of this format to determine which provider configurations map
to the module-local aliases.
  • Loading branch information
alisdair committed Jan 26, 2022
1 parent fb61a3d commit 4bf7240
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/command/jsonconfig/config.go
Expand Up @@ -50,6 +50,7 @@ type moduleCall struct {
Module module `json:"module,omitempty"`
VersionConstraint string `json:"version_constraint,omitempty"`
DependsOn []string `json:"depends_on,omitempty"`
Providers map[string]string `json:"providers,omitempty"`
}

// variables is the JSON representation of the variables provided to the current
Expand Down Expand Up @@ -336,6 +337,13 @@ func marshalModuleCall(c *configs.Config, mc *configs.ModuleCall, schemas *terra
ret.DependsOn = dependencies
}

if len(mc.Providers) > 0 {
ret.Providers = make(map[string]string, len(mc.Providers))
for _, ppc := range mc.Providers {
ret.Providers[ppc.InChild.String()] = ppc.InParent.String()
}
}

return ret
}

Expand Down
@@ -0,0 +1,18 @@
terraform {
required_providers {
test = {
source = "hashicorp/test"
configuration_aliases = [test, test.second]
}
}
}

resource "test_instance" "test_primary" {
ami = "primary"
provider = test
}

resource "test_instance" "test_secondary" {
ami = "secondary"
provider = test.second
}
16 changes: 16 additions & 0 deletions internal/command/testdata/show-json/provider-aliasing/main.tf
@@ -0,0 +1,16 @@
provider "test" {
region = "somewhere"
}

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

module "child" {
source = "./child"
providers = {
test = test
test.second = test.backup
}
}
150 changes: 150 additions & 0 deletions internal/command/testdata/show-json/provider-aliasing/output.json
@@ -0,0 +1,150 @@
{
"format_version": "1.0",
"planned_values": {
"root_module": {
"child_modules": [
{
"resources": [
{
"address": "module.child.test_instance.test_primary",
"mode": "managed",
"type": "test_instance",
"name": "test_primary",
"provider_name": "registry.terraform.io/hashicorp/test",
"schema_version": 0,
"values": {
"ami": "primary"
},
"sensitive_values": {}
},
{
"address": "module.child.test_instance.test_secondary",
"mode": "managed",
"type": "test_instance",
"name": "test_secondary",
"provider_name": "registry.terraform.io/hashicorp/test",
"schema_version": 0,
"values": {
"ami": "secondary"
},
"sensitive_values": {}
}
],
"address": "module.child"
}
]
}
},
"resource_changes": [
{
"address": "module.child.test_instance.test_primary",
"module_address": "module.child",
"mode": "managed",
"type": "test_instance",
"name": "test_primary",
"provider_name": "registry.terraform.io/hashicorp/test",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"ami": "primary"
},
"after_unknown": {
"id": true
},
"before_sensitive": false,
"after_sensitive": {}
}
},
{
"address": "module.child.test_instance.test_secondary",
"module_address": "module.child",
"mode": "managed",
"type": "test_instance",
"name": "test_secondary",
"provider_name": "registry.terraform.io/hashicorp/test",
"change": {
"actions": [
"create"
],
"before": null,
"after": {
"ami": "secondary"
},
"after_unknown": {
"id": true
},
"before_sensitive": false,
"after_sensitive": {}
}
}
],
"configuration": {
"provider_config": {
"module.child:test": {
"name": "test",
"module_address": "module.child"
},
"test": {
"name": "test",
"expressions": {
"region": {
"constant_value": "somewhere"
}
}
},
"test.backup": {
"name": "test",
"alias": "backup",
"expressions": {
"region": {
"constant_value": "elsewhere"
}
}
}
},
"root_module": {
"module_calls": {
"child": {
"source": "./child",
"module": {
"resources": [
{
"address": "test_instance.test_primary",
"mode": "managed",
"type": "test_instance",
"name": "test_primary",
"provider_config_key": "child:test",
"expressions": {
"ami": {
"constant_value": "primary"
}
},
"schema_version": 0
},
{
"address": "test_instance.test_secondary",
"mode": "managed",
"type": "test_instance",
"name": "test_secondary",
"provider_config_key": "child:test.second",
"expressions": {
"ami": {
"constant_value": "secondary"
}
},
"schema_version": 0
}
]
},
"providers": {
"test": "test",
"test.second": "test.backup"
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions website/docs/internals/json-format.mdx
Expand Up @@ -441,6 +441,14 @@ Because the configuration models are produced at a stage prior to expression eva
// itself, using the same structure as the "root_module" object,
// recursively describing the full module tree.
"module": <module-configuration-representation>,

// "providers" is a map of provider aliases, as defined in the child
// module, to provider configurations, as defined in the root module and
// the "provider_configs" map above.
"providers": {
"aws": "aws",
"aws.secondary": "aws.foo"
},
}
}
}
Expand Down

0 comments on commit 4bf7240

Please sign in to comment.