]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package terraform |
2 | ||
3 | import ( | |
107c1cdb ND |
4 | "github.com/hashicorp/terraform/addrs" |
5 | "github.com/hashicorp/terraform/configs/configschema" | |
bae9f6d2 | 6 | "github.com/hashicorp/terraform/dag" |
863486a6 | 7 | "github.com/zclconf/go-cty/cty" |
bae9f6d2 JC |
8 | ) |
9 | ||
10 | // ResourceCountTransformer is a GraphTransformer that expands the count | |
11 | // out for a specific resource. | |
12 | // | |
13 | // This assumes that the count is already interpolated. | |
14 | type ResourceCountTransformer struct { | |
107c1cdb ND |
15 | Concrete ConcreteResourceInstanceNodeFunc |
16 | Schema *configschema.Block | |
bae9f6d2 | 17 | |
107c1cdb ND |
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 | |
20 | // be created. | |
863486a6 AG |
21 | Count int |
22 | ForEach map[string]cty.Value | |
23 | Addr addrs.AbsResource | |
bae9f6d2 JC |
24 | } |
25 | ||
26 | func (t *ResourceCountTransformer) Transform(g *Graph) error { | |
863486a6 | 27 | if t.Count < 0 && t.ForEach == nil { |
107c1cdb ND |
28 | // Negative count indicates that count is not set at all. |
29 | addr := t.Addr.Instance(addrs.NoKey) | |
30 | ||
31 | abstract := NewNodeAbstractResourceInstance(addr) | |
32 | abstract.Schema = t.Schema | |
33 | var node dag.Vertex = abstract | |
34 | if f := t.Concrete; f != nil { | |
35 | node = f(abstract) | |
36 | } | |
37 | ||
38 | g.Add(node) | |
39 | return nil | |
bae9f6d2 JC |
40 | } |
41 | ||
863486a6 AG |
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 { | |
49 | node = f(abstract) | |
50 | } | |
51 | ||
52 | g.Add(node) | |
53 | } | |
54 | ||
bae9f6d2 JC |
55 | // For each count, build and add the node |
56 | for i := 0; i < t.Count; i++ { | |
107c1cdb ND |
57 | key := addrs.IntKey(i) |
58 | addr := t.Addr.Instance(key) | |
bae9f6d2 | 59 | |
107c1cdb ND |
60 | abstract := NewNodeAbstractResourceInstance(addr) |
61 | abstract.Schema = t.Schema | |
bae9f6d2 JC |
62 | var node dag.Vertex = abstract |
63 | if f := t.Concrete; f != nil { | |
64 | node = f(abstract) | |
65 | } | |
66 | ||
bae9f6d2 JC |
67 | g.Add(node) |
68 | } | |
69 | ||
70 | return nil | |
71 | } |