From 7f3d7313d8fb977a7965ea9a130808d3618e00bb Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Tue, 29 May 2018 12:50:31 -0700 Subject: [PATCH] core: Update error message for non-existing "terraform import" The previous wording of this message was a little awkward, and a little confusing due to the mention of it being a non-existing "resource", when elsewhere in our output we use that noun to refer to the configuration construct rather than the remote object. Here we rework it as a diagnostic message, and while here also include an extra note about a common problem of using an id from a different region than the provider is configured for, to help the user realize what is wrong in that case. --- terraform/context_import_test.go | 6 ++++++ terraform/eval_import_state.go | 19 ++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/terraform/context_import_test.go b/terraform/context_import_test.go index b2bb7c472..00317c98f 100644 --- a/terraform/context_import_test.go +++ b/terraform/context_import_test.go @@ -190,6 +190,12 @@ func TestContextImport_missingType(t *testing.T) { func TestContextImport_moduleProvider(t *testing.T) { p := mockProviderWithResourceTypeSchema("aws_instance", &configschema.Block{}) + p.GetSchemaReturn.Provider = &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "foo": {Type: cty.String, Optional: true}, + }, + } + m := testModule(t, "import-provider") ctx := testContext2(t, &ContextOpts{ Config: m, diff --git a/terraform/eval_import_state.go b/terraform/eval_import_state.go index 0b70bb92f..55a0f5c55 100644 --- a/terraform/eval_import_state.go +++ b/terraform/eval_import_state.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/hashicorp/terraform/addrs" + "github.com/hashicorp/terraform/tfdiags" ) // EvalImportState is an EvalNode implementation that performs an @@ -64,15 +65,19 @@ type EvalImportStateVerify struct { // TODO: test func (n *EvalImportStateVerify) Eval(ctx EvalContext) (interface{}, error) { + var diags tfdiags.Diagnostics + state := *n.State if state.Empty() { - return nil, fmt.Errorf( - "import %s (id: %s): Terraform detected a resource with this ID doesn't\n"+ - "exist. Please verify the ID is correct. You cannot import non-existent\n"+ - "resources using Terraform import.", - n.Addr.String(), - n.Id) + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Cannot import non-existent remote object", + fmt.Sprintf( + "While attempting to import an existing object to %s, the provider detected that no object exists with the id %q. Only pre-existing objects can be imported; check that the id is correct and that it is associated with the provider's configured region or endpoint, or use \"terraform apply\" to create a new remote object for this resource.", + n.Addr.String(), n.Id, + ), + )) } - return nil, nil + return nil, diags.ErrWithWarnings() }