github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/terraform/node_resource_refresh.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  )
     8  
     9  // NodeRefreshableResource represents a resource that is "applyable":
    10  // it is ready to be applied and is represented by a diff.
    11  type NodeRefreshableResource struct {
    12  	*NodeAbstractResource
    13  }
    14  
    15  // GraphNodeDestroyer
    16  func (n *NodeRefreshableResource) DestroyAddr() *ResourceAddress {
    17  	return n.Addr
    18  }
    19  
    20  // GraphNodeEvalable
    21  func (n *NodeRefreshableResource) EvalTree() EvalNode {
    22  	// Eval info is different depending on what kind of resource this is
    23  	switch mode := n.Addr.Mode; mode {
    24  	case config.ManagedResourceMode:
    25  		return n.evalTreeManagedResource()
    26  	case config.DataResourceMode:
    27  		dn := &NodeRefreshableDataResourceInstance{
    28  			NodeAbstractResource: n.NodeAbstractResource,
    29  		}
    30  
    31  		return dn.EvalTree()
    32  	default:
    33  		panic(fmt.Errorf("unsupported resource mode %s", mode))
    34  	}
    35  }
    36  
    37  func (n *NodeRefreshableResource) evalTreeManagedResource() EvalNode {
    38  	addr := n.NodeAbstractResource.Addr
    39  
    40  	// stateId is the ID to put into the state
    41  	stateId := addr.stateId()
    42  
    43  	// Build the instance info. More of this will be populated during eval
    44  	info := &InstanceInfo{
    45  		Id:   stateId,
    46  		Type: addr.Type,
    47  	}
    48  
    49  	// Declare a bunch of variables that are used for state during
    50  	// evaluation. Most of this are written to by-address below.
    51  	var provider ResourceProvider
    52  	var state *InstanceState
    53  
    54  	// This happened during initial development. All known cases were
    55  	// fixed and tested but as a sanity check let's assert here.
    56  	if n.ResourceState == nil {
    57  		err := fmt.Errorf(
    58  			"No resource state attached for addr: %s\n\n"+
    59  				"This is a bug. Please report this to Terraform with your configuration\n"+
    60  				"and state attached. Please be careful to scrub any sensitive information.",
    61  			addr)
    62  		return &EvalReturnError{Error: &err}
    63  	}
    64  
    65  	return &EvalSequence{
    66  		Nodes: []EvalNode{
    67  			&EvalGetProvider{
    68  				Name:   n.ProvidedBy()[0],
    69  				Output: &provider,
    70  			},
    71  			&EvalReadState{
    72  				Name:   stateId,
    73  				Output: &state,
    74  			},
    75  			&EvalRefresh{
    76  				Info:     info,
    77  				Provider: &provider,
    78  				State:    &state,
    79  				Output:   &state,
    80  			},
    81  			&EvalWriteState{
    82  				Name:         stateId,
    83  				ResourceType: n.ResourceState.Type,
    84  				Provider:     n.ResourceState.Provider,
    85  				Dependencies: n.ResourceState.Dependencies,
    86  				State:        &state,
    87  			},
    88  		},
    89  	}
    90  }