Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 052345a

Browse files
committed
core: Fix empty multi-variable type
Previously, interpolation of multi-variables was returning an empty variable if the resource count was 0. The empty variable was defined as TypeString, Value "". This means that empty resource counts fail type checking for interpolation functions which operate on lists. Instead, return an empty list if the count is 0. A context test tests this against further regression. Also add a regression test covering the case of a single count multi-variable. In order to make the context testing framework deal with this change it was necessary to special case empty lists in the test diff function. Fixes hashicorp#7002
1 parent 2fd4a62 commit 052345a

5 files changed

Lines changed: 85 additions & 2 deletions

File tree

terraform/context_apply_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,69 @@ func TestContext2Apply(t *testing.T) {
4848
}
4949
}
5050

51+
func TestContext2Apply_resourceCountOneList(t *testing.T) {
52+
m := testModule(t, "apply-resource-count-one-list")
53+
p := testProvider("null")
54+
p.ApplyFn = testApplyFn
55+
p.DiffFn = testDiffFn
56+
ctx := testContext2(t, &ContextOpts{
57+
Module: m,
58+
Providers: map[string]ResourceProviderFactory{
59+
"null": testProviderFuncFixed(p),
60+
},
61+
})
62+
63+
if _, err := ctx.Plan(); err != nil {
64+
t.Fatalf("err: %s", err)
65+
}
66+
67+
state, err := ctx.Apply()
68+
if err != nil {
69+
t.Fatalf("err: %s", err)
70+
}
71+
72+
actual := strings.TrimSpace(state.String())
73+
expected := strings.TrimSpace(`null_resource.foo:
74+
ID = foo
75+
76+
Outputs:
77+
78+
test = [foo]`)
79+
if actual != expected {
80+
t.Fatalf("expected: \n%s\n\ngot: \n%s\n", expected, actual)
81+
}
82+
}
83+
func TestContext2Apply_resourceCountZeroList(t *testing.T) {
84+
m := testModule(t, "apply-resource-count-zero-list")
85+
p := testProvider("null")
86+
p.ApplyFn = testApplyFn
87+
p.DiffFn = testDiffFn
88+
ctx := testContext2(t, &ContextOpts{
89+
Module: m,
90+
Providers: map[string]ResourceProviderFactory{
91+
"null": testProviderFuncFixed(p),
92+
},
93+
})
94+
95+
if _, err := ctx.Plan(); err != nil {
96+
t.Fatalf("err: %s", err)
97+
}
98+
99+
state, err := ctx.Apply()
100+
if err != nil {
101+
t.Fatalf("err: %s", err)
102+
}
103+
104+
actual := strings.TrimSpace(state.String())
105+
expected := strings.TrimSpace(`<no state>
106+
Outputs:
107+
108+
test = []`)
109+
if actual != expected {
110+
t.Fatalf("expected: \n%s\n\ngot: \n%s\n", expected, actual)
111+
}
112+
}
113+
51114
func TestContext2Apply_mapVarBetweenModules(t *testing.T) {
52115
m := testModule(t, "apply-map-var-through-module")
53116
p := testProvider("null")

terraform/context_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package terraform
22

33
import (
44
"fmt"
5+
"reflect"
56
"strings"
67
"testing"
78
"time"
@@ -166,7 +167,12 @@ func testDiffFn(
166167

167168
attrDiff := &ResourceAttrDiff{
168169
Old: "",
169-
New: v.(string),
170+
}
171+
172+
if reflect.DeepEqual(v, []interface{}{}) {
173+
attrDiff.New = ""
174+
} else {
175+
attrDiff.New = v.(string)
170176
}
171177

172178
if k == "require_new" {

terraform/interpolate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func (i *Interpolater) computeResourceMultiVariable(
487487

488488
// If we have no module in the state yet or count, return empty
489489
if module == nil || len(module.Resources) == 0 || count == 0 {
490-
return &ast.Variable{Type: ast.TypeString, Value: ""}, nil
490+
return &ast.Variable{Type: ast.TypeList, Value: []ast.Variable{}}, nil
491491
}
492492

493493
var values []string
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "null_resource" "foo" {
2+
count = 1
3+
}
4+
5+
output "test" {
6+
value = "${sort(null_resource.foo.*.id)}"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "null_resource" "foo" {
2+
count = 0
3+
}
4+
5+
output "test" {
6+
value = "${sort(null_resource.foo.*.id)}"
7+
}

0 commit comments

Comments
 (0)