From e4ba7373924237f3554578f9613ddd027160c0fa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Oct 2014 17:14:25 -0700 Subject: [PATCH] terraform: validate count is non-negative --- terraform/context.go | 17 +++++++++++++++++ terraform/context_test.go | 19 +++++++++++++++++++ .../validate-count-negative/main.tf | 3 +++ 3 files changed, 39 insertions(+) create mode 100644 terraform/test-fixtures/validate-count-negative/main.tf diff --git a/terraform/context.go b/terraform/context.go index a8894a73f..5dd2d2f05 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -991,6 +991,23 @@ func (c *walkContext) validateWalkFn() depgraph.WalkFunc { // If we're expanding, then expand the nodes, and then rewalk the graph if rn.ExpandMode > ResourceExpandNone { + // Interpolate the count and verify it is non-negative + rc := NewResourceConfig(rn.Config.RawCount) + rc.interpolate(c) + count, err := rn.Config.Count() + if err == nil { + if count < 0 { + err = fmt.Errorf( + "%s error: count must be positive", rn.Resource.Id) + } + } + if err != nil { + l.Lock() + defer l.Unlock() + meta.Errs = append(meta.Errs, err) + return nil + } + return c.genericWalkResource(rn, walkFn) } diff --git a/terraform/context_test.go b/terraform/context_test.go index b4ea97824..d44c499c2 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -107,6 +107,25 @@ func TestContextValidate_badVar(t *testing.T) { } } +func TestContextValidate_countNegative(t *testing.T) { + p := testProvider("aws") + m := testModule(t, "validate-count-negative") + c := testContext(t, &ContextOpts{ + Module: m, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + w, e := c.Validate() + if len(w) > 0 { + t.Fatalf("bad: %#v", w) + } + if len(e) == 0 { + t.Fatalf("bad: %#v", e) + } +} + func TestContextValidate_moduleBadResource(t *testing.T) { m := testModule(t, "validate-module-bad-rc") p := testProvider("aws") diff --git a/terraform/test-fixtures/validate-count-negative/main.tf b/terraform/test-fixtures/validate-count-negative/main.tf new file mode 100644 index 000000000..d5bb04653 --- /dev/null +++ b/terraform/test-fixtures/validate-count-negative/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "test" { + count = "-5" +}