From fd81a7111a80f8729b90b05e7fd301bcd26fa6c6 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Wed, 17 Apr 2024 09:16:22 +0200 Subject: [PATCH] stacks: continue read and update if import is deferred --- .../terraform/node_resource_plan_instance.go | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/internal/terraform/node_resource_plan_instance.go b/internal/terraform/node_resource_plan_instance.go index 1cfa74f36d..c4379f2c98 100644 --- a/internal/terraform/node_resource_plan_instance.go +++ b/internal/terraform/node_resource_plan_instance.go @@ -546,7 +546,7 @@ func (n *NodePlannableResourceInstance) importState(ctx EvalContext, addr addrs. imported := resp.ImportedResources - if len(imported) == 0 && resp.Deferred == nil { + if len(imported) == 0 { diags = diags.Append(tfdiags.Sourceless( tfdiags.Error, "Import returned no resources", @@ -589,27 +589,33 @@ func (n *NodePlannableResourceInstance) importState(ctx EvalContext, addr addrs. return nil, diags } - // call post-import hook - diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) { - return h.PostPlanImport(hookResourceID, imported) - })) + // We expect the import to return a single instance object, + // even when deferring the import. + importedState := imported[0].AsInstanceObject() - if imported[0].TypeName == "" { - diags = diags.Append(fmt.Errorf("import of %s didn't set type", n.Addr.String())) - return nil, diags - } + // We can only call the hooks and validate the imported state if we have + // actually done the import. + if resp.Deferred == nil { + // call post-import hook + diags = diags.Append(ctx.Hook(func(h Hook) (HookAction, error) { + return h.PostPlanImport(hookResourceID, imported) + })) - importedState := imported[0].AsInstanceObject() + if imported[0].TypeName == "" { + diags = diags.Append(fmt.Errorf("import of %s didn't set type", n.Addr.String())) + return nil, diags + } - if importedState.Value.IsNull() { - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - "Import returned null resource", - fmt.Sprintf("While attempting to import with ID %s, the provider"+ - "returned an instance with no state.", - n.importTarget.IDString, - ), - )) + if importedState.Value.IsNull() { + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Import returned null resource", + fmt.Sprintf("While attempting to import with ID %s, the provider"+ + "returned an instance with no state.", + n.importTarget.IDString, + ), + )) + } } // refresh