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.
This commit is contained in:
James Bardin 2018-07-11 21:13:43 -04:00 committed by Martin Atkins
parent befa81c74f
commit 3112b707be
2 changed files with 14 additions and 1 deletions

View File

@ -219,10 +219,16 @@ func (d *ResourceData) Id() string {
if d.state != nil { if d.state != nil {
result = d.state.ID result = d.state.ID
if result == "" {
result = d.state.Attributes["id"]
}
} }
if d.newState != nil { if d.newState != nil {
result = d.newState.ID result = d.newState.ID
if result == "" {
result = d.newState.Attributes["id"]
}
} }
return result return result
@ -246,6 +252,10 @@ func (d *ResourceData) ConnInfo() map[string]string {
func (d *ResourceData) SetId(v string) { func (d *ResourceData) SetId(v string) {
d.once.Do(d.init) d.once.Do(d.init)
d.newState.ID = v 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. // SetConnInfo sets the connection info for a resource.

View File

@ -3440,7 +3440,10 @@ func TestResourceDataSetId(t *testing.T) {
d.SetId("foo") d.SetId("foo")
actual := d.State() 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) t.Fatalf("bad: %#v", actual)
} }
} }