Skip to content

Commit bd29ab0

Browse files
committed
Extract requireNoopResourceAction
1 parent 024ac49 commit bd29ab0

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

helper/resource/testing_new_import_state.go

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -188,29 +188,8 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
188188
if plan.ResourceChanges != nil {
189189
logging.HelperResourceDebug(ctx, fmt.Sprintf("ImportBlockWithId: %d resource changes", len(plan.ResourceChanges)))
190190

191-
for _, rc := range plan.ResourceChanges {
192-
if rc.Address != resourceName {
193-
// we're only interested in the changes for the resource being imported
194-
continue
195-
}
196-
if rc.Change != nil && rc.Change.Actions != nil {
197-
// should this be length checked and used as a condition, if it's a no-op then there shouldn't be any other changes here
198-
for _, action := range rc.Change.Actions {
199-
if action != "no-op" {
200-
var stdout string
201-
err = runProviderCommand(ctx, t, func() error {
202-
var err error
203-
stdout, err = importWd.SavedPlanRawStdout(ctx)
204-
return err
205-
}, importWd, providers)
206-
if err != nil {
207-
return fmt.Errorf("retrieving formatted plan output: %w", err)
208-
}
209-
210-
return fmt.Errorf("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", rc.Address, action, stdout)
211-
}
212-
}
213-
}
191+
if err := requireNoopResourceAction(ctx, t, plan, resourceName, importWd, providers); err != nil {
192+
return err
214193
}
215194
}
216195

@@ -389,6 +368,46 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
389368
return nil
390369
}
391370

371+
func requireNoopResourceAction(ctx context.Context, t testing.T, plan *tfjson.Plan, resourceName string, importWd *plugintest.WorkingDir, providers *providerFactories) error {
372+
t.Helper()
373+
374+
rc := findResourceChangeInPlan(t, plan, resourceName)
375+
if rc == nil || rc.Change == nil || rc.Change.Actions == nil {
376+
// does this matter?
377+
return nil
378+
}
379+
380+
// should this be length checked and used as a condition, if it's a no-op then there shouldn't be any other changes here
381+
for _, action := range rc.Change.Actions {
382+
if action != "no-op" {
383+
var stdout string
384+
err := runProviderCommand(ctx, t, func() error {
385+
var err error
386+
stdout, err = importWd.SavedPlanRawStdout(ctx)
387+
return err
388+
}, importWd, providers)
389+
if err != nil {
390+
return fmt.Errorf("retrieving formatted plan output: %w", err)
391+
}
392+
393+
return fmt.Errorf("importing resource %s: expected a no-op resource action, got %q action with plan \nstdout:\n\n%s", rc.Address, action, stdout)
394+
}
395+
}
396+
397+
return nil
398+
}
399+
400+
func findResourceChangeInPlan(t testing.T, plan *tfjson.Plan, resourceName string) *tfjson.ResourceChange {
401+
t.Helper()
402+
403+
for _, rc := range plan.ResourceChanges {
404+
if rc.Address == resourceName {
405+
return rc
406+
}
407+
}
408+
return nil
409+
}
410+
392411
func appendImportBlock(config string, resourceName string, importID string) string {
393412
return config + fmt.Sprintf(``+"\n"+
394413
`import {`+"\n"+

0 commit comments

Comments
 (0)