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: multi-var interpolation should use state for count #9361

Merged
merged 1 commit into from
Oct 14, 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
68 changes: 54 additions & 14 deletions terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,17 +490,14 @@ func (i *Interpolater) computeResourceMultiVariable(
return nil, err
}

// Get the count so we know how many to iterate over
count, err := cr.Count()
// Get the keys for all the resources that are created for this resource
resourceKeys, err := i.resourceCountKeys(module, cr, v)
if err != nil {
return nil, fmt.Errorf(
"Error reading %s count: %s",
v.ResourceId(),
err)
return nil, err
}

// If count is zero, we return an empty list
if count == 0 {
if len(resourceKeys) == 0 {
return &ast.Variable{Type: ast.TypeList, Value: []ast.Variable{}}, nil
}

Expand All @@ -510,13 +507,15 @@ func (i *Interpolater) computeResourceMultiVariable(
}

var values []interface{}
for j := 0; j < count; j++ {
id := fmt.Sprintf("%s.%d", v.ResourceId(), j)

// If we're dealing with only a single resource, then the
// ID doesn't have a trailing index.
if count == 1 {
id = v.ResourceId()
for _, id := range resourceKeys {
// ID doesn't have a trailing index. We try both here, but if a value
// without a trailing index is found we prefer that. This choice
// is for legacy reasons: older versions of TF preferred it.
if id == v.ResourceId()+".0" {
potential := v.ResourceId()
if _, ok := module.Resources[potential]; ok {
id = potential
}
}

r, ok := module.Resources[id]
Expand Down Expand Up @@ -678,3 +677,44 @@ func (i *Interpolater) resourceVariableInfo(
module := i.State.ModuleByPath(scope.Path)
return module, cr, nil
}

func (i *Interpolater) resourceCountKeys(
ms *ModuleState,
cr *config.Resource,
v *config.ResourceVariable) ([]string, error) {
id := v.ResourceId()

// If we're NOT applying, then we assume we can read the count
// from the state. Plan and so on may not have any state yet so
// we do a full interpolation.
if i.Operation != walkApply {
count, err := cr.Count()
if err != nil {
return nil, err
}

result := make([]string, count)
for i := 0; i < count; i++ {
result[i] = fmt.Sprintf("%s.%d", id, i)
}

return result, nil
}

// We need to determine the list of resource keys to get values from.
// This needs to be sorted so the order is deterministic. We used to
// use "cr.Count()" but that doesn't work if the count is interpolated
// and we can't guarantee that so we instead depend on the state.
var resourceKeys []string
for k, _ := range ms.Resources {
// If we don't have the right prefix then ignore it
if k != id && !strings.HasPrefix(k, id+".") {
continue
}

// Add it to the list
resourceKeys = append(resourceKeys, k)
}
sort.Strings(resourceKeys)
return resourceKeys, nil
}
43 changes: 43 additions & 0 deletions terraform/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,49 @@ func TestInterpolater_resourceVariableMulti(t *testing.T) {
})
}

func TestInterpolater_resourceVariableMulti_interpolated(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: "a",
Attributes: map[string]string{"foo": "a"},
},
},

"aws_instance.web.1": &ResourceState{
Type: "aws_instance",
Primary: &InstanceState{
ID: "b",
Attributes: map[string]string{"foo": "b"},
},
},
},
},
},
}

i := &Interpolater{
Operation: walkApply,
Module: testModule(t, "interpolate-multi-interp"),
State: state,
StateLock: lock,
}

scope := &InterpolationScope{
Path: rootModulePath,
}

expected := []interface{}{"a", "b"}
testInterpolate(t, i, scope, "aws_instance.web.*.foo",
interfaceToVariableSwallowError(expected))
}

func interfaceToVariableSwallowError(input interface{}) ast.Variable {
variable, _ := hil.InterfaceToVariable(input)
return variable
Expand Down
3 changes: 3 additions & 0 deletions terraform/test-fixtures/interpolate-multi-interp/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_instance" "web" {
count = "${var.c}"
}