]>
Commit | Line | Data |
---|---|---|
1 | package terraform | |
2 | ||
3 | import ( | |
4 | "github.com/hashicorp/terraform/addrs" | |
5 | "github.com/hashicorp/terraform/configs/configschema" | |
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 ConcreteResourceInstanceNodeFunc | |
15 | Schema *configschema.Block | |
16 | ||
17 | // Count is either the number of indexed instances to create, or -1 to | |
18 | // indicate that count is not set at all and thus a no-key instance should | |
19 | // be created. | |
20 | Count int | |
21 | Addr addrs.AbsResource | |
22 | } | |
23 | ||
24 | func (t *ResourceCountTransformer) Transform(g *Graph) error { | |
25 | if t.Count < 0 { | |
26 | // Negative count indicates that count is not set at all. | |
27 | addr := t.Addr.Instance(addrs.NoKey) | |
28 | ||
29 | abstract := NewNodeAbstractResourceInstance(addr) | |
30 | abstract.Schema = t.Schema | |
31 | var node dag.Vertex = abstract | |
32 | if f := t.Concrete; f != nil { | |
33 | node = f(abstract) | |
34 | } | |
35 | ||
36 | g.Add(node) | |
37 | return nil | |
38 | } | |
39 | ||
40 | // For each count, build and add the node | |
41 | for i := 0; i < t.Count; i++ { | |
42 | key := addrs.IntKey(i) | |
43 | addr := t.Addr.Instance(key) | |
44 | ||
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 | ||
55 | return nil | |
56 | } |