Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: provisioner splat vars can only reference other resources #1016

Merged
merged 3 commits into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}"
}
}