github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/transform_resource_count.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/dag"
     7  )
     8  
     9  // ResourceCountTransformer is a GraphTransformer that expands the count
    10  // out for a specific resource.
    11  //
    12  // This assumes that the count is already interpolated.
    13  type ResourceCountTransformer struct {
    14  	Concrete ConcreteResourceNodeFunc
    15  
    16  	Count int
    17  	Addr  *ResourceAddress
    18  }
    19  
    20  func (t *ResourceCountTransformer) Transform(g *Graph) error {
    21  	// Don't allow the count to be negative
    22  	if t.Count < 0 {
    23  		return fmt.Errorf("negative count: %d", t.Count)
    24  	}
    25  
    26  	// For each count, build and add the node
    27  	for i := 0; i < t.Count; i++ {
    28  		// Set the index. If our count is 1 we special case it so that
    29  		// we handle the "resource.0" and "resource" boundary properly.
    30  		index := i
    31  		if t.Count == 1 {
    32  			index = -1
    33  		}
    34  
    35  		// Build the resource address
    36  		addr := t.Addr.Copy()
    37  		addr.Index = index
    38  
    39  		// Build the abstract node and the concrete one
    40  		abstract := &NodeAbstractResource{
    41  			Addr: addr,
    42  		}
    43  		var node dag.Vertex = abstract
    44  		if f := t.Concrete; f != nil {
    45  			node = f(abstract)
    46  		}
    47  
    48  		// Add it to the graph
    49  		g.Add(node)
    50  	}
    51  
    52  	return nil
    53  }