Merge pull request #20235 from hashicorp/jbardin/id

only force top-level id's back to unknown
This commit is contained in:
James Bardin 2019-02-05 16:42:01 -05:00 committed by GitHub
commit 1f4d2f4c50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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_map": testResourceMap(),
"test_resource_computed_set": testResourceComputedSet(),
"test_resource_nested_id": testResourceNestedId(),
},
DataSourcesMap: map[string]*schema.Resource{
"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
}
// "id" must exist and not be an empty string, or it must be unknown
if attr == "id" {
// "id" must exist and not be an empty string, or it must be unknown.
// This only applied to top-level "id" fields.
if attr == "id" && len(path) == 1 {
if old == "" {
result[attr] = config.UnknownVariableValue
} else {