diff --git a/config/config.go b/config/config.go index 14e35d6fac..5210e49376 100644 --- a/config/config.go +++ b/config/config.go @@ -212,7 +212,14 @@ func (r *Module) Id() string { // Count returns the count of this resource. func (r *Resource) Count() (int, error) { - v, err := strconv.ParseInt(r.RawCount.Value().(string), 0, 0) + raw := r.RawCount.Value() + count, ok := r.RawCount.Value().(string) + if !ok { + return 0, fmt.Errorf( + "expected count to be a string or int, got %T", raw) + } + + v, err := strconv.ParseInt(count, 0, 0) if err != nil { return 0, err } diff --git a/config/config_test.go b/config/config_test.go index f554061ea0..1c7b36e04e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -11,6 +11,7 @@ import ( "strings" "testing" + "github.com/hashicorp/hil/ast" "github.com/hashicorp/terraform/helper/logging" ) @@ -98,6 +99,24 @@ func TestConfigCount_string(t *testing.T) { } } +// Terraform GH-11800 +func TestConfigCount_list(t *testing.T) { + c := testConfig(t, "count-list") + + // The key is to interpolate so it doesn't fail parsing + c.Resources[0].RawCount.Interpolate(map[string]ast.Variable{ + "var.list": ast.Variable{ + Value: []ast.Variable{}, + Type: ast.TypeList, + }, + }) + + _, err := c.Resources[0].Count() + if err == nil { + t.Fatal("should error") + } +} + func TestConfigCount_var(t *testing.T) { c := testConfig(t, "count-var") _, err := c.Resources[0].Count() diff --git a/config/test-fixtures/count-list/main.tf b/config/test-fixtures/count-list/main.tf new file mode 100644 index 0000000000..3c6f1ab8f7 --- /dev/null +++ b/config/test-fixtures/count-list/main.tf @@ -0,0 +1,3 @@ +resource "foo" "bar" { + count = "${var.list}" +}