diff --git a/helper/schema/resource_data.go b/helper/schema/resource_data.go index 6cc01ee0b..92d0a73e8 100644 --- a/helper/schema/resource_data.go +++ b/helper/schema/resource_data.go @@ -219,10 +219,16 @@ func (d *ResourceData) Id() string { if d.state != nil { result = d.state.ID + if result == "" { + result = d.state.Attributes["id"] + } } if d.newState != nil { result = d.newState.ID + if result == "" { + result = d.newState.Attributes["id"] + } } return result @@ -246,6 +252,10 @@ func (d *ResourceData) ConnInfo() map[string]string { func (d *ResourceData) SetId(v string) { d.once.Do(d.init) d.newState.ID = v + + // once we transition away from the legacy state types, "id" will no longer + // be a special field, and will become a normal attribute. + d.setWriter.unsafeWriteField("id", v) } // SetConnInfo sets the connection info for a resource. diff --git a/helper/schema/resource_data_test.go b/helper/schema/resource_data_test.go index b6a09b2b3..6ef19017d 100644 --- a/helper/schema/resource_data_test.go +++ b/helper/schema/resource_data_test.go @@ -3440,7 +3440,10 @@ func TestResourceDataSetId(t *testing.T) { d.SetId("foo") actual := d.State() - if actual.ID != "foo" { + + // SetId should set both the ID field as well as the attribute, to aid in + // transitioning to the new type system. + if actual.ID != "foo" && actual.Attributes["id"] != "foo" { t.Fatalf("bad: %#v", actual) } }