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

core: treat refs to unknown set resource attrs as unknown #4840

Merged
merged 1 commit into from
Jan 27, 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
10 changes: 10 additions & 0 deletions terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,16 @@ func (i *Interpolater) interpolateListAttribute(
log.Printf("[DEBUG] Interpolating computed list attribute %s (%s)",
resourceID, attr)

// In Terraform's internal dotted representation of list-like attributes, the
// ".#" count field is marked as unknown to indicate "this whole list is
// unknown". We must honor that meaning here so computed references can be
// treated properly during the plan phase.
if attr == config.UnknownVariableValue {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a comment help here? Maybe slightly rhetorical since I'm unsure at first glance as an outsider to this PR why this behavior needs to be here. At least covered by tests! :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - done in 48b172a

return attr, nil
}

// Otherwise we gather the values from the list-like attribute and return
// them.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the above guard, we'd plow right into the list building below even when the whole list was computed and end up returning an empty list, which was wrong.

var members []string
numberedListMember := regexp.MustCompile("^" + resourceID + "\\.[0-9]+$")
for id, value := range attributes {
Expand Down
38 changes: 38 additions & 0 deletions terraform/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,44 @@ func TestInterpolator_resourceMultiAttributesWithResourceCount(t *testing.T) {
})
}

func TestInterpolator_resourceMultiAttributesComputed(t *testing.T) {
lock := new(sync.RWMutex)
// The state would never be written with an UnknownVariableValue in it, but
// it can/does exist that way in memory during the plan phase.
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"aws_route53_zone.yada": &ResourceState{
Type: "aws_route53_zone",
Primary: &InstanceState{
ID: "z-abc123",
Attributes: map[string]string{
"name_servers.#": config.UnknownVariableValue,
},
},
},
},
},
},
}
i := &Interpolater{
Module: testModule(t, "interpolate-multi-vars"),
StateLock: lock,
State: state,
}

scope := &InterpolationScope{
Path: rootModulePath,
}

testInterpolate(t, i, scope, "aws_route53_zone.yada.name_servers", ast.Variable{
Value: config.UnknownVariableValue,
Type: ast.TypeString,
})
}

func getInterpolaterFixture(t *testing.T) *Interpolater {
lock := new(sync.RWMutex)
state := &State{
Expand Down