From a633cdf95d4aac21ee140d544836159ffd913526 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 13 Nov 2016 22:17:51 -0800 Subject: [PATCH] terraform: improve error messages to assist REPL --- repl/session_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ terraform/interpolate.go | 14 ++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/repl/session_test.go b/repl/session_test.go index 5936c61aa..30b60eca6 100644 --- a/repl/session_test.go +++ b/repl/session_test.go @@ -25,6 +25,21 @@ func TestSession_basicState(t *testing.T) { }, }, }, + + &terraform.ModuleState{ + Path: []string{"root", "module"}, + Resources: map[string]*terraform.ResourceState{ + "test_instance.foo": &terraform.ResourceState{ + Type: "test_instance", + Primary: &terraform.InstanceState{ + ID: "bar", + Attributes: map[string]string{ + "id": "bar", + }, + }, + }, + }, + }, }, } @@ -64,6 +79,32 @@ func TestSession_basicState(t *testing.T) { }, }) }) + + t.Run("missing module", func(t *testing.T) { + testSession(t, testSessionTest{ + State: state, + Inputs: []testSessionInput{ + { + Input: "module.child.foo", + Error: true, + ErrorContains: "Couldn't find module \"child\"", + }, + }, + }) + }) + + t.Run("missing module output", func(t *testing.T) { + testSession(t, testSessionTest{ + State: state, + Inputs: []testSessionInput{ + { + Input: "module.module.foo", + Error: true, + ErrorContains: "Couldn't find output \"foo\"", + }, + }, + }) + }) } func TestSession_stateless(t *testing.T) { diff --git a/terraform/interpolate.go b/terraform/interpolate.go index cba22e466..3d88da844 100644 --- a/terraform/interpolate.go +++ b/terraform/interpolate.go @@ -160,6 +160,13 @@ func (i *Interpolater) valueModuleVar( // ensure that the module is in the state, so if we reach this // point otherwise it really is a panic. result[n] = unknownVariable() + + // During apply this is always an error + if i.Operation == walkApply { + return fmt.Errorf( + "Couldn't find module %q for var: %s", + v.Name, v.FullKey()) + } } else { // Get the value from the outputs if outputState, ok := mod.Outputs[v.Field]; ok { @@ -171,6 +178,13 @@ func (i *Interpolater) valueModuleVar( } else { // Same reasons as the comment above. result[n] = unknownVariable() + + // During apply this is always an error + if i.Operation == walkApply { + return fmt.Errorf( + "Couldn't find output %q for module var: %s", + v.Field, v.FullKey()) + } } }