config: self var validation

This commit is contained in:
Mitchell Hashimoto 2015-02-23 14:43:14 -08:00
parent 9c612964d8
commit 965fe45b9e
4 changed files with 44 additions and 0 deletions

View File

@ -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}
}

View File

@ -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 {

View File

@ -0,0 +1,11 @@
resource "aws_instance" "foo" {
foo = "bar"
connection {
host = "${self.foo}"
}
provisioner "shell" {
value = "${self.foo}"
}
}

View File

@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
foo = "${self.bar}"
}