@@ -99,7 +99,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
9999 var priorIdentityValues map [string ]any
100100
101101 if kind .plannable () && kind .resourceIdentity () {
102- priorIdentityValues = identityValuesFromState (stateJSON , resourceName )
102+ priorIdentityValues = identityValuesFromStateValues (stateJSON . Values , resourceName )
103103 if len (priorIdentityValues ) == 0 {
104104 return fmt .Errorf ("importing resource %s: expected prior state to have resource identity values, got none" , resourceName )
105105 }
@@ -231,32 +231,15 @@ func testImportBlock(ctx context.Context, t testing.T, workingDir *plugintest.Wo
231231 }
232232
233233 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 )
236237 }
237238 }
238239
239240 return nil
240241}
241242
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-
260243func testImportCommand (ctx context.Context , t testing.T , workingDir * plugintest.WorkingDir , providers * providerFactories , resourceName string , importId string , step TestStep , state * terraform.State ) error {
261244 err := runProviderCommand (ctx , t , workingDir , providers , func () error {
262245 return workingDir .Import (ctx , resourceName , importId )
@@ -515,18 +498,17 @@ func importStatePreconditions(t testing.T, helper *plugintest.Helper, step TestS
515498 return nil
516499}
517500
518- func resourcesFromState (state * tfjson.State ) []* tfjson.StateResource {
519- stateValues := state .Values
501+ func resourcesFromState (stateValues * tfjson.StateValues ) []* tfjson.StateResource {
520502 if stateValues == nil || stateValues .RootModule == nil {
521503 return []* tfjson.StateResource {}
522504 }
523505
524506 return stateValues .RootModule .Resources
525507}
526508
527- func identityValuesFromState ( state * tfjson.State , resourceName string ) map [string ]any {
509+ func identityValuesFromStateValues ( stateValues * tfjson.StateValues , resourceName string ) map [string ]any {
528510 var resource * tfjson.StateResource
529- resources := resourcesFromState (state )
511+ resources := resourcesFromState (stateValues )
530512
531513 for _ , r := range resources {
532514 if r .Address == resourceName {
0 commit comments