From 50095612a217cc3145bc669ab6777fe8893298a8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 18 Jul 2014 16:31:01 -0700 Subject: [PATCH] config: resource should be unique --- config/config.go | 11 +++++++++++ config/config_test.go | 7 +++++++ config/test-fixtures/validate-dup-resource/main.tf | 7 +++++++ 3 files changed, 25 insertions(+) create mode 100644 config/test-fixtures/validate-dup-resource/main.tf diff --git a/config/config.go b/config/config.go index e9f1535eb..07c9005e7 100644 --- a/config/config.go +++ b/config/config.go @@ -145,7 +145,18 @@ func (c *Config) Validate() error { // Check that all references to resources are valid resources := make(map[string]*Resource) + dupped := make(map[string]struct{}) for _, r := range c.Resources { + if _, ok := resources[r.Id()]; ok { + if _, ok := dupped[r.Id()]; !ok { + dupped[r.Id()] = struct{}{} + + errs = append(errs, fmt.Errorf( + "%s: resource repeated multiple times", + r.Id())) + } + } + resources[r.Id()] = r } for source, vs := range vars { diff --git a/config/config_test.go b/config/config_test.go index f9f539406..1a5ecb8d9 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -22,6 +22,13 @@ func TestConfigValidate_badMultiResource(t *testing.T) { } } +func TestConfigValidate_dupResource(t *testing.T) { + c := testConfig(t, "validate-dup-resource") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_outputBadField(t *testing.T) { c := testConfig(t, "validate-output-bad-field") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-dup-resource/main.tf b/config/test-fixtures/validate-dup-resource/main.tf new file mode 100644 index 000000000..23a66aa31 --- /dev/null +++ b/config/test-fixtures/validate-dup-resource/main.tf @@ -0,0 +1,7 @@ +resource "aws_instance" "web" { + count = 5 +} + +resource "aws_instance" "web" { + count = 10 +}