stacks: return diags on deferred cli import

TF-13961
Daniel Schmidt 2 years ago
parent ce721b8e9c
commit efdcc7a711
No known key found for this signature in database
GPG Key ID: 377C3A4D62FBBBE2

@ -1031,6 +1031,61 @@ func TestContextImport_33572(t *testing.T) {
}
}
func TestContextImport_deferred(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "import-provider")
ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
},
})
p.ImportResourceStateResponse = &providers.ImportResourceStateResponse{
Deferred: &providers.Deferred{
Reason: providers.DeferredReasonAbsentPrereq,
},
ImportedResources: []providers.ImportedResource{
{
TypeName: "aws_instance",
State: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("foo"),
}),
},
},
}
state, diags := ctx.Import(m, states.NewState(), &ImportOpts{
Targets: []*ImportTarget{
{
LegacyAddr: addrs.RootModuleInstance.ResourceInstance(
addrs.ManagedResourceMode, "aws_instance", "foo", addrs.NoKey,
),
IDString: "bar",
},
},
})
if !diags.HasErrors() {
t.Fatalf("expected errors, got none.")
}
if len(diags) != 1 {
t.Fatalf("expected 1 error, got %d", len(diags))
}
expectedDiagSummary := "Cannot import deferred remote object"
if !strings.Contains(diags[0].Description().Summary, expectedDiagSummary) {
t.Fatalf("expected error to contain %q, got %q", expectedDiagSummary, diags[0].Description().Summary)
}
expectedDiagDetail := `While attempting to import an existing object to "aws_instance.foo", the provider deferred importing the resource`
if !strings.Contains(diags[0].Description().Detail, expectedDiagDetail) {
t.Fatalf("expected error to contain %q, got %q", expectedDiagDetail, diags[0].Description().Detail)
}
if !state.Empty() {
t.Fatalf("expected empty state, got %s", state)
}
}
const testImportStr = `
aws_instance.foo:
ID = foo

@ -95,10 +95,26 @@ func (n *graphNodeImportState) Execute(ctx EvalContext, op walkOperation) (diags
}
resp := provider.ImportResourceState(providers.ImportResourceStateRequest{
TypeName: n.Addr.Resource.Resource.Type,
ID: n.ID,
TypeName: n.Addr.Resource.Resource.Type,
ID: n.ID,
DeferralAllowed: ctx.Deferrals().DeferralAllowed(),
})
diags = diags.Append(resp.Diagnostics)
if resp.Deferred != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Cannot import deferred remote object",
fmt.Sprintf(
"While attempting to import an existing object to %q, "+
"the provider deferred importing the resource. "+
"Please either use an import block for importing this resource "+
"or remove the to be imported resource from your configuration, "+
"apply the configuration using \"terraform apply\", "+
"add the to be imported resource again, and retry the import operation.",
n.Addr,
),
))
}
if diags.HasErrors() {
return diags
}

Loading…
Cancel
Save