From 68ba2d6ff0dcda71905ecb4fca57c132c6c67ce7 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 18 Nov 2016 15:47:30 -0500 Subject: [PATCH] ResourceConfig.get should never return (nil, true) Fixes a case where ResourceConfig.get inadvertently returns a nil value. Add an integration test where assigning a map to a list via interpolation would panic. --- builtin/providers/test/resource_test.go | 34 +++++++++++++++++++++++++ terraform/resource.go | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/builtin/providers/test/resource_test.go b/builtin/providers/test/resource_test.go index 198101363..ee46f0b51 100644 --- a/builtin/providers/test/resource_test.go +++ b/builtin/providers/test/resource_test.go @@ -393,6 +393,40 @@ resource "test_resource" "foo" { }) } +// Reproduces plan-time panic when the wrong type is interpolated in a list of +// maps. +// TODO: this should return a type error, rather than silently setting an empty +// list +func TestResource_dataSourceListMapPanic(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource" "foo" { + required = "val" + required_map = {x = "y"} + list_of_map = "${var.maplist}" +} + +variable "maplist" { + type = "list" + + default = [ + {a = "b"} + ] +} + `), + ExpectError: nil, + Check: func(s *terraform.State) error { + return nil + }, + }, + }, + }) +} + func testAccCheckResourceDestroy(s *terraform.State) error { return nil } diff --git a/terraform/resource.go b/terraform/resource.go index 9a8305536..0acf0beb2 100644 --- a/terraform/resource.go +++ b/terraform/resource.go @@ -316,7 +316,8 @@ func (c *ResourceConfig) get( // prefix so were split as path components above. actualKey := strings.Join(parts[i-1:], ".") if prevMap, ok := previous.(map[string]interface{}); ok { - return prevMap[actualKey], true + v, ok := prevMap[actualKey] + return v, ok } return nil, false