From 3112b707be92308b9b4cf75060bd2843ec226fb0 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 11 Jul 2018 21:13:43 -0400 Subject: [PATCH] SetId should set the attribute as well The update protocol shims will also check for this this, but eventually "id" will only be a normal attribute, and we shouldn't have to special case this. --- helper/schema/resource_data.go | 10 ++++++++++ helper/schema/resource_data_test.go | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) 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) } }