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

terraform: allow indexing into a computed list for multi-count resources #10657

Merged
merged 1 commit into from
Dec 12, 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
25 changes: 25 additions & 0 deletions terraform/context_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,31 @@ func TestContext2Plan_computedList(t *testing.T) {
}
}

// GH-8695. This tests that you can index into a computed list on a
// splatted resource.
func TestContext2Plan_computedMultiIndex(t *testing.T) {
m := testModule(t, "plan-computed-multi-index")
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(testTerraformPlanComputedMultiIndexStr)
if actual != expected {
t.Fatalf("bad:\n%s", actual)
}
}

func TestContext2Plan_count(t *testing.T) {
m := testModule(t, "plan-count")
p := testProvider("aws")
Expand Down
2 changes: 1 addition & 1 deletion terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ func (i *Interpolater) computeResourceMultiVariable(
}

if multiAttr == unknownVariable {
return &ast.Variable{Type: ast.TypeString, Value: ""}, nil
return &unknownVariable, nil
}

values = append(values, multiAttr)
Expand Down
49 changes: 49 additions & 0 deletions terraform/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,55 @@ func TestInterpolater_resourceVariableMulti(t *testing.T) {
})
}

// When a splat reference is made to an attribute that is a computed list,
// the result should be unknown.
func TestInterpolater_resourceVariableMultiList(t *testing.T) {
lock := new(sync.RWMutex)
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_instance.web.0": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
Attributes: map[string]string{
"ip.#": config.UnknownVariableValue,
},
},
},

"aws_instance.web.1": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "bar",
Attributes: map[string]string{
"ip.#": "0",
},
},
},
},
},
},
}

i := &Interpolater{
Module: testModule(t, "interpolate-resource-variable"),
State: state,
StateLock: lock,
}

scope := &InterpolationScope{
Path: rootModulePath,
}

testInterpolate(t, i, scope, "aws_instance.web.*.ip", ast.Variable{
Value: config.UnknownVariableValue,
Type: ast.TypeUnknown,
})
}

func TestInterpolater_resourceVariableMulti_interpolated(t *testing.T) {
lock := new(sync.RWMutex)
state := &State{
Expand Down
18 changes: 18 additions & 0 deletions terraform/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,24 @@ STATE:
<no state>
`

const testTerraformPlanComputedMultiIndexStr = `
DIFF:

CREATE: aws_instance.bar
foo: "" => "<computed>"
type: "" => "aws_instance"
CREATE: aws_instance.foo.0
ip.#: "" => "<computed>"
type: "" => "aws_instance"
CREATE: aws_instance.foo.1
ip.#: "" => "<computed>"
type: "" => "aws_instance"

STATE:

<no state>
`

const testTerraformPlanCountStr = `
DIFF:

Expand Down
9 changes: 9 additions & 0 deletions terraform/test-fixtures/plan-computed-multi-index/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resource "aws_instance" "foo" {
count = 2
compute = "ip.#"
}

resource "aws_instance" "bar" {
count = 1
foo = "${aws_instance.foo.*.ip[count.index]}"
}