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

Interpolate variable during Input and Validate #6833

Merged
merged 3 commits into from
May 24, 2016
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
23 changes: 23 additions & 0 deletions terraform/context_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,3 +569,26 @@ func TestContext2Input_varPartiallyComputed(t *testing.T) {
t.Fatalf("err: %s", err)
}
}

// Module variables weren't being interpolated during the Input walk.
// https://github.com/hashicorp/terraform/issues/5322
func TestContext2Input_interpolateVar(t *testing.T) {
input := new(MockUIInput)

m := testModule(t, "input-interpolate-var")
p := testProvider("null")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn

ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
},
UIInput: input,
})

if err := ctx.Input(InputModeStd); err != nil {
t.Fatalf("err: %s", err)
}
}
54 changes: 54 additions & 0 deletions terraform/context_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,57 @@ func TestContext2Validate_varRefFilled(t *testing.T) {
t.Fatalf("bad: %#v", value)
}
}

// Module variables weren't being interpolated during Validate phase.
// related to https://github.com/hashicorp/terraform/issues/5322
func TestContext2Validate_interpolateVar(t *testing.T) {
input := new(MockUIInput)

m := testModule(t, "input-interpolate-var")
p := testProvider("null")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn

ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"template": testProviderFuncFixed(p),
},
UIInput: input,
})

w, e := ctx.Validate()
if w != nil {
t.Log("warnings:", w)
}
if e != nil {
t.Fatal("err:", e)
}
}

// When module vars reference something that is actually computed, this
// shouldn't cause validation to fail.
func TestContext2Validate_interpolateComputedModuleVarDef(t *testing.T) {
input := new(MockUIInput)

m := testModule(t, "validate-computed-module-var-ref")
p := testProvider("aws")
p.ApplyFn = testApplyFn
p.DiffFn = testDiffFn

ctx := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
UIInput: input,
})

w, e := ctx.Validate()
if w != nil {
t.Log("warnings:", w)
}
if e != nil {
t.Fatal("err:", e)
}
}
1 change: 1 addition & 0 deletions terraform/eval_context_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ func (ctx *BuiltinEvalContext) Interpolate(
Path: ctx.Path(),
Resource: r,
}

vs, err := ctx.Interpolater.Values(scope, cfg.Variables)
if err != nil {
return nil, err
Expand Down
4 changes: 1 addition & 3 deletions terraform/eval_interpolate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package terraform

import (
"github.com/hashicorp/terraform/config"
)
import "github.com/hashicorp/terraform/config"

// EvalInterpolate is an EvalNode implementation that takes a raw
// configuration and interpolates it.
Expand Down
3 changes: 2 additions & 1 deletion terraform/graph_config_node_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func (n *GraphNodeConfigOutput) DependentOn() []string {
// GraphNodeEvalable impl.
func (n *GraphNodeConfigOutput) EvalTree() EvalNode {
return &EvalOpFilter{
Ops: []walkOperation{walkRefresh, walkPlan, walkApply, walkDestroy},
Ops: []walkOperation{walkRefresh, walkPlan, walkApply,
walkDestroy, walkInput, walkValidate},
Node: &EvalSequence{
Nodes: []EvalNode{
&EvalWriteOutput{
Expand Down
9 changes: 0 additions & 9 deletions terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,6 @@ func (i *Interpolater) valueModuleVar(
n string,
v *config.ModuleVariable,
result map[string]ast.Variable) error {
// If we're computing all dynamic fields, then module vars count
// and we mark it as computed.
if i.Operation == walkValidate {
result[n] = ast.Variable{
Value: config.UnknownVariableValue,
Type: ast.TypeString,
}
return nil
}

// Build the path to the child module we want
path := make([]string, len(scope.Path), len(scope.Path)+1)
Expand Down
5 changes: 5 additions & 0 deletions terraform/test-fixtures/input-interpolate-var/child/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "list" { }
resource "template_file" "temp" {
count = "${length(split(",", var.list))}"
template = "foo"
}
7 changes: 7 additions & 0 deletions terraform/test-fixtures/input-interpolate-var/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "source" {
source = "./source"
}
module "child" {
source = "./child"
list = "${module.source.list}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output "list" { value = "foo,bar,baz" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "destin" { }

resource "aws_instance" "dest" {
attr = "${var.destin}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module "source" {
source = "./source"
}

module "dest" {
source = "./dest"
destin = "${module.source.sourceout}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_instance" "source" { }

output "sourceout" { value = "${aws_instance.source.id}" }