github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/terraform/eval_import_state.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // EvalImportState is an EvalNode implementation that performs an
     8  // ImportState operation on a provider. This will return the imported
     9  // states but won't modify any actual state.
    10  type EvalImportState struct {
    11  	Provider *ResourceProvider
    12  	Info     *InstanceInfo
    13  	Id       string
    14  	Output   *[]*InstanceState
    15  }
    16  
    17  // TODO: test
    18  func (n *EvalImportState) Eval(ctx EvalContext) (interface{}, error) {
    19  	provider := *n.Provider
    20  
    21  	{
    22  		// Call pre-import hook
    23  		err := ctx.Hook(func(h Hook) (HookAction, error) {
    24  			return h.PreImportState(n.Info, n.Id)
    25  		})
    26  		if err != nil {
    27  			return nil, err
    28  		}
    29  	}
    30  
    31  	// Import!
    32  	state, err := provider.ImportState(n.Info, n.Id)
    33  	if err != nil {
    34  		return nil, fmt.Errorf(
    35  			"import %s (id: %s): %s", n.Info.HumanId(), n.Id, err)
    36  	}
    37  
    38  	if n.Output != nil {
    39  		*n.Output = state
    40  	}
    41  
    42  	{
    43  		// Call post-import hook
    44  		err := ctx.Hook(func(h Hook) (HookAction, error) {
    45  			return h.PostImportState(n.Info, state)
    46  		})
    47  		if err != nil {
    48  			return nil, err
    49  		}
    50  	}
    51  
    52  	return nil, nil
    53  }
    54  
    55  // EvalImportStateVerify verifies the state after ImportState and
    56  // after the refresh to make sure it is non-nil and valid.
    57  type EvalImportStateVerify struct {
    58  	Info  *InstanceInfo
    59  	Id    string
    60  	State **InstanceState
    61  }
    62  
    63  // TODO: test
    64  func (n *EvalImportStateVerify) Eval(ctx EvalContext) (interface{}, error) {
    65  	state := *n.State
    66  	if state.Empty() {
    67  		return nil, fmt.Errorf(
    68  			"import %s (id: %s): Terraform detected a resource with this ID doesn't\n"+
    69  				"exist. Please verify the ID is correct. You cannot import non-existent\n"+
    70  				"resources using Terraform import.",
    71  			n.Info.HumanId(),
    72  			n.Id)
    73  	}
    74  
    75  	return nil, nil
    76  }