From b484ec19b626617b57cd56446cd4c3670df3e90d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Oct 2014 18:24:37 -0700 Subject: [PATCH] config: validate that count vars are valid types --- config/config.go | 17 +++++++++++++++++ config/config_test.go | 7 +++++++ .../validate-count-var-invalid/main.tf | 3 +++ 3 files changed, 27 insertions(+) create mode 100644 config/test-fixtures/validate-count-var-invalid/main.tf diff --git a/config/config.go b/config/config.go index b49dd685b0..fae135dec0 100644 --- a/config/config.go +++ b/config/config.go @@ -199,6 +199,23 @@ func (c *Config) Validate() error { } } + // Check that all count variables are valid. + for source, vs := range vars { + for _, v := range vs { + cv, ok := v.(*CountVariable) + if !ok { + continue + } + + if cv.Type == CountValueInvalid { + errs = append(errs, fmt.Errorf( + "%s: invalid count variable: %s", + source, + cv.FullKey())) + } + } + } + // Check that all references to modules are valid modules := make(map[string]*Module) dupped := make(map[string]struct{}) diff --git a/config/config_test.go b/config/config_test.go index 317d69edf9..e90d32b692 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -95,6 +95,13 @@ func TestConfigValidate_countUserVar(t *testing.T) { } } +func TestConfigValidate_countVarInvalid(t *testing.T) { + c := testConfig(t, "validate-count-var-invalid") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_dupModule(t *testing.T) { c := testConfig(t, "validate-dup-module") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-count-var-invalid/main.tf b/config/test-fixtures/validate-count-var-invalid/main.tf new file mode 100644 index 0000000000..a7f3fdb534 --- /dev/null +++ b/config/test-fixtures/validate-count-var-invalid/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "foo" { + foo = "${count.foo}" +}