@@ -17,6 +17,10 @@ import (
17
17
"github.com/hashicorp/terraform/internal/terraform"
18
18
)
19
19
20
+ func ptrOf [T any ](v T ) * T {
21
+ return & v
22
+ }
23
+
20
24
func TestMarshalAttributeValues (t * testing.T ) {
21
25
tests := []struct {
22
26
Attr cty.Value
@@ -105,7 +109,7 @@ func TestMarshalAttributeValues(t *testing.T) {
105
109
}
106
110
107
111
for _ , test := range tests {
108
- got := marshalAttributeValues (test .Attr , test . Schema )
112
+ got := marshalAttributeValues (test .Attr )
109
113
eq := reflect .DeepEqual (got , test .Want )
110
114
if ! eq {
111
115
t .Fatalf ("wrong result:\n Got: %#v\n Want: %#v\n " , got , test .Want )
@@ -185,11 +189,13 @@ func TestMarshalPlannedOutputs(t *testing.T) {
185
189
186
190
func TestMarshalPlanResources (t * testing.T ) {
187
191
tests := map [string ]struct {
188
- Action plans.Action
189
- Before cty.Value
190
- After cty.Value
191
- Want []resource
192
- Err bool
192
+ Action plans.Action
193
+ Before cty.Value
194
+ After cty.Value
195
+ Want []resource
196
+ Err bool
197
+ BeforeIdentity cty.Value
198
+ AfterIdentity cty.Value
193
199
}{
194
200
"create with unknowns" : {
195
201
Action : plans .Create ,
@@ -257,6 +263,37 @@ func TestMarshalPlanResources(t *testing.T) {
257
263
}},
258
264
Err : false ,
259
265
},
266
+ "with identity" : {
267
+ Action : plans .Create ,
268
+ Before : cty .NullVal (cty .EmptyObject ),
269
+ After : cty .ObjectVal (map [string ]cty.Value {
270
+ "woozles" : cty .StringVal ("woo" ),
271
+ "foozles" : cty .NullVal (cty .String ),
272
+ }),
273
+ BeforeIdentity : cty .NullVal (cty .EmptyObject ),
274
+ AfterIdentity : cty .ObjectVal (map [string ]cty.Value {
275
+ "id" : cty .StringVal ("someId" ),
276
+ }),
277
+ Want : []resource {{
278
+ Address : "test_thing.example" ,
279
+ Mode : "managed" ,
280
+ Type : "test_thing" ,
281
+ Name : "example" ,
282
+ Index : addrs .InstanceKey (nil ),
283
+ ProviderName : "registry.terraform.io/hashicorp/test" ,
284
+ SchemaVersion : 1 ,
285
+ AttributeValues : attributeValues {
286
+ "woozles" : json .RawMessage (`"woo"` ),
287
+ "foozles" : json .RawMessage (`null` ),
288
+ },
289
+ SensitiveValues : json .RawMessage ("{}" ),
290
+ IdentitySchemaVersion : ptrOf [uint64 ](2 ),
291
+ IdentityValues : attributeValues {
292
+ "id" : json .RawMessage (`"someId"` ),
293
+ },
294
+ }},
295
+ Err : false ,
296
+ },
260
297
}
261
298
262
299
for name , test := range tests {
@@ -270,6 +307,23 @@ func TestMarshalPlanResources(t *testing.T) {
270
307
if err != nil {
271
308
t .Fatal (err )
272
309
}
310
+
311
+ var beforeIdentity , afterIdentity plans.DynamicValue
312
+ if ! test .BeforeIdentity .IsNull () {
313
+ var err error
314
+ beforeIdentity , err = plans .NewDynamicValue (test .BeforeIdentity , test .BeforeIdentity .Type ())
315
+ if err != nil {
316
+ t .Fatal (err )
317
+ }
318
+ }
319
+ if ! test .AfterIdentity .IsNull () {
320
+ var err error
321
+ afterIdentity , err = plans .NewDynamicValue (test .AfterIdentity , test .AfterIdentity .Type ())
322
+ if err != nil {
323
+ t .Fatal (err )
324
+ }
325
+ }
326
+
273
327
testChange := & plans.ChangesSrc {
274
328
Resources : []* plans.ResourceInstanceChangeSrc {
275
329
{
@@ -283,9 +337,11 @@ func TestMarshalPlanResources(t *testing.T) {
283
337
Module : addrs .RootModule ,
284
338
},
285
339
ChangeSrc : plans.ChangeSrc {
286
- Action : test .Action ,
287
- Before : before ,
288
- After : after ,
340
+ Action : test .Action ,
341
+ Before : before ,
342
+ After : after ,
343
+ BeforeIdentity : beforeIdentity ,
344
+ AfterIdentity : afterIdentity ,
289
345
},
290
346
},
291
347
},
@@ -357,6 +413,13 @@ func testSchemas() *terraform.Schemas {
357
413
"foozles" : {Type : cty .String , Optional : true },
358
414
},
359
415
},
416
+ IdentityVersion : 2 ,
417
+ Identity : & configschema.Object {
418
+ Attributes : map [string ]* configschema.Attribute {
419
+ "id" : {Type : cty .String , Required : true },
420
+ },
421
+ Nesting : configschema .NestingSingle ,
422
+ },
360
423
},
361
424
},
362
425
},
0 commit comments