mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #19971 from hashicorp/jbardin/resource-data-id
make sure id really gets set in SetId
This commit is contained in:
commit
056892b410
@ -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.
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user