Skip to content

Commit

Permalink
Merge pull request #1016 from hashicorp/b-splat-validate
Browse files Browse the repository at this point in the history
config: provisioner splat vars can only reference other resources
  • Loading branch information
mitchellh committed Feb 23, 2015
2 parents 8a9c8a8 + c14e84a commit 87ecf4f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
34 changes: 34 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,40 @@ func (c *Config) Validate() error {
n, d))
}
}

// Verify provisioners don't contain any splats
for _, p := range r.Provisioners {
// This validation checks that there are now splat variables
// referencing ourself. This currently is not allowed.

for _, v := range p.ConnInfo.Variables {
rv, ok := v.(*ResourceVariable)
if !ok {
continue
}

if rv.Multi && rv.Index == -1 && rv.Type == r.Type && rv.Name == r.Name {
errs = append(errs, fmt.Errorf(
"%s: connection info cannot contain splat variable "+
"referencing itself", n))
break
}
}

for _, v := range p.RawConfig.Variables {
rv, ok := v.(*ResourceVariable)
if !ok {
continue
}

if rv.Multi && rv.Index == -1 && rv.Type == r.Type && rv.Name == r.Name {
errs = append(errs, fmt.Errorf(
"%s: connection info cannot contain splat variable "+
"referencing itself", n))
break
}
}
}
}

for source, vs := range vars {
Expand Down
28 changes: 28 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,34 @@ func TestConfigValidate_pathVarInvalid(t *testing.T) {
}
}

func TestConfigValidate_provConnSplatOther(t *testing.T) {
c := testConfig(t, "validate-prov-conn-splat-other")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}

func TestConfigValidate_provConnSplatSelf(t *testing.T) {
c := testConfig(t, "validate-prov-conn-splat-self")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}

func TestConfigValidate_provSplatOther(t *testing.T) {
c := testConfig(t, "validate-prov-splat-other")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid: %s", err)
}
}

func TestConfigValidate_provSplatSelf(t *testing.T) {
c := testConfig(t, "validate-prov-splat-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 {
Expand Down
9 changes: 9 additions & 0 deletions config/test-fixtures/validate-prov-conn-splat-other/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "aws_instance" "foo" {}

resource "aws_instance" "bar" {
connection {
host = "${element(aws_instance.foo.*.private_ip, 0)}"
}

provisioner "local-exec" {}
}
7 changes: 7 additions & 0 deletions config/test-fixtures/validate-prov-conn-splat-self/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_instance" "bar" {
connection {
host = "${element(aws_instance.bar.*.private_ip, 0)}"
}

provisioner "local-exec" {}
}
7 changes: 7 additions & 0 deletions config/test-fixtures/validate-prov-splat-other/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_instance" "foo" {}

resource "aws_instance" "bar" {
provisioner "local-exec" {
command = "${element(aws_instance.foo.*.private_ip, 0)}"
}
}
7 changes: 7 additions & 0 deletions config/test-fixtures/validate-prov-splat-self/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_instance" "foo" {}

resource "aws_instance" "bar" {
provisioner "local-exec" {
command = "${element(aws_instance.bar.*.private_ip, 0)}"
}
}

0 comments on commit 87ecf4f

Please sign in to comment.