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.
This commit is contained in:
Martin Atkins 2018-05-29 12:50:31 -07:00
parent 3de6a3db82
commit 7f3d7313d8
2 changed files with 18 additions and 7 deletions

View File

@ -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,

View File

@ -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()
}