@@ -99,7 +99,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
99
99
var priorIdentityValues map [string ]any
100
100
101
101
if kind .plannable () && kind .resourceIdentity () {
102
- priorIdentityValues = identityValuesFromState (stateJSON , resourceName )
102
+ priorIdentityValues = identityValuesFromStateValues (stateJSON . Values , resourceName )
103
103
if len (priorIdentityValues ) == 0 {
104
104
return fmt .Errorf ("importing resource %s: expected prior state to have resource identity values, got none" , resourceName )
105
105
}
@@ -231,32 +231,15 @@ func testImportBlock(ctx context.Context, t testing.T, workingDir *plugintest.Wo
231
231
}
232
232
233
233
if kind .resourceIdentity () {
234
- if err := verifyIdentityValues (ctx , t , workingDir , providers , resourceName , priorIdentityValues ); err != nil {
235
- return err
234
+ newIdentityValues := identityValuesFromStateValues (plan .PlannedValues , resourceName )
235
+ if ! cmp .Equal (priorIdentityValues , newIdentityValues ) {
236
+ return fmt .Errorf ("importing resource %s: expected identity values %v, got %v" , resourceName , priorIdentityValues , newIdentityValues )
236
237
}
237
238
}
238
239
239
240
return nil
240
241
}
241
242
242
- func verifyIdentityValues (ctx context.Context , t testing.T , workingDir * plugintest.WorkingDir , providers * providerFactories , resourceName string , priorIdentityValues map [string ]any ) error {
243
- err := runProviderCommandApply (ctx , t , workingDir , providers )
244
- if err != nil {
245
- return fmt .Errorf ("applying plan with import config: %s" , err )
246
- }
247
-
248
- newStateJSON , err := runProviderCommandGetStateJSON (ctx , t , workingDir , providers )
249
- if err != nil {
250
- return fmt .Errorf ("getting state after applying plan with import config: %s" , err )
251
- }
252
-
253
- newIdentityValues := identityValuesFromState (newStateJSON , resourceName )
254
- if ! cmp .Equal (priorIdentityValues , newIdentityValues ) {
255
- return fmt .Errorf ("importing resource %s: expected identity values %v, got %v" , resourceName , priorIdentityValues , newIdentityValues )
256
- }
257
- return nil
258
- }
259
-
260
243
func testImportCommand (ctx context.Context , t testing.T , workingDir * plugintest.WorkingDir , providers * providerFactories , resourceName string , importId string , step TestStep , state * terraform.State ) error {
261
244
err := runProviderCommand (ctx , t , workingDir , providers , func () error {
262
245
return workingDir .Import (ctx , resourceName , importId )
@@ -515,18 +498,17 @@ func importStatePreconditions(t testing.T, helper *plugintest.Helper, step TestS
515
498
return nil
516
499
}
517
500
518
- func resourcesFromState (state * tfjson.State ) []* tfjson.StateResource {
519
- stateValues := state .Values
501
+ func resourcesFromState (stateValues * tfjson.StateValues ) []* tfjson.StateResource {
520
502
if stateValues == nil || stateValues .RootModule == nil {
521
503
return []* tfjson.StateResource {}
522
504
}
523
505
524
506
return stateValues .RootModule .Resources
525
507
}
526
508
527
- func identityValuesFromState ( state * tfjson.State , resourceName string ) map [string ]any {
509
+ func identityValuesFromStateValues ( stateValues * tfjson.StateValues , resourceName string ) map [string ]any {
528
510
var resource * tfjson.StateResource
529
- resources := resourcesFromState (state )
511
+ resources := resourcesFromState (stateValues )
530
512
531
513
for _ , r := range resources {
532
514
if r .Address == resourceName {
0 commit comments