only force top-level id's back to unknown

Nested structures may have "id" fields, which should be treated
normally.
This commit is contained in:
James Bardin 2019-02-05 16:16:08 -05:00
parent 3b18dd7c01
commit 411df99f33
4 changed files with 83 additions and 2 deletions

View File

@ -33,6 +33,7 @@ func Provider() terraform.ResourceProvider {
"test_resource_list_set": testResourceListSet(), "test_resource_list_set": testResourceListSet(),
"test_resource_map": testResourceMap(), "test_resource_map": testResourceMap(),
"test_resource_computed_set": testResourceComputedSet(), "test_resource_computed_set": testResourceComputedSet(),
"test_resource_nested_id": testResourceNestedId(),
}, },
DataSourcesMap: map[string]*schema.Resource{ DataSourcesMap: map[string]*schema.Resource{
"test_data_source": testDataSource(), "test_data_source": testDataSource(),

View File

@ -0,0 +1,48 @@
package test
import (
"github.com/hashicorp/terraform/helper/schema"
)
func testResourceNestedId() *schema.Resource {
return &schema.Resource{
Create: testResourceNestedIdCreate,
Read: testResourceNestedIdRead,
Update: testResourceNestedIdUpdate,
Delete: testResourceNestedIdDelete,
Schema: map[string]*schema.Schema{
"list_block": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
},
}
}
func testResourceNestedIdCreate(d *schema.ResourceData, meta interface{}) error {
d.SetId("testId")
return nil
}
func testResourceNestedIdRead(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceNestedIdUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}
func testResourceNestedIdDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

View File

@ -0,0 +1,31 @@
package test
import (
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestResourceNestedId_unknownId(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testAccCheckResourceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: strings.TrimSpace(`
resource "test_resource_nested_id" "foo" {
}
resource "test_resource_nested_id" "bar" {
list_block {
id = test_resource_nested_id.foo.id
}
}
`),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("test_resource_nested_id.bar", "list_block.0.id", "testId"),
),
},
},
})
}

View File

@ -665,8 +665,9 @@ func (d *InstanceDiff) applySingleAttrDiff(path []string, attrs map[string]strin
return result, nil return result, nil
} }
// "id" must exist and not be an empty string, or it must be unknown // "id" must exist and not be an empty string, or it must be unknown.
if attr == "id" { // This only applied to top-level "id" fields.
if attr == "id" && len(path) == 1 {
if old == "" { if old == "" {
result[attr] = config.UnknownVariableValue result[attr] = config.UnknownVariableValue
} else { } else {