From 20da842bcfb1b0a903d76536795918f1dfd4126f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 22 Jul 2014 17:16:48 -0700 Subject: [PATCH] config: validate dependsOn --- config/config.go | 11 +++++++++++ config/config_test.go | 7 +++++++ config/test-fixtures/validate-bad-depends-on/main.tf | 3 +++ config/test-fixtures/validate-good/main.tf | 2 ++ 4 files changed, 23 insertions(+) create mode 100644 config/test-fixtures/validate-bad-depends-on/main.tf diff --git a/config/config.go b/config/config.go index f2c8b41b5..012fcee6f 100644 --- a/config/config.go +++ b/config/config.go @@ -155,6 +155,17 @@ func (c *Config) Validate() error { } dupped = nil + // Make sure all dependsOn are valid in resources + for n, r := range resources { + for _, d := range r.DependsOn { + if _, ok := resources[d]; !ok { + errs = append(errs, fmt.Errorf( + "%s: resource depends on non-existent resource '%s'", + n, d)) + } + } + } + for source, vs := range vars { for _, v := range vs { rv, ok := v.(*ResourceVariable) diff --git a/config/config_test.go b/config/config_test.go index 9b141fa8f..508a281ab 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -16,6 +16,13 @@ func TestConfigValidate(t *testing.T) { } } +func TestConfigValidate_badDependsOn(t *testing.T) { + c := testConfig(t, "validate-bad-depends-on") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_badMultiResource(t *testing.T) { c := testConfig(t, "validate-bad-multi-resource") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-bad-depends-on/main.tf b/config/test-fixtures/validate-bad-depends-on/main.tf new file mode 100644 index 000000000..60ca18c7e --- /dev/null +++ b/config/test-fixtures/validate-bad-depends-on/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "web" { + depends_on = ["aws_instance.db"] +} diff --git a/config/test-fixtures/validate-good/main.tf b/config/test-fixtures/validate-good/main.tf index 965877a10..5a4f87159 100644 --- a/config/test-fixtures/validate-good/main.tf +++ b/config/test-fixtures/validate-good/main.tf @@ -32,4 +32,6 @@ resource aws_instance "web" { device_index = 0 description = "Main network interface" } + + depends_on = ["aws_security_group.firewall"] }