diff --git a/builtin/providers/test/resource_state_func.go b/builtin/providers/test/resource_state_func.go index 46c4b9b20..609e5ea53 100644 --- a/builtin/providers/test/resource_state_func.go +++ b/builtin/providers/test/resource_state_func.go @@ -6,6 +6,7 @@ import ( "fmt" "math/rand" + "github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/schema" ) @@ -35,6 +36,26 @@ func testResourceStateFunc() *schema.Resource { Type: schema.TypeString, Optional: true, }, + + // set block with computed elements + "set_block": { + Type: schema.TypeSet, + Optional: true, + Set: setBlockHash, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "required": { + Type: schema.TypeString, + Required: true, + }, + "optional": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + }, + }, + }, }, } } @@ -44,6 +65,13 @@ func stateFuncHash(v interface{}) string { return hex.EncodeToString(hash[:]) } +func setBlockHash(v interface{}) int { + m := v.(map[string]interface{}) + required, _ := m["required"].(string) + optional, _ := m["optional"].(string) + return hashcode.String(fmt.Sprintf("%s|%s", required, optional)) +} + func testResourceStateFuncCreate(d *schema.ResourceData, meta interface{}) error { d.SetId(fmt.Sprintf("%x", rand.Int63())) @@ -57,6 +85,22 @@ func testResourceStateFuncCreate(d *schema.ResourceData, meta interface{}) error } } + // Check that we can lookup set elements by our computed hash. + // This is not advised, but we can use this to make sure the final diff was + // prepared with the correct values. + setBlock, ok := d.GetOk("set_block") + if ok { + set := setBlock.(*schema.Set) + for _, obj := range set.List() { + idx := setBlockHash(obj) + requiredAddr := fmt.Sprintf("%s.%d.%s", "set_block", idx, "required") + _, ok := d.GetOkExists(requiredAddr) + if !ok { + return fmt.Errorf("failed to get attr %q from %#v", fmt.Sprintf(requiredAddr), d.State().Attributes) + } + } + } + return testResourceStateFuncRead(d, meta) } diff --git a/builtin/providers/test/resource_state_func_test.go b/builtin/providers/test/resource_state_func_test.go index afdbd924e..cf5726eea 100644 --- a/builtin/providers/test/resource_state_func_test.go +++ b/builtin/providers/test/resource_state_func_test.go @@ -58,3 +58,28 @@ resource "test_resource_state_func" "foo" { }, }) } + +func TestResourceStateFunc_getOkSetElem(t *testing.T) { + resource.UnitTest(t, resource.TestCase{ + Providers: testAccProviders, + CheckDestroy: testAccCheckResourceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: strings.TrimSpace(` +resource "test_resource_state_func" "foo" { +} + +resource "test_resource_state_func" "bar" { + set_block { + required = "foo" + optional = test_resource_state_func.foo.id + } + set_block { + required = test_resource_state_func.foo.id + } +} + `), + }, + }, + }) +}