From fbf06e2a59c59250915c5848c0303c1737bbd937 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 25 Aug 2016 14:51:49 -0700 Subject: [PATCH] config: vars must be unique --- config/config.go | 6 ++++++ config/config_test.go | 7 +++++++ config/loader_hcl.go | 7 ++++--- config/test-fixtures/validate-var-default/main.tf | 2 +- config/test-fixtures/validate-var-dup/main.tf | 2 ++ 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 config/test-fixtures/validate-var-dup/main.tf diff --git a/config/config.go b/config/config.go index ac6749a31..d98ad854b 100644 --- a/config/config.go +++ b/config/config.go @@ -239,6 +239,12 @@ func (c *Config) Validate() error { vars := c.InterpolatedVariables() varMap := make(map[string]*Variable) for _, v := range c.Variables { + if _, ok := varMap[v.Name]; ok { + errs = append(errs, fmt.Errorf( + "Variable '%s': duplicate found. Variable names must be unique.", + v.Name)) + } + varMap[v.Name] = v } diff --git a/config/config_test.go b/config/config_test.go index 81bd11b23..bbbb7fa06 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -447,6 +447,13 @@ func TestConfigValidate_varDefaultInterpolate(t *testing.T) { } } +func TestConfigValidate_varDup(t *testing.T) { + c := testConfig(t, "validate-var-dup") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_varMultiExactNonSlice(t *testing.T) { c := testConfig(t, "validate-var-multi-exact-non-slice") if err := c.Validate(); err != nil { diff --git a/config/loader_hcl.go b/config/loader_hcl.go index 8c81156e4..4adf5ab45 100644 --- a/config/loader_hcl.go +++ b/config/loader_hcl.go @@ -29,6 +29,7 @@ func (t *hclConfigurable) Config() (*Config, error) { } type hclVariable struct { + Name string `hcl:",key"` Default interface{} Description string DeclaredType string `hcl:"type"` @@ -36,7 +37,7 @@ func (t *hclConfigurable) Config() (*Config, error) { } var rawConfig struct { - Variable map[string]*hclVariable + Variable []*hclVariable } // Top-level item should be the object list @@ -56,7 +57,7 @@ func (t *hclConfigurable) Config() (*Config, error) { config := new(Config) if len(rawConfig.Variable) > 0 { config.Variables = make([]*Variable, 0, len(rawConfig.Variable)) - for k, v := range rawConfig.Variable { + for _, v := range rawConfig.Variable { // Defaults turn into a slice of map[string]interface{} and // we need to make sure to convert that down into the // proper type for Config. @@ -72,7 +73,7 @@ func (t *hclConfigurable) Config() (*Config, error) { } newVar := &Variable{ - Name: k, + Name: v.Name, DeclaredType: v.DeclaredType, Default: v.Default, Description: v.Description, diff --git a/config/test-fixtures/validate-var-default/main.tf b/config/test-fixtures/validate-var-default/main.tf index 022227501..80db1843d 100644 --- a/config/test-fixtures/validate-var-default/main.tf +++ b/config/test-fixtures/validate-var-default/main.tf @@ -2,7 +2,7 @@ variable "foo" { default = "bar" } -variable "foo" { +variable "foo2" { default = { foo = "bar" } diff --git a/config/test-fixtures/validate-var-dup/main.tf b/config/test-fixtures/validate-var-dup/main.tf new file mode 100644 index 000000000..6f75d8d47 --- /dev/null +++ b/config/test-fixtures/validate-var-dup/main.tf @@ -0,0 +1,2 @@ +variable "foo" {} +variable "foo" {}