github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/eval_import_state.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform-plugin-sdk/internal/addrs"
     8  	"github.com/hashicorp/terraform-plugin-sdk/internal/providers"
     9  	"github.com/hashicorp/terraform-plugin-sdk/internal/states"
    10  	"github.com/hashicorp/terraform-plugin-sdk/internal/tfdiags"
    11  )
    12  
    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
    19  	ID       string
    20  	Output   *[]providers.ImportedResource
    21  }
    22  
    23  // TODO: test
    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
    28  
    29  	{
    30  		// Call pre-import hook
    31  		err := ctx.Hook(func(h Hook) (HookAction, error) {
    32  			return h.PreImportState(absAddr, n.ID)
    33  		})
    34  		if err != nil {
    35  			return nil, err
    36  		}
    37  	}
    38  
    39  	resp := provider.ImportResourceState(providers.ImportResourceStateRequest{
    40  		TypeName: n.Addr.Resource.Type,
    41  		ID:       n.ID,
    42  	})
    43  	diags = diags.Append(resp.Diagnostics)
    44  	if diags.HasErrors() {
    45  		return nil, diags.Err()
    46  	}
    47  
    48  	imported := resp.ImportedResources
    49  
    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)
    52  	}
    53  
    54  	if n.Output != nil {
    55  		*n.Output = imported
    56  	}
    57  
    58  	{
    59  		// Call post-import hook
    60  		err := ctx.Hook(func(h Hook) (HookAction, error) {
    61  			return h.PostImportState(absAddr, imported)
    62  		})
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  	}
    67  
    68  	return nil, nil
    69  }
    70  
    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
    76  }
    77  
    78  // TODO: test
    79  func (n *EvalImportStateVerify) Eval(ctx EvalContext) (interface{}, error) {
    80  	var diags tfdiags.Diagnostics
    81  
    82  	state := *n.State
    83  	if state.Value.IsNull() {
    84  		diags = diags.Append(tfdiags.Sourceless(
    85  			tfdiags.Error,
    86  			"Cannot import non-existent remote object",
    87  			fmt.Sprintf(
    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.",
    89  				n.Addr.String(),
    90  			),
    91  		))
    92  	}
    93  
    94  	return nil, diags.ErrWithWarnings()
    95  }