From 5e3d2dbdfa4e6329e018e291b40adf9769ad900a Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Fri, 26 Jun 2020 09:45:02 -0400 Subject: [PATCH] configs: Fail early for invalid resource provider If a resource's "provider" reference is invalid and cannot be parsed, we should not store the reference as part of a `ProviderConfigRef`. Doing so creates an invalid data structure, which prevents us from using `MustParseProviderPart` with the name in later steps. The invalid test files added in this commit will cause a panic without the code change. --- configs/resource.go | 3 +++ .../testdata/invalid-files/data-invalid-provider-reference.tf | 3 +++ .../invalid-files/resource-invalid-provider-reference.tf | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 configs/testdata/invalid-files/data-invalid-provider-reference.tf create mode 100644 configs/testdata/invalid-files/resource-invalid-provider-reference.tf diff --git a/configs/resource.go b/configs/resource.go index 72adf66f0..9b0e4ef1e 100644 --- a/configs/resource.go +++ b/configs/resource.go @@ -422,6 +422,9 @@ func decodeProviderConfigRef(expr hcl.Expression, argName string) (*ProviderConf name := traversal.RootName() nameDiags := checkProviderNameNormalized(name, traversal[0].SourceRange()) diags = append(diags, nameDiags...) + if diags.HasErrors() { + return nil, diags + } ret := &ProviderConfigRef{ Name: name, diff --git a/configs/testdata/invalid-files/data-invalid-provider-reference.tf b/configs/testdata/invalid-files/data-invalid-provider-reference.tf new file mode 100644 index 000000000..aef92fd33 --- /dev/null +++ b/configs/testdata/invalid-files/data-invalid-provider-reference.tf @@ -0,0 +1,3 @@ +data "test_resource" "t" { + provider = my_test +} diff --git a/configs/testdata/invalid-files/resource-invalid-provider-reference.tf b/configs/testdata/invalid-files/resource-invalid-provider-reference.tf new file mode 100644 index 000000000..6fe675f27 --- /dev/null +++ b/configs/testdata/invalid-files/resource-invalid-provider-reference.tf @@ -0,0 +1,3 @@ +resource "test_resource" "t" { + provider = my_test +}