helper/schema: properly put "id" into attributes
This commit is contained in:
parent
021a23fe99
commit
5fc41cc272
|
@ -81,17 +81,7 @@ func (r *Resource) Apply(
|
|||
err = r.Update(data, meta)
|
||||
}
|
||||
|
||||
// Always set the ID attribute if it is set. We also always collapse
|
||||
// the state since even partial states need to be returned.
|
||||
state := data.State()
|
||||
if state.ID != "" {
|
||||
if state.Attributes == nil {
|
||||
state.Attributes = make(map[string]string)
|
||||
}
|
||||
state.Attributes["id"] = state.ID
|
||||
}
|
||||
|
||||
return state, err
|
||||
return data.State(), err
|
||||
}
|
||||
|
||||
// Diff returns a diff of this resource and is API compatible with the
|
||||
|
|
|
@ -129,6 +129,10 @@ func (d *ResourceData) State() *terraform.ResourceState {
|
|||
result.Attributes = d.stateObject("", d.schema)
|
||||
result.Dependencies = d.Dependencies()
|
||||
|
||||
if v := d.Id(); v != "" {
|
||||
result.Attributes["id"] = d.Id()
|
||||
}
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
|
|
|
@ -1309,6 +1309,43 @@ func TestResourceDataState(t *testing.T) {
|
|||
Attributes: map[string]string{},
|
||||
},
|
||||
},
|
||||
|
||||
// Basic state with other keys
|
||||
{
|
||||
Schema: map[string]*Schema{
|
||||
"availability_zone": &Schema{
|
||||
Type: TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
},
|
||||
|
||||
State: &terraform.ResourceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"id": "bar",
|
||||
},
|
||||
},
|
||||
|
||||
Diff: &terraform.ResourceDiff{
|
||||
Attributes: map[string]*terraform.ResourceAttrDiff{
|
||||
"availability_zone": &terraform.ResourceAttrDiff{
|
||||
Old: "",
|
||||
New: "foo",
|
||||
RequiresNew: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Result: &terraform.ResourceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"id": "bar",
|
||||
"availability_zone": "foo",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range cases {
|
||||
|
|
|
@ -129,6 +129,7 @@ func TestResourceApply_destroyPartial(t *testing.T) {
|
|||
expected := &terraform.ResourceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"id": "bar",
|
||||
"foo": "42",
|
||||
},
|
||||
}
|
||||
|
@ -291,6 +292,7 @@ func TestResourceRefresh(t *testing.T) {
|
|||
expected := &terraform.ResourceState{
|
||||
ID: "bar",
|
||||
Attributes: map[string]string{
|
||||
"id": "bar",
|
||||
"foo": "13",
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue