prevent calling Count() on non-existent resources

It turns out that a few use cases depend on not finding a resource
without an error.

The other code paths had sufficient nil checks for this, but there was
one place where we called Count() that needed to be checked. If the
existence of the resource matters, it would be caught at a higher level
and still return an "unknown resource" error to the user.
This commit is contained in:
James Bardin 2017-03-01 11:49:41 -05:00
parent 1e80c402bf
commit 6fa4a591a0
2 changed files with 7 additions and 11 deletions

View File

@ -698,6 +698,10 @@ func (i *Interpolater) resourceCountMax(
// from the state. Plan and so on may not have any state yet so
// we do a full interpolation.
if i.Operation != walkApply {
if cr == nil {
return 0, nil
}
count, err := cr.Count()
if err != nil {
return 0, err

View File

@ -877,7 +877,7 @@ func TestInterpolator_sets(t *testing.T) {
}
// When a splat reference is made to a resource that is unknown, we should
// return an error.
// return an empty list rather than panicking.
func TestInterpolater_resourceUnknownVariableList(t *testing.T) {
i := &Interpolater{
Module: testModule(t, "plan-computed-data-resource"),
@ -889,16 +889,8 @@ func TestInterpolater_resourceUnknownVariableList(t *testing.T) {
Path: rootModulePath,
}
// missing "data" from the reference here
v, err := config.NewInterpolatedVariable("aws_vpc.bar.*.foo")
if err != nil {
t.Fatalf("err: %s", err)
}
_, err = i.Values(scope, map[string]config.InterpolatedVariable{"foo": v})
if err == nil {
t.Fatal("expected error interpolating invalid resource")
}
testInterpolate(t, i, scope, "aws_vpc.bar.*.foo",
interfaceToVariableSwallowError([]interface{}{}))
}
func testInterpolate(