github.com/loicalbertin/terraform@v0.6.15-0.20170626182346-8e2583055467/terraform/transform_resource_refresh_plannable.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  )
     7  
     8  // ResourceRefreshPlannableTransformer is a GraphTransformer that replaces any
     9  // nodes that don't have state yet exist in config with
    10  // NodePlannableResourceInstance.
    11  //
    12  // This transformer is used when expanding count on managed resource nodes
    13  // during the refresh phase to ensure that data sources that have
    14  // interpolations that depend on resources existing in the graph can be walked
    15  // properly.
    16  type ResourceRefreshPlannableTransformer struct {
    17  	// The full global state.
    18  	State *State
    19  }
    20  
    21  // Transform implements GraphTransformer for
    22  // ResourceRefreshPlannableTransformer.
    23  func (t *ResourceRefreshPlannableTransformer) Transform(g *Graph) error {
    24  nextVertex:
    25  	for _, v := range g.Vertices() {
    26  		addr := v.(*NodeRefreshableManagedResourceInstance).Addr
    27  
    28  		// Find the state for this address, if there is one
    29  		filter := &StateFilter{State: t.State}
    30  		results, err := filter.Filter(addr.String())
    31  		if err != nil {
    32  			return err
    33  		}
    34  
    35  		// Check to see if we have a state for this resource. If we do, skip this
    36  		// node.
    37  		for _, result := range results {
    38  			if _, ok := result.Value.(*ResourceState); ok {
    39  				continue nextVertex
    40  			}
    41  		}
    42  		// If we don't, convert this resource to a NodePlannableResourceInstance node
    43  		// with all of the data we need to make it happen.
    44  		log.Printf("[TRACE] No state for %s, converting to NodePlannableResourceInstance", addr.String())
    45  		new := &NodePlannableResourceInstance{
    46  			NodeAbstractResource: v.(*NodeRefreshableManagedResourceInstance).NodeAbstractResource,
    47  		}
    48  		// Replace the node in the graph
    49  		if !g.Replace(v, new) {
    50  			return fmt.Errorf("ResourceRefreshPlannableTransformer: Could not replace node %#v with %#v", v, new)
    51  		}
    52  	}
    53  
    54  	return nil
    55  }