Merge pull request #19971 from hashicorp/jbardin/resource-data-id

make sure id really gets set in SetId
This commit is contained in:
James Bardin 2019-01-10 20:53:14 -05:00 committed by GitHub
commit 056892b410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -255,7 +255,15 @@ func (d *ResourceData) SetId(v string) {
// once we transition away from the legacy state types, "id" will no longer
// be a special field, and will become a normal attribute.
// set the attribute normally
d.setWriter.unsafeWriteField("id", v)
// Make sure the newState is also set, otherwise the old value
// may get precedence.
if d.newState.Attributes == nil {
d.newState.Attributes = map[string]string{}
}
d.newState.Attributes["id"] = v
}
// SetConnInfo sets the connection info for a resource.

View File

@ -3499,14 +3499,27 @@ func TestResourceDataSetMeta_Timeouts(t *testing.T) {
}
func TestResourceDataSetId(t *testing.T) {
d := &ResourceData{}
d := &ResourceData{
state: &terraform.InstanceState{
ID: "test",
Attributes: map[string]string{
"id": "test",
},
},
}
d.SetId("foo")
actual := d.State()
// 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" {
if actual.ID != "foo" || actual.Attributes["id"] != "foo" {
t.Fatalf("bad: %#v", actual)
}
d.SetId("")
actual = d.State()
if actual != nil {
t.Fatalf("bad: %#v", actual)
}
}