terraform: set count to 1 while validating [GH-442]

This commit is contained in:
Mitchell Hashimoto 2014-10-17 18:18:28 -07:00
parent f626c5df96
commit 990b814188
3 changed files with 55 additions and 11 deletions

View File

@ -1034,18 +1034,20 @@ func (c *walkContext) validateWalkFn() depgraph.WalkFunc {
// Interpolate the count and verify it is non-negative // Interpolate the count and verify it is non-negative
rc := NewResourceConfig(rn.Config.RawCount) rc := NewResourceConfig(rn.Config.RawCount)
rc.interpolate(c, rn.Resource) rc.interpolate(c, rn.Resource)
count, err := rn.Config.Count() if !rc.IsComputed(rn.Config.RawCount.Key) {
if err == nil { count, err := rn.Config.Count()
if count < 0 { if err == nil {
err = fmt.Errorf( if count < 0 {
"%s error: count must be positive", rn.Resource.Id) 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) return c.genericWalkResource(rn, walkFn)
@ -1269,6 +1271,20 @@ func (c *walkContext) genericWalkResource(
rc := NewResourceConfig(rn.Config.RawCount) rc := NewResourceConfig(rn.Config.RawCount)
rc.interpolate(c, rn.Resource) 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 // Expand the node to the actual resources
g, err := rn.Expand() g, err := rn.Expand()
if err != nil { if err != nil {

View File

@ -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) { func TestContextValidate_moduleBadResource(t *testing.T) {
m := testModule(t, "validate-module-bad-rc") m := testModule(t, "validate-module-bad-rc")
p := testProvider("aws") p := testProvider("aws")

View File

@ -0,0 +1,6 @@
variable "foo" {}
resource "aws_instance" "foo" {
foo = "foo"
count = "${var.foo}"
}