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

Accessing a resource's computed list or set attribute #1573

Closed
Closed
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
59 changes: 53 additions & 6 deletions terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"strings"
"sync"
"strconv"
"sort"

"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/lang/ast"
Expand Down Expand Up @@ -324,6 +326,27 @@ func (i *Interpolater) computeResourceVariable(
return attr, nil
}

// Check sets and lists for attributes
if parts := strings.Split(v.Field, "."); len(parts) > 1 {
for i := 1; i < len(parts); i++ {

// Sets and lists have an attribute that is their size
// so use that to check if it has been computed or not yet
key := fmt.Sprintf("%s.#", strings.Join(parts[:i], "."))
if attr, ok := r.Primary.Attributes[key]; ok {

// If the number of instances has not been computed yet, don't try
// to find the values
if _, err := strconv.ParseInt(attr, 0, 0); err != nil {
return attr, nil
}

values := computeValuesFromParts(parts, r.Primary.Attributes)
return strings.Join(values, config.InterpSplitDelim), nil
}
}
}

// At apply time, we can't do the "maybe has it" check below
// that we need for plans since parent elements might be computed.
// Therefore, it is an error and we're missing the key.
Expand All @@ -341,14 +364,9 @@ func (i *Interpolater) computeResourceVariable(
// a computed list. If so, then the whole thing is computed.
if parts := strings.Split(v.Field, "."); len(parts) > 1 {
for i := 1; i < len(parts); i++ {
// Lists and sets make this
key := fmt.Sprintf("%s.#", strings.Join(parts[:i], "."))
if attr, ok := r.Primary.Attributes[key]; ok {
return attr, nil
}

// Maps make this
key = fmt.Sprintf("%s", strings.Join(parts[:i], "."))
key := fmt.Sprintf("%s", strings.Join(parts[:i], "."))
if attr, ok := r.Primary.Attributes[key]; ok {
return attr, nil
}
Expand All @@ -372,6 +390,35 @@ MISSING:
v.FullKey())
}

func computeValuesFromParts(parts []string, attributes map[string]string) []string {
var values []string

attributeName := parts[0]
attributeIndex := parts[1]

for attribute, value := range attributes {
if attributeParts := strings.Split(attribute, "."); attributeParts[0] == attributeName && attributeParts[1] != "#" {
if len(parts) <= 2 {
values = append(values, value)
} else if nestedAttribute := parts[2]; attributeParts[2] == nestedAttribute {
values = append(values, value)
}
}
}

sort.StringSlice(values).Sort()

if index, err := strconv.ParseInt(attributeIndex, 0, 0); err == nil {
if int(index) < len(values) {
values = []string{values[int(index)]}
} else {
values = []string{}
}
}

return values
}

func (i *Interpolater) computeResourceMultiVariable(
scope *InterpolationScope,
v *config.ResourceVariable) (string, error) {
Expand Down
118 changes: 118 additions & 0 deletions terraform/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"sync"
"testing"
"strings"

"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/lang/ast"
Expand Down Expand Up @@ -136,6 +137,122 @@ func TestInterpolater_pathRoot(t *testing.T) {
})
}

func TestInterpolater_computedSet(t *testing.T) {
lock := new(sync.RWMutex)
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"resource.name": &ResourceState{
Type: "resource",
Primary: &InstanceState{
ID: "qux",
Attributes: map[string]string{
"foo.#": "4",
"foo.298374.bar1": "baz1",
"foo.298374.bar2": "baz12",
"foo.233489.bar1": "baz2",
"foo.233489.bar2": "baz22",
"foo.872348.bar1": "baz3",
"foo.872348.bar2": "baz32",
"foo.348573.bar1": "baz4",
"foo.348573.bar2": "baz42",
},
},
},
},
},
},
}

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

scope := &InterpolationScope{
Path: rootModulePath,
}

testInterpolate(t, i, scope, "resource.name.foo.#", ast.Variable{
Value: "4",
Type: ast.TypeString,
})

expectedValues := []string{"baz1", "baz2", "baz3", "baz4"}
testInterpolate(t, i, scope, "resource.name.foo.*.bar1", ast.Variable{
Value: strings.Join(expectedValues, config.InterpSplitDelim),
Type: ast.TypeString,
})

expectedValues = []string{"baz12", "baz22", "baz32", "baz42"}
testInterpolate(t, i, scope, "resource.name.foo.*.bar2", ast.Variable{
Value: strings.Join(expectedValues, config.InterpSplitDelim),
Type: ast.TypeString,
})

testInterpolate(t, i, scope, "resource.name.foo.0.bar1", ast.Variable{
Value: "baz1",
Type: ast.TypeString,
})

testInterpolate(t, i, scope, "resource.name.foo.4.bar1", ast.Variable{
Value: "",
Type: ast.TypeString,
})
}

func TestInterpolater_computedList(t *testing.T) {
lock := new(sync.RWMutex)
state := &State{
Modules: []*ModuleState{
&ModuleState{
Path: rootModulePath,
Resources: map[string]*ResourceState{
"resource.name": &ResourceState{
Type: "resource",
Primary: &InstanceState{
ID: "qux",
Attributes: map[string]string{
"foo.#": "4",
"foo.298374": "bar1",
"foo.233489": "bar2",
"foo.872348": "bar3",
"foo.348573": "bar4",
},
},
},
},
},
},
}

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

scope := &InterpolationScope{
Path: rootModulePath,
}

testInterpolate(t, i, scope, "resource.name.foo.#", ast.Variable{
Value: "4",
Type: ast.TypeString,
})

expectedValues := []string{"bar1", "bar2", "bar3", "bar4"}
testInterpolate(t, i, scope, "resource.name.foo.*", ast.Variable{
Value: strings.Join(expectedValues, config.InterpSplitDelim),
Type: ast.TypeString,
})
}

func testInterpolate(
t *testing.T, i *Interpolater,
scope *InterpolationScope,
Expand All @@ -155,6 +272,7 @@ func testInterpolate(
expected := map[string]ast.Variable{
"foo": expectedVar,
}

if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
Expand Down
3 changes: 3 additions & 0 deletions terraform/test-fixtures/resource-computed-list/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "resource" "name" {
foo = [ "bar1", "bar2", "bar3", "bar4" ]
}
21 changes: 21 additions & 0 deletions terraform/test-fixtures/resource-computed-set/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource "resource" "name" {
foo {
bar1 = "baz1"
bar2 = "baz12"
}

foo {
bar1 = "baz2"
bar2 = "baz22"
}

foo {
bar1 = "baz3"
bar2 = "baz32"
}

foo {
bar1 = "baz4"
bar2 = "baz42"
}
}