diff --git a/tfconfig/load_hcl.go b/tfconfig/load_hcl.go index 28e7b00..cb59ce6 100644 --- a/tfconfig/load_hcl.go +++ b/tfconfig/load_hcl.go @@ -222,6 +222,21 @@ func LoadModuleFromFile(file *hcl.File, mod *Module) hcl.Diagnostics { } } + providerKey := name + var alias string + if attr, defined := content.Attributes["alias"]; defined { + valDiags := gohcl.DecodeExpression(attr.Expr, nil, &alias) + diags = append(diags, valDiags...) + if !valDiags.HasErrors() && alias != "" { + providerKey = fmt.Sprintf("%s.%s", name, alias) + } + } + + mod.ProviderConfigs[providerKey] = &ProviderConfig{ + Name: name, + Alias: alias, + } + case "resource", "data": content, _, contentDiags := block.Body.PartialContent(resourceSchema) diff --git a/tfconfig/module.go b/tfconfig/module.go index c2250af..7c8c741 100644 --- a/tfconfig/module.go +++ b/tfconfig/module.go @@ -12,9 +12,10 @@ type Module struct { RequiredCore []string `json:"required_core,omitempty"` RequiredProviders map[string]*ProviderRequirement `json:"required_providers"` - ManagedResources map[string]*Resource `json:"managed_resources"` - DataResources map[string]*Resource `json:"data_resources"` - ModuleCalls map[string]*ModuleCall `json:"module_calls"` + ProviderConfigs map[string]*ProviderConfig `json:"provider_configs,omitempty"` + ManagedResources map[string]*Resource `json:"managed_resources"` + DataResources map[string]*Resource `json:"data_resources"` + ModuleCalls map[string]*ModuleCall `json:"module_calls"` // Diagnostics records any errors and warnings that were detected during // loading, primarily for inclusion in serialized forms of the module @@ -22,6 +23,12 @@ type Module struct { Diagnostics Diagnostics `json:"diagnostics,omitempty"` } +// ProviderConfig represents a provider block in the configuration +type ProviderConfig struct { + Name string `json:"name"` + Alias string `json:"alias,omitempty"` +} + // NewModule creates new Module representing Terraform module at the given path func NewModule(path string) *Module { return &Module{ @@ -29,6 +36,7 @@ func NewModule(path string) *Module { Variables: make(map[string]*Variable), Outputs: make(map[string]*Output), RequiredProviders: make(map[string]*ProviderRequirement), + ProviderConfigs: make(map[string]*ProviderConfig), ManagedResources: make(map[string]*Resource), DataResources: make(map[string]*Resource), ModuleCalls: make(map[string]*ModuleCall), diff --git a/tfconfig/testdata/legacy-block-labels/legacy-block-labels.out.json b/tfconfig/testdata/legacy-block-labels/legacy-block-labels.out.json index 2272341..9b05329 100644 --- a/tfconfig/testdata/legacy-block-labels/legacy-block-labels.out.json +++ b/tfconfig/testdata/legacy-block-labels/legacy-block-labels.out.json @@ -36,6 +36,10 @@ } } }, + "provider_configs": { + "aws": {"name": "aws"}, + "noversion": {"name": "noversion"} + }, "managed_resources": { "null_resource.foo": { "mode": "managed", diff --git a/tfconfig/testdata/provider-aliases/provider-aliases.out.json b/tfconfig/testdata/provider-aliases/provider-aliases.out.json new file mode 100644 index 0000000..162f41f --- /dev/null +++ b/tfconfig/testdata/provider-aliases/provider-aliases.out.json @@ -0,0 +1,22 @@ +{ + "path": "testdata/provider-aliases", + "variables": {}, + "outputs": {}, + "required_providers": { + "bar": {}, + "baz": {}, + "empty": {}, + "foo": {} + }, + "provider_configs": { + "bar.yellow": {"name": "bar", "alias": "yellow"}, + "baz": {"name": "baz"}, + "empty": {"name": "empty"}, + "bar": {"name": "bar"}, + "foo.blue": {"name": "foo", "alias": "blue"}, + "foo.red": {"name": "foo", "alias": "red"} + }, + "managed_resources": {}, + "data_resources": {}, + "module_calls": {} +} diff --git a/tfconfig/testdata/provider-aliases/provider-aliases.out.md b/tfconfig/testdata/provider-aliases/provider-aliases.out.md new file mode 100644 index 0000000..98396a7 --- /dev/null +++ b/tfconfig/testdata/provider-aliases/provider-aliases.out.md @@ -0,0 +1,9 @@ + +# Module `testdata/provider-aliases` + +Provider Requirements: +* **bar:** (any version) +* **baz:** (any version) +* **empty:** (any version) +* **foo:** (any version) + diff --git a/tfconfig/testdata/provider-aliases/provider-aliases.tf b/tfconfig/testdata/provider-aliases/provider-aliases.tf new file mode 100644 index 0000000..2d31137 --- /dev/null +++ b/tfconfig/testdata/provider-aliases/provider-aliases.tf @@ -0,0 +1,21 @@ +provider "foo" { + alias = "blue" +} + +provider "foo" { + alias = "red" +} + +provider "bar" { +} + +provider "bar" { + alias = "yellow" +} + +provider "baz" { +} + +provider "empty" { + alias = "" +} diff --git a/tfconfig/testdata/provider-configs/provider-configs.out.json b/tfconfig/testdata/provider-configs/provider-configs.out.json index 540d55b..ea166ca 100644 --- a/tfconfig/testdata/provider-configs/provider-configs.out.json +++ b/tfconfig/testdata/provider-configs/provider-configs.out.json @@ -16,6 +16,10 @@ }, "variables": {}, "outputs": {}, + "provider_configs": { + "foo": {"name": "foo"}, + "bar": {"name": "bar"} + }, "managed_resources": { "bar_bar.bar": { "mode": "managed", diff --git a/tfconfig/testdata/type-conversions/type-conversions.out.json b/tfconfig/testdata/type-conversions/type-conversions.out.json index 1cf67df..1844baf 100644 --- a/tfconfig/testdata/type-conversions/type-conversions.out.json +++ b/tfconfig/testdata/type-conversions/type-conversions.out.json @@ -39,6 +39,9 @@ } } }, + "provider_configs": { + "foo": {"name": "foo"} + }, "managed_resources": { "foo.foo": { "mode": "managed", diff --git a/tfconfig/testdata/type-errors/type-errors.out.json b/tfconfig/testdata/type-errors/type-errors.out.json index 3156fc4..e10620f 100644 --- a/tfconfig/testdata/type-errors/type-errors.out.json +++ b/tfconfig/testdata/type-errors/type-errors.out.json @@ -108,6 +108,9 @@ } } }, + "provider_configs": { + "foo": {"name": "foo"} + }, "managed_resources": { "foo.foo": { "mode": "managed",