Skip to content

Commit 5fc41cc

Browse files
committed
helper/schema: properly put "id" into attributes
1 parent 021a23f commit 5fc41cc

4 files changed

Lines changed: 44 additions & 11 deletions

File tree

helper/schema/resource.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,7 @@ func (r *Resource) Apply(
8181
err = r.Update(data, meta)
8282
}
8383

84-
// Always set the ID attribute if it is set. We also always collapse
85-
// the state since even partial states need to be returned.
86-
state := data.State()
87-
if state.ID != "" {
88-
if state.Attributes == nil {
89-
state.Attributes = make(map[string]string)
90-
}
91-
state.Attributes["id"] = state.ID
92-
}
93-
94-
return state, err
84+
return data.State(), err
9585
}
9686

9787
// Diff returns a diff of this resource and is API compatible with the

helper/schema/resource_data.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ func (d *ResourceData) State() *terraform.ResourceState {
129129
result.Attributes = d.stateObject("", d.schema)
130130
result.Dependencies = d.Dependencies()
131131

132+
if v := d.Id(); v != "" {
133+
result.Attributes["id"] = d.Id()
134+
}
135+
132136
return &result
133137
}
134138

helper/schema/resource_data_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,43 @@ func TestResourceDataState(t *testing.T) {
13091309
Attributes: map[string]string{},
13101310
},
13111311
},
1312+
1313+
// Basic state with other keys
1314+
{
1315+
Schema: map[string]*Schema{
1316+
"availability_zone": &Schema{
1317+
Type: TypeString,
1318+
Optional: true,
1319+
Computed: true,
1320+
ForceNew: true,
1321+
},
1322+
},
1323+
1324+
State: &terraform.ResourceState{
1325+
ID: "bar",
1326+
Attributes: map[string]string{
1327+
"id": "bar",
1328+
},
1329+
},
1330+
1331+
Diff: &terraform.ResourceDiff{
1332+
Attributes: map[string]*terraform.ResourceAttrDiff{
1333+
"availability_zone": &terraform.ResourceAttrDiff{
1334+
Old: "",
1335+
New: "foo",
1336+
RequiresNew: true,
1337+
},
1338+
},
1339+
},
1340+
1341+
Result: &terraform.ResourceState{
1342+
ID: "bar",
1343+
Attributes: map[string]string{
1344+
"id": "bar",
1345+
"availability_zone": "foo",
1346+
},
1347+
},
1348+
},
13121349
}
13131350

13141351
for i, tc := range cases {

helper/schema/resource_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func TestResourceApply_destroyPartial(t *testing.T) {
129129
expected := &terraform.ResourceState{
130130
ID: "bar",
131131
Attributes: map[string]string{
132+
"id": "bar",
132133
"foo": "42",
133134
},
134135
}
@@ -291,6 +292,7 @@ func TestResourceRefresh(t *testing.T) {
291292
expected := &terraform.ResourceState{
292293
ID: "bar",
293294
Attributes: map[string]string{
295+
"id": "bar",
294296
"foo": "13",
295297
},
296298
}

0 commit comments

Comments
 (0)