7 "github.com/hashicorp/terraform/addrs"
8 "github.com/hashicorp/terraform/providers"
9 "github.com/hashicorp/terraform/states"
10 "github.com/hashicorp/terraform/tfdiags"
13 // EvalImportState is an EvalNode implementation that performs an
14 // ImportState operation on a provider. This will return the imported
15 // states but won't modify any actual state.
16 type EvalImportState struct {
17 Addr addrs.ResourceInstance
18 Provider *providers.Interface
20 Output *[]providers.ImportedResource
24 func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) {
25 absAddr := n.Addr.Absolute(ctx.Path())
26 provider := *n.Provider
27 var diags tfdiags.Diagnostics
30 // Call pre-import hook
31 err := ctx.Hook(func(h Hook) (HookAction, error) {
32 return h.PreImportState(absAddr, n.ID)
39 resp := provider.ImportResourceState(providers.ImportResourceStateRequest{
40 TypeName: n.Addr.Resource.Type,
43 diags = diags.Append(resp.Diagnostics)
44 if diags.HasErrors() {
45 return nil, diags.Err()
48 imported := resp.ImportedResources
50 for _, obj := range imported {
51 log.Printf("[TRACE] EvalImportState: import %s %q produced instance object of type %s", absAddr.String(), n.ID, obj.TypeName)
59 // Call post-import hook
60 err := ctx.Hook(func(h Hook) (HookAction, error) {
61 return h.PostImportState(absAddr, imported)
71 // EvalImportStateVerify verifies the state after ImportState and
72 // after the refresh to make sure it is non-nil and valid.
73 type EvalImportStateVerify struct {
74 Addr addrs.ResourceInstance
75 State **states.ResourceInstanceObject
79 func (n *EvalImportStateVerify) Eval(ctx EvalContext) (interface{}, error) {
80 var diags tfdiags.Diagnostics
83 if state.Value.IsNull() {
84 diags = diags.Append(tfdiags.Sourceless(
86 "Cannot import non-existent remote object",
88 "While attempting to import an existing object to %s, the provider detected that no object exists with the given id. 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.",
94 return nil, diags.ErrWithWarnings()