From 898b459a03e7ffd904d28b92737bf96a03e6d22b Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Wed, 9 Sep 2020 11:50:28 -0400 Subject: [PATCH] configs: Error on invalid required_providers attrs A few users have recently been confused about the purpose of the required_providers objects, adding provider configuration parameters in addition to version and source. This previously did not cause an error so would result in a confusingly distant failure. This commit adds a single diagnostic for any required_providers object which includes attributes other than version or source. --- configs/provider_requirements.go | 13 +++++++++++ configs/provider_requirements_test.go | 31 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/configs/provider_requirements.go b/configs/provider_requirements.go index 74188ed816..ef746f3faa 100644 --- a/configs/provider_requirements.go +++ b/configs/provider_requirements.go @@ -113,6 +113,19 @@ func decodeRequiredProvidersBlock(block *hcl.Block) (*RequiredProviders, hcl.Dia } } } + attrTypes := expr.Type().AttributeTypes() + for name := range attrTypes { + if name == "version" || name == "source" { + continue + } + diags = append(diags, &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid required_providers object", + Detail: `required_providers objects can only contain "version" and "source" attributes. To configure a provider, use a "provider" block.`, + Subject: attr.Expr.Range().Ptr(), + }) + break + } default: // should not happen diff --git a/configs/provider_requirements_test.go b/configs/provider_requirements_test.go index 42e4bfbe1b..bf4d998821 100644 --- a/configs/provider_requirements_test.go +++ b/configs/provider_requirements_test.go @@ -332,6 +332,37 @@ func TestDecodeRequiredProvidersBlock(t *testing.T) { }, Error: "Invalid source", }, + "additional attributes": { + Block: &hcl.Block{ + Type: "required_providers", + Body: hcltest.MockBody(&hcl.BodyContent{ + Attributes: hcl.Attributes{ + "my-test": { + Name: "my-test", + Expr: hcltest.MockExprLiteral(cty.ObjectVal(map[string]cty.Value{ + "source": cty.StringVal("mycloud/test"), + "version": cty.StringVal("2.0.0"), + "invalid": cty.BoolVal(true), + })), + }, + }, + }), + DefRange: blockRange, + }, + Want: &RequiredProviders{ + RequiredProviders: map[string]*RequiredProvider{ + "my-test": { + Name: "my-test", + Source: "mycloud/test", + Type: addrs.NewProvider(addrs.DefaultRegistryHost, "mycloud", "test"), + Requirement: testVC("2.0.0"), + DeclRange: mockRange, + }, + }, + DeclRange: blockRange, + }, + Error: "Invalid required_providers object", + }, } for name, test := range tests {