]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/hashicorp/terraform/terraform/transform_resource_count.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / terraform / transform_resource_count.go
CommitLineData
bae9f6d2
JC
1package terraform
2
3import (
107c1cdb
ND
4 "github.com/hashicorp/terraform/addrs"
5 "github.com/hashicorp/terraform/configs/configschema"
bae9f6d2
JC
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.
13type ResourceCountTransformer struct {
107c1cdb
ND
14 Concrete ConcreteResourceInstanceNodeFunc
15 Schema *configschema.Block
bae9f6d2 16
107c1cdb
ND
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.
bae9f6d2 20 Count int
107c1cdb 21 Addr addrs.AbsResource
bae9f6d2
JC
22}
23
24func (t *ResourceCountTransformer) Transform(g *Graph) error {
bae9f6d2 25 if t.Count < 0 {
107c1cdb
ND
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
bae9f6d2
JC
38 }
39
40 // For each count, build and add the node
41 for i := 0; i < t.Count; i++ {
107c1cdb
ND
42 key := addrs.IntKey(i)
43 addr := t.Addr.Instance(key)
bae9f6d2 44
107c1cdb
ND
45 abstract := NewNodeAbstractResourceInstance(addr)
46 abstract.Schema = t.Schema
bae9f6d2
JC
47 var node dag.Vertex = abstract
48 if f := t.Concrete; f != nil {
49 node = f(abstract)
50 }
51
bae9f6d2
JC
52 g.Add(node)
53 }
54
55 return nil
56}