@@ -20,7 +20,7 @@ import (
2020 "github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
2121)
2222
23- func testStepNewImportState (ctx context.Context , t testing.T , helper * plugintest.Helper , wd * plugintest.WorkingDir , step TestStep , cfg teststep.Config , providers * providerFactories , stepIndex int ) error {
23+ func testStepNewImportState (ctx context.Context , t testing.T , helper * plugintest.Helper , wd * plugintest.WorkingDir , step TestStep , cfg teststep.Config , cfgHcl string , providers * providerFactories , stepIndex int ) error {
2424 t .Helper ()
2525
2626 configRequest := teststep.PrepareConfigurationRequest {
@@ -31,9 +31,7 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
3131 StepNumber : stepIndex + 1 ,
3232 TestName : t .Name (),
3333 },
34- }.Exec ()
35-
36- testStepConfig := teststep .Configuration (configRequest )
34+ }
3735
3836 if step .ResourceName == "" {
3937 t .Fatal ("ResourceName is required for an import state test" )
@@ -93,13 +91,24 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
9391
9492 logging .HelperResourceTrace (ctx , fmt .Sprintf ("Using import identifier: %s" , importId ))
9593
96- // Create working directory for import tests
97- if testStepConfig == nil {
98- logging .HelperResourceTrace (ctx , "Using prior TestStep Config for import" )
99-
100- testStepConfig = cfg
94+ var testStepConfig teststep.Config
95+ if step .ImportStateKind == ImportBlockWithResourceIdentity {
96+ cfgHcl += `
97+ import {
98+ to = examplecloud_bucket.storage
99+ id = "test-bucket" // to be replaced with identity
100+ }`
101+ configRequest .Raw = cfgHcl
102+ testStepConfig = teststep .Configuration (configRequest .Exec ())
103+ } else {
104+ // Create working directory for import tests
101105 if testStepConfig == nil {
102- t .Fatal ("Cannot import state with no specified config" )
106+ logging .HelperResourceTrace (ctx , "Using prior TestStep Config for import" )
107+
108+ testStepConfig = cfg
109+ if testStepConfig == nil {
110+ t .Fatal ("Cannot import state with no specified config" )
111+ }
103112 }
104113 }
105114
@@ -140,7 +149,26 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
140149 case ImportBlockWithId :
141150 t .Fatalf ("not yet implemented" )
142151 case ImportBlockWithResourceIdentity :
143- t .Fatalf ("not yet implemented" )
152+ if ! step .ImportStatePersist {
153+ err = runProviderCommand (ctx , t , func () error {
154+ return importWd .Init (ctx )
155+ }, importWd , providers )
156+ if err != nil {
157+ t .Fatalf ("Error running init: %s" , err )
158+ }
159+ }
160+ err = runProviderCommand (ctx , t , func () error {
161+ return importWd .CreatePlan (ctx )
162+ }, importWd , providers )
163+ if err != nil {
164+ t .Fatalf ("Error running plan: %s" , err )
165+ }
166+ err = runProviderCommand (ctx , t , func () error {
167+ return importWd .Apply (ctx )
168+ }, importWd , providers )
169+ if err != nil {
170+ t .Fatalf ("Error running apply: %s" , err )
171+ }
144172 default :
145173 t .Fatalf (`\o/` )
146174 }
0 commit comments