File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments