@@ -6,6 +6,7 @@ package resource
66import (
77 "context"
88 "fmt"
9+ "github.com/hashicorp/terraform-exec/tfexec"
910 "reflect"
1011 "strings"
1112
@@ -20,7 +21,7 @@ import (
2021 "github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
2122)
2223
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 {
24+ func testStepNewImportState (ctx context.Context , t testing.T , helper * plugintest.Helper , wd * plugintest.WorkingDir , step TestStep , cfgRaw string , providers * providerFactories , stepIndex int ) error {
2425 t .Helper ()
2526
2627 configRequest := teststep.PrepareConfigurationRequest {
@@ -93,11 +94,40 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
9394
9495 logging .HelperResourceTrace (ctx , fmt .Sprintf ("Using import identifier: %s" , importId ))
9596
97+ if testStepConfig == nil {
98+ logging .HelperResourceTrace (ctx , "Using prior TestStep Config for import" )
99+ }
100+
96101 // Create working directory for import tests
97102 if testStepConfig == nil {
98103 logging .HelperResourceTrace (ctx , "Using prior TestStep Config for import" )
99104
100- testStepConfig = cfg
105+ switch step .ImportStateKind {
106+ case ImportBlockWithResourceIdentity :
107+ t .Fatalf ("TODO implement me" )
108+ case ImportBlockWithId :
109+ cfgRaw += fmt .Sprintf (`
110+ import {
111+ to = %s
112+ id = %q
113+ }
114+ ` , step .ResourceName , importId )
115+ default :
116+ // Not an import block test so nothing to do here
117+ }
118+
119+ confRequest := teststep.PrepareConfigurationRequest {
120+ Directory : step .ConfigDirectory ,
121+ File : step .ConfigFile ,
122+ Raw : cfgRaw ,
123+ TestStepConfigRequest : config.TestStepConfigRequest {
124+ StepNumber : stepIndex + 1 ,
125+ TestName : t .Name (),
126+ },
127+ }.Exec ()
128+
129+ testStepConfig = teststep .Configuration (confRequest )
130+ //testStepConfig = cfg
101131 if testStepConfig == nil {
102132 t .Fatal ("Cannot import state with no specified config" )
103133 }
@@ -129,11 +159,22 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
129159 }
130160 }
131161
132- err = runProviderCommand (ctx , t , func () error {
133- return importWd .Import (ctx , step .ResourceName , importId )
134- }, importWd , providers )
135- if err != nil {
136- return err
162+ if step .ImportStateKind == ImportBlockWithResourceIdentity || step .ImportStateKind == ImportBlockWithId {
163+ var opts []tfexec.ApplyOption
164+
165+ err = runProviderCommand (ctx , t , func () error {
166+ return importWd .Apply (ctx , opts ... )
167+ }, importWd , providers )
168+ if err != nil {
169+ return err
170+ }
171+ } else {
172+ err = runProviderCommand (ctx , t , func () error {
173+ return importWd .Import (ctx , step .ResourceName , importId )
174+ }, importWd , providers )
175+ if err != nil {
176+ return err
177+ }
137178 }
138179
139180 var importState * terraform.State
0 commit comments