From 4bf7240475323af9bf802fb5d35a039937f9a535 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Fri, 10 Dec 2021 12:48:32 -0500 Subject: [PATCH] jsonconfig: Add provider configs to module calls 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. --- internal/command/jsonconfig/config.go | 8 + .../show-json/provider-aliasing/child/main.tf | 18 +++ .../show-json/provider-aliasing/main.tf | 16 ++ .../show-json/provider-aliasing/output.json | 150 ++++++++++++++++++ website/docs/internals/json-format.mdx | 8 + 5 files changed, 200 insertions(+) create mode 100644 internal/command/testdata/show-json/provider-aliasing/child/main.tf create mode 100644 internal/command/testdata/show-json/provider-aliasing/main.tf create mode 100755 internal/command/testdata/show-json/provider-aliasing/output.json diff --git a/internal/command/jsonconfig/config.go b/internal/command/jsonconfig/config.go index 617181ba608b..2290a292185a 100644 --- a/internal/command/jsonconfig/config.go +++ b/internal/command/jsonconfig/config.go @@ -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 @@ -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 } diff --git a/internal/command/testdata/show-json/provider-aliasing/child/main.tf b/internal/command/testdata/show-json/provider-aliasing/child/main.tf new file mode 100644 index 000000000000..deba8d9a33ff --- /dev/null +++ b/internal/command/testdata/show-json/provider-aliasing/child/main.tf @@ -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 +} diff --git a/internal/command/testdata/show-json/provider-aliasing/main.tf b/internal/command/testdata/show-json/provider-aliasing/main.tf new file mode 100644 index 000000000000..6113bbfbfb83 --- /dev/null +++ b/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 + } +} diff --git a/internal/command/testdata/show-json/provider-aliasing/output.json b/internal/command/testdata/show-json/provider-aliasing/output.json new file mode 100755 index 000000000000..66c4b3f9bfcc --- /dev/null +++ b/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" + } + } + } + } + } +} diff --git a/website/docs/internals/json-format.mdx b/website/docs/internals/json-format.mdx index 556d442eeb36..7125c3ce38a9 100644 --- a/website/docs/internals/json-format.mdx +++ b/website/docs/internals/json-format.mdx @@ -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": , + + // "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" + }, } } }