Skip to content

Commit a04ff07

Browse files
committed
config: Resource.Count should type check
Fixes hashicorp#11800 Type check the value of count so we don't panic on the conversion. I wondered "why didn't we do this before?" There is no excuse for NOT doing it at all but the reasoning was beacuse prior to the list/map work in 0.7, the value couldn't be anything other than a string since any primitive can turn into a string. Regardless, we should've always done this.
1 parent 8af52a0 commit a04ff07

3 files changed

Lines changed: 30 additions & 1 deletion

File tree

config/config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,14 @@ func (r *Module) Id() string {
213213

214214
// Count returns the count of this resource.
215215
func (r *Resource) Count() (int, error) {
216-
v, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0)
216+
raw := r.RawCount.Value()
217+
count, ok := r.RawCount.Value().(string)
218+
if !ok {
219+
return 0, fmt.Errorf(
220+
"expected count to be a string or int, got %T", raw)
221+
}
222+
223+
v, err := strconv.ParseInt(count, 0, 0)
217224
if err != nil {
218225
return 0, err
219226
}

config/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"github.com/hashicorp/hil/ast"
1415
"github.com/hashicorp/terraform/helper/logging"
1516
)
1617

@@ -98,6 +99,24 @@ func TestConfigCount_string(t *testing.T) {
9899
}
99100
}
100101

102+
// Terraform GH-11800
103+
func TestConfigCount_list(t *testing.T) {
104+
c := testConfig(t, "count-list")
105+
106+
// The key is to interpolate so it doesn't fail parsing
107+
c.Resources[0].RawCount.Interpolate(map[string]ast.Variable{
108+
"var.list": ast.Variable{
109+
Value: []ast.Variable{},
110+
Type: ast.TypeList,
111+
},
112+
})
113+
114+
_, err := c.Resources[0].Count()
115+
if err == nil {
116+
t.Fatal("should error")
117+
}
118+
}
119+
101120
func TestConfigCount_var(t *testing.T) {
102121
c := testConfig(t, "count-var")
103122
_, err := c.Resources[0].Count()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
resource "foo" "bar" {
2+
count = "${var.list}"
3+
}

0 commit comments

Comments
 (0)