From 105b66e74d5b07d867d84a33ca500f2a2fda01c4 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 10 Nov 2017 09:52:00 -0500 Subject: [PATCH] error out when a referenced provider is missing --- .../transform-provider-invalid/main.tf | 11 +++++++ .../transform-provider-invalid/mod/main.tf | 2 ++ terraform/transform_provider.go | 3 +- terraform/transform_provider_test.go | 29 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 terraform/test-fixtures/transform-provider-invalid/main.tf create mode 100644 terraform/test-fixtures/transform-provider-invalid/mod/main.tf diff --git a/terraform/test-fixtures/transform-provider-invalid/main.tf b/terraform/test-fixtures/transform-provider-invalid/main.tf new file mode 100644 index 000000000000..17e6304e02f6 --- /dev/null +++ b/terraform/test-fixtures/transform-provider-invalid/main.tf @@ -0,0 +1,11 @@ +provider "aws" { +} + +module "mod" { + source = "./mod" + + # aws.foo doesn't exist, and should report an error + providers = { + "aws" = "aws.foo" + } +} diff --git a/terraform/test-fixtures/transform-provider-invalid/mod/main.tf b/terraform/test-fixtures/transform-provider-invalid/mod/main.tf new file mode 100644 index 000000000000..03641197f0fc --- /dev/null +++ b/terraform/test-fixtures/transform-provider-invalid/mod/main.tf @@ -0,0 +1,2 @@ +resource "aws_resource" "foo" { +} diff --git a/terraform/transform_provider.go b/terraform/transform_provider.go index da2ae3f31845..19df21897ec5 100644 --- a/terraform/transform_provider.go +++ b/terraform/transform_provider.go @@ -428,7 +428,7 @@ func (t *ProviderConfigTransformer) Transform(g *Graph) error { // Start the transformation process if err := t.transform(g, t.Module); err != nil { - return nil + return err } // finally attach the configs to the new nodes @@ -524,6 +524,7 @@ func (t *ProviderConfigTransformer) addProxyProviders(g *Graph, m *module.Tree) for name, parentName := range parentCfg.Providers { fullName := ResolveProviderName(name, path) fullParentName := ResolveProviderName(parentName, parentPath) + parentProvider := t.providers[fullParentName] if parentProvider == nil { diff --git a/terraform/transform_provider_test.go b/terraform/transform_provider_test.go index c6698b382f69..a418b5b6784b 100644 --- a/terraform/transform_provider_test.go +++ b/terraform/transform_provider_test.go @@ -545,6 +545,35 @@ provider.aws.foo`) } } +// error out when a non-existent provider is named in a module providers map +func TestProviderConfigTransformer_invalidProvider(t *testing.T) { + mod := testModule(t, "transform-provider-invalid") + concrete := func(a *NodeAbstractProvider) dag.Vertex { return a } + + g := Graph{Path: RootModulePath} + { + tf := &ConfigTransformer{Module: mod} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + { + tf := &AttachResourceConfigTransformer{Module: mod} + if err := tf.Transform(&g); err != nil { + t.Fatalf("err: %s", err) + } + } + + tf := TransformProviders([]string{"aws"}, concrete, mod) + err := tf.Transform(&g) + if err == nil { + t.Fatal("expected missing provider error") + } + if !strings.Contains(err.Error(), "provider.aws.foo") { + t.Fatalf("error should reference missing provider, got: %s", err) + } +} + const testTransformProviderBasicStr = ` aws_instance.web provider.aws