From fe2a3063418a54c2e744dd59bf1e48ed498cc0d5 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 11 Aug 2014 09:46:56 -0700 Subject: [PATCH] config: validate no interp in var [GH-180] --- CHANGELOG.md | 2 ++ config/config.go | 17 +++++++++++++++++ config/config_test.go | 7 +++++++ .../validate-var-default-interpolate/main.tf | 3 +++ 4 files changed, 29 insertions(+) create mode 100644 config/test-fixtures/validate-var-default-interpolate/main.tf diff --git a/CHANGELOG.md b/CHANGELOG.md index 32d2581639..c26461b206 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## 0.2.0 (unreleased) +BUG FIXES: + * core: Variables are validated to not contain interpolations. [GH-180] ## 0.1.1 (August 5, 2014) diff --git a/config/config.go b/config/config.go index 6e01c4231c..b89ffcf297 100644 --- a/config/config.go +++ b/config/config.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform/flatmap" "github.com/hashicorp/terraform/helper/multierror" "github.com/mitchellh/mapstructure" + "github.com/mitchellh/reflectwalk" ) // Config is the configuration that comes from loading a collection @@ -116,6 +117,22 @@ func (c *Config) Validate() error { errs = append(errs, fmt.Errorf( "Variable '%s': must be string or mapping", v.Name)) + continue + } + + interp := false + fn := func(i Interpolation) (string, error) { + interp = true + return "", nil + } + + w := &interpolationWalker{F: fn} + if err := reflectwalk.Walk(v.Default, w); err == nil { + if interp { + errs = append(errs, fmt.Errorf( + "Variable '%s': cannot contain interpolations", + v.Name)) + } } } diff --git a/config/config_test.go b/config/config_test.go index e5b6653a97..84f10a528e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -100,6 +100,13 @@ func TestConfigValidate_varDefaultBadType(t *testing.T) { } } +func TestConfigValidate_varDefaultInterpolate(t *testing.T) { + c := testConfig(t, "validate-var-default-interpolate") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestProviderConfigName(t *testing.T) { pcs := []*ProviderConfig{ &ProviderConfig{Name: "aw"}, diff --git a/config/test-fixtures/validate-var-default-interpolate/main.tf b/config/test-fixtures/validate-var-default-interpolate/main.tf new file mode 100644 index 0000000000..f9dfe151ec --- /dev/null +++ b/config/test-fixtures/validate-var-default-interpolate/main.tf @@ -0,0 +1,3 @@ +variable "foo" { + default = "${aws_instance.foo.bar}" +}