Merge pull request #4747 from hashicorp/b-escaped
Literals with escaped interpolations work
This commit is contained in:
commit
911575b7d6
|
@ -118,9 +118,15 @@ func (w *interpolationWalker) Primitive(v reflect.Value) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// If the AST we got is just a literal string value, then we ignore it
|
||||
if _, ok := astRoot.(*ast.LiteralNode); ok {
|
||||
return nil
|
||||
// If the AST we got is just a literal string value with the same
|
||||
// value then we ignore it. We have to check if its the same value
|
||||
// because it is possible to input a string, get out a string, and
|
||||
// have it be different. For example: "foo-$${bar}" turns into
|
||||
// "foo-${bar}"
|
||||
if n, ok := astRoot.(*ast.LiteralNode); ok {
|
||||
if s, ok := n.Value.(string); ok && s == v.String() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if w.ContextF != nil {
|
||||
|
|
|
@ -18,7 +18,9 @@ func TestInterpolationWalker_detect(t *testing.T) {
|
|||
Input: map[string]interface{}{
|
||||
"foo": "$${var.foo}",
|
||||
},
|
||||
Result: nil,
|
||||
Result: []string{
|
||||
"Literal(TypeString, ${var.foo})",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
|
@ -114,7 +116,7 @@ func TestInterpolationWalker_replace(t *testing.T) {
|
|||
"foo": "$${var.foo}",
|
||||
},
|
||||
Output: map[string]interface{}{
|
||||
"foo": "$${var.foo}",
|
||||
"foo": "bar",
|
||||
},
|
||||
Value: "bar",
|
||||
},
|
||||
|
|
|
@ -24,6 +24,14 @@ func TestEval(t *testing.T) {
|
|||
ast.TypeString,
|
||||
},
|
||||
|
||||
{
|
||||
"foo $${bar}",
|
||||
nil,
|
||||
false,
|
||||
"foo ${bar}",
|
||||
ast.TypeString,
|
||||
},
|
||||
|
||||
{
|
||||
"foo ${bar}",
|
||||
&ast.BasicScope{
|
||||
|
|
|
@ -114,6 +114,38 @@ func TestRawConfig_double(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRawConfigInterpolate_escaped(t *testing.T) {
|
||||
raw := map[string]interface{}{
|
||||
"foo": "bar-$${baz}",
|
||||
}
|
||||
|
||||
rc, err := NewRawConfig(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Before interpolate, Config() should be the raw
|
||||
if !reflect.DeepEqual(rc.Config(), raw) {
|
||||
t.Fatalf("bad: %#v", rc.Config())
|
||||
}
|
||||
|
||||
if err := rc.Interpolate(nil); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := rc.Config()
|
||||
expected := map[string]interface{}{
|
||||
"foo": "bar-${baz}",
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(actual, expected) {
|
||||
t.Fatalf("bad: %#v", actual)
|
||||
}
|
||||
if len(rc.UnknownKeys()) != 0 {
|
||||
t.Fatalf("bad: %#v", rc.UnknownKeys())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRawConfig_merge(t *testing.T) {
|
||||
raw1 := map[string]interface{}{
|
||||
"foo": "${var.foo}",
|
||||
|
|
|
@ -104,6 +104,29 @@ func TestContext2Plan_emptyDiff(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestContext2Plan_escapedVar(t *testing.T) {
|
||||
m := testModule(t, "plan-escaped-var")
|
||||
p := testProvider("aws")
|
||||
p.DiffFn = testDiffFn
|
||||
ctx := testContext2(t, &ContextOpts{
|
||||
Module: m,
|
||||
Providers: map[string]ResourceProviderFactory{
|
||||
"aws": testProviderFuncFixed(p),
|
||||
},
|
||||
})
|
||||
|
||||
plan, err := ctx.Plan()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
actual := strings.TrimSpace(plan.String())
|
||||
expected := strings.TrimSpace(testTerraformPlanEscapedVarStr)
|
||||
if actual != expected {
|
||||
t.Fatalf("bad:\n%s", actual)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContext2Plan_minimal(t *testing.T) {
|
||||
m := testModule(t, "plan-empty")
|
||||
p := testProvider("aws")
|
||||
|
|
|
@ -983,6 +983,18 @@ STATE:
|
|||
<no state>
|
||||
`
|
||||
|
||||
const testTerraformPlanEscapedVarStr = `
|
||||
DIFF:
|
||||
|
||||
CREATE: aws_instance.foo
|
||||
foo: "" => "bar-${baz}"
|
||||
type: "" => "aws_instance"
|
||||
|
||||
STATE:
|
||||
|
||||
<no state>
|
||||
`
|
||||
|
||||
const testTerraformPlanModulesStr = `
|
||||
DIFF:
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
resource "aws_instance" "foo" {
|
||||
foo = "bar-$${baz}"
|
||||
}
|
Loading…
Reference in New Issue