From 990b8141886f77491a440e2f71fc512545f5d98f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 17 Oct 2014 18:18:28 -0700 Subject: [PATCH] terraform: set count to 1 while validating [GH-442] --- terraform/context.go | 38 +++++++++++++------ terraform/context_test.go | 22 +++++++++++ .../validate-count-variable/main.tf | 6 +++ 3 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 terraform/test-fixtures/validate-count-variable/main.tf diff --git a/terraform/context.go b/terraform/context.go index f7b24aced..2cd510f5b 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -1034,18 +1034,20 @@ func (c *walkContext) validateWalkFn() depgraph.WalkFunc { // Interpolate the count and verify it is non-negative rc := NewResourceConfig(rn.Config.RawCount) rc.interpolate(c, rn.Resource) - count, err := rn.Config.Count() - if err == nil { - if count < 0 { - err = fmt.Errorf( - "%s error: count must be positive", rn.Resource.Id) + if !rc.IsComputed(rn.Config.RawCount.Key) { + 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 } - } - if err != nil { - l.Lock() - defer l.Unlock() - meta.Errs = append(meta.Errs, err) - return nil } return c.genericWalkResource(rn, walkFn) @@ -1269,6 +1271,20 @@ func (c *walkContext) genericWalkResource( rc := NewResourceConfig(rn.Config.RawCount) rc.interpolate(c, rn.Resource) + // If we're validating, then we set the count to 1 if it is computed + if c.Operation == walkValidate { + if key := rn.Config.RawCount.Key; rc.IsComputed(key) { + // Preserve the old value so that we reset it properly + old := rn.Config.RawCount.Raw[key] + defer func() { + rn.Config.RawCount.Raw[key] = old + }() + + // Set th count to 1 for validation purposes + rn.Config.RawCount.Raw[key] = "1" + } + } + // Expand the node to the actual resources g, err := rn.Expand() if err != nil { diff --git a/terraform/context_test.go b/terraform/context_test.go index 213237cc7..105c2c0a3 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -149,6 +149,28 @@ func TestContextValidate_countVariable(t *testing.T) { } } +func TestContextValidate_countVariableNoDefault(t *testing.T) { + p := testProvider("aws") + m := testModule(t, "validate-count-variable") + 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) > 1 { + for _, err := range e { + t.Errorf("bad: %s", err) + } + t.FailNow() + } +} + func TestContextValidate_moduleBadResource(t *testing.T) { m := testModule(t, "validate-module-bad-rc") p := testProvider("aws") diff --git a/terraform/test-fixtures/validate-count-variable/main.tf b/terraform/test-fixtures/validate-count-variable/main.tf new file mode 100644 index 000000000..9c892ac2e --- /dev/null +++ b/terraform/test-fixtures/validate-count-variable/main.tf @@ -0,0 +1,6 @@ +variable "foo" {} + +resource "aws_instance" "foo" { + foo = "foo" + count = "${var.foo}" +}