4 "github.com/hashicorp/terraform/addrs"
5 "github.com/hashicorp/terraform/configs/configschema"
6 "github.com/hashicorp/terraform/dag"
7 "github.com/zclconf/go-cty/cty"
10 // ResourceCountTransformer is a GraphTransformer that expands the count
11 // out for a specific resource.
13 // This assumes that the count is already interpolated.
14 type ResourceCountTransformer struct {
15 Concrete ConcreteResourceInstanceNodeFunc
16 Schema *configschema.Block
18 // Count is either the number of indexed instances to create, or -1 to
19 // indicate that count is not set at all and thus a no-key instance should
22 ForEach map[string]cty.Value
23 Addr addrs.AbsResource
26 func (t *ResourceCountTransformer) Transform(g *Graph) error {
27 if t.Count < 0 && t.ForEach == nil {
28 // Negative count indicates that count is not set at all.
29 addr := t.Addr.Instance(addrs.NoKey)
31 abstract := NewNodeAbstractResourceInstance(addr)
32 abstract.Schema = t.Schema
33 var node dag.Vertex = abstract
34 if f := t.Concrete; f != nil {
42 // Add nodes related to the for_each expression
43 for key := range t.ForEach {
44 addr := t.Addr.Instance(addrs.StringKey(key))
45 abstract := NewNodeAbstractResourceInstance(addr)
46 abstract.Schema = t.Schema
47 var node dag.Vertex = abstract
48 if f := t.Concrete; f != nil {
55 // For each count, build and add the node
56 for i := 0; i < t.Count; i++ {
57 key := addrs.IntKey(i)
58 addr := t.Addr.Instance(key)
60 abstract := NewNodeAbstractResourceInstance(addr)
61 abstract.Schema = t.Schema
62 var node dag.Vertex = abstract
63 if f := t.Concrete; f != nil {