From 6fa4a591a0420881481e32b1a77750418e4d15a9 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 1 Mar 2017 11:49:41 -0500 Subject: [PATCH] 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. --- terraform/interpolate.go | 4 ++++ terraform/interpolate_test.go | 14 +++----------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/terraform/interpolate.go b/terraform/interpolate.go index a59af6e83..11d5a53dc 100644 --- a/terraform/interpolate.go +++ b/terraform/interpolate.go @@ -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 diff --git a/terraform/interpolate_test.go b/terraform/interpolate_test.go index e161b5e37..b85f2aad4 100644 --- a/terraform/interpolate_test.go +++ b/terraform/interpolate_test.go @@ -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(