Skip to content

Commit

Permalink
Allow escaped interpolation-like sequences in variable defaults
Browse files Browse the repository at this point in the history
The variable validator assumes that any AST node it gets from an
interpolation walk is an indicator of an interpolation. Unfortunately,
back in f223be1 we changed the interpolation walker to emit a LiteralNode
as a way to signal that the result is a literal but not identical to the
input due to escapes.

The existence of this issue suggests a bit of a design smell in that the
interpolation walker interface at first glance appears to skip over all
literals, but it actually emits them in this one situation. In the long
run we should perhaps think about whether the abstraction is right here,
but this is a shallow, tactical change that fixes #13001.
  • Loading branch information
apparentlymart committed Mar 28, 2017
1 parent 8b1df45 commit 3a90252
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions command/test-fixtures/validate-valid/main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
variable "var_with_escaped_interp" {
# This is here because in the past it failed. See Github #13001
default = "foo-$${bar.baz}"
}

resource "test_instance" "foo" {
ami = "bar"

Expand Down
6 changes: 4 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,10 @@ func (c *Config) Validate() error {
}

interp := false
fn := func(ast.Node) (interface{}, error) {
interp = true
fn := func(n ast.Node) (interface{}, error) {
if _, ok := n.(*ast.LiteralNode); !ok {
interp = true
}
return "", nil
}

Expand Down
7 changes: 7 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,13 @@ func TestConfigValidate_varDefaultInterpolate(t *testing.T) {
}
}

func TestConfigValidate_varDefaultInterpolateEscaped(t *testing.T) {
c := testConfig(t, "validate-var-default-interpolate-escaped")
if err := c.Validate(); err != nil {
t.Fatalf("should be valid, but got err: %s", err)
}
}

func TestConfigValidate_varDup(t *testing.T) {
c := testConfig(t, "validate-var-dup")
if err := c.Validate(); err == nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "foo" {
# This should be considered valid since the sequence is escaped and is
# thus not actually an interpolation.
default = "foo bar $${aws_instance.foo.bar}"
}

0 comments on commit 3a90252

Please sign in to comment.