From 965fe45b9e92ca3be34f744e57fee70b7946c1b4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Feb 2015 14:43:14 -0800 Subject: [PATCH] config: self var validation --- config/config.go | 16 ++++++++++++++++ config/config_test.go | 14 ++++++++++++++ .../validate-resource-prov-self/main.tf | 11 +++++++++++ .../test-fixtures/validate-resource-self/main.tf | 3 +++ 4 files changed, 44 insertions(+) create mode 100644 config/test-fixtures/validate-resource-prov-self/main.tf create mode 100644 config/test-fixtures/validate-resource-self/main.tf diff --git a/config/config.go b/config/config.go index 99295ab8f8..8dd9810ebf 100644 --- a/config/config.go +++ b/config/config.go @@ -477,6 +477,22 @@ func (c *Config) Validate() error { } } + // Validate the self variable + for source, rc := range c.rawConfigs() { + // Ignore provisioners. This is a pretty brittle way to do this, + // but better than also repeating all the resources. + if strings.Contains(source, "provision") { + continue + } + + for _, v := range rc.Variables { + if _, ok := v.(*SelfVariable); ok { + errs = append(errs, fmt.Errorf( + "%s: cannot contain self-reference %s", source, v.FullKey())) + } + } + } + if len(errs) > 0 { return &multierror.Error{Errors: errs} } diff --git a/config/config_test.go b/config/config_test.go index 821539ccf8..0503d2e667 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -214,6 +214,20 @@ func TestConfigValidate_provSplatSelf(t *testing.T) { } } +func TestConfigValidate_resourceProvVarSelf(t *testing.T) { + c := testConfig(t, "validate-resource-prov-self") + if err := c.Validate(); err != nil { + t.Fatalf("should be valid: %s", err) + } +} + +func TestConfigValidate_resourceVarSelf(t *testing.T) { + c := testConfig(t, "validate-resource-self") + if err := c.Validate(); err == nil { + t.Fatal("should not be valid") + } +} + func TestConfigValidate_unknownThing(t *testing.T) { c := testConfig(t, "validate-unknownthing") if err := c.Validate(); err == nil { diff --git a/config/test-fixtures/validate-resource-prov-self/main.tf b/config/test-fixtures/validate-resource-prov-self/main.tf new file mode 100644 index 0000000000..4a55ac24ec --- /dev/null +++ b/config/test-fixtures/validate-resource-prov-self/main.tf @@ -0,0 +1,11 @@ +resource "aws_instance" "foo" { + foo = "bar" + + connection { + host = "${self.foo}" + } + + provisioner "shell" { + value = "${self.foo}" + } +} diff --git a/config/test-fixtures/validate-resource-self/main.tf b/config/test-fixtures/validate-resource-self/main.tf new file mode 100644 index 0000000000..20049d55bb --- /dev/null +++ b/config/test-fixtures/validate-resource-self/main.tf @@ -0,0 +1,3 @@ +resource "aws_instance" "foo" { + foo = "${self.bar}" +}