Skip to content

Commit

Permalink
terraform: improve error messages to assist REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Nov 14, 2016
1 parent d9c5221 commit a633cdf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
41 changes: 41 additions & 0 deletions repl/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
},
},
},
},
},
}

Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
}
}
}

Expand Down

0 comments on commit a633cdf

Please sign in to comment.