From dd1430302250df25311c12428abbb9df88a1ea35 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 2 Oct 2014 16:51:20 -0700 Subject: [PATCH] config: validate that count is an int --- config/config.go | 12 +++++++ config/config_test.go | 14 ++++++++ config/raw_config.go | 36 ++++++++++--------- .../test-fixtures/validate-count-int/main.tf | 5 +++ .../validate-count-not-int/main.tf | 5 +++ 5 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 config/test-fixtures/validate-count-int/main.tf create mode 100644 config/test-fixtures/validate-count-not-int/main.tf diff --git a/config/config.go b/config/config.go index ef8be866d..4d6ea1a85 100644 --- a/config/config.go +++ b/config/config.go @@ -275,6 +275,18 @@ func (c *Config) Validate() error { } } + // Interpolate with a fixed number to verify that its a number + r.RawCount.interpolate(func(Interpolation) (string, error) { + return "5", nil + }) + _, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0) + if err != nil { + errs = append(errs, fmt.Errorf( + "%s: resource count must be an integer", + n)) + } + r.RawCount.init() + for _, d := range r.DependsOn { if _, ok := resources[d]; !ok { errs = append(errs, fmt.Errorf( diff --git a/config/config_test.go b/config/config_test.go index 636af0b58..76c12d4a0 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -53,6 +53,13 @@ func TestConfigValidate_badDependsOn(t *testing.T) { } } +func TestConfigValidate_countInt(t *testing.T) { + c := testConfig(t, "validate-count-int") + if err := c.Validate(); err != nil { + t.Fatalf("err: %s", err) + } +} + func TestConfigValidate_countModuleVar(t *testing.T) { c := testConfig(t, "validate-count-module-var") if err := c.Validate(); err == nil { @@ -60,6 +67,13 @@ func TestConfigValidate_countModuleVar(t *testing.T) { } } +func TestConfigValidate_countNotInt(t *testing.T) { + c := testConfig(t, "validate-count-not-int") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_countResourceVar(t *testing.T) { c := testConfig(t, "validate-count-resource-var") if err := c.Validate(); err == nil { diff --git a/config/raw_config.go b/config/raw_config.go index f3b144800..e1c6c4108 100644 --- a/config/raw_config.go +++ b/config/raw_config.go @@ -79,24 +79,9 @@ func (r *RawConfig) Config() map[string]interface{} { // // If a variable key is missing, this will panic. func (r *RawConfig) Interpolate(vs map[string]string) error { - config, err := copystructure.Copy(r.Raw) - if err != nil { - return err - } - r.config = config.(map[string]interface{}) - - fn := func(i Interpolation) (string, error) { + return r.interpolate(func(i Interpolation) (string, error) { return i.Interpolate(vs) - } - - w := &interpolationWalker{F: fn, Replace: true} - err = reflectwalk.Walk(r.config, w) - if err != nil { - return err - } - - r.unknownKeys = w.unknownKeys - return nil + }) } func (r *RawConfig) init() error { @@ -126,6 +111,23 @@ func (r *RawConfig) init() error { return nil } +func (r *RawConfig) interpolate(fn interpolationWalkerFunc) error { + config, err := copystructure.Copy(r.Raw) + if err != nil { + return err + } + r.config = config.(map[string]interface{}) + + w := &interpolationWalker{F: fn, Replace: true} + err = reflectwalk.Walk(r.config, w) + if err != nil { + return err + } + + r.unknownKeys = w.unknownKeys + return nil +} + func (r *RawConfig) merge(r2 *RawConfig) *RawConfig { rawRaw, err := copystructure.Copy(r.Raw) if err != nil { diff --git a/config/test-fixtures/validate-count-int/main.tf b/config/test-fixtures/validate-count-int/main.tf new file mode 100644 index 000000000..7b27c2202 --- /dev/null +++ b/config/test-fixtures/validate-count-int/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +resource "aws_instance" "web" { + count = "${var.foo}" +} diff --git a/config/test-fixtures/validate-count-not-int/main.tf b/config/test-fixtures/validate-count-not-int/main.tf new file mode 100644 index 000000000..af34d34c6 --- /dev/null +++ b/config/test-fixtures/validate-count-not-int/main.tf @@ -0,0 +1,5 @@ +variable "foo" {} + +resource "aws_instance" "web" { + count = "nope${var.foo}" +}