2015-02-08 23:00:13 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceCountTransformer is a GraphTransformer that expands the count
|
|
|
|
// out for a specific resource.
|
|
|
|
type ResourceCountTransformer struct {
|
|
|
|
Resource *config.Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ResourceCountTransformer) Transform(g *Graph) error {
|
|
|
|
// Expand the resource count
|
|
|
|
count, err := t.Resource.Count()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow the count to be negative
|
|
|
|
if count < 0 {
|
|
|
|
return fmt.Errorf("negative count: %d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each count, build and add the node
|
|
|
|
nodes := make([]dag.Vertex, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
// Save the node for later so we can do connections
|
|
|
|
nodes[i] = &graphNodeExpandedResource{
|
|
|
|
Index: i,
|
|
|
|
Resource: t.Resource,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the node now
|
|
|
|
g.Add(nodes[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the dependency connections
|
|
|
|
for _, n := range nodes {
|
|
|
|
// Connect the dependents. We ignore the return value for missing
|
|
|
|
// dependents since that should've been caught at a higher level.
|
|
|
|
g.ConnectDependent(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type graphNodeExpandedResource struct {
|
|
|
|
Index int
|
|
|
|
Resource *config.Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeExpandedResource) Name() string {
|
|
|
|
return fmt.Sprintf("%s #%d", n.Resource.Id(), n.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDependable impl.
|
|
|
|
func (n *graphNodeExpandedResource) DependableName() []string {
|
|
|
|
return []string{
|
|
|
|
n.Resource.Id(),
|
2015-02-10 23:12:49 +01:00
|
|
|
n.stateId(),
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDependent impl.
|
|
|
|
func (n *graphNodeExpandedResource) DependentOn() []string {
|
|
|
|
config := &GraphNodeConfigResource{Resource: n.Resource}
|
|
|
|
return config.DependentOn()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeProviderConsumer
|
|
|
|
func (n *graphNodeExpandedResource) ProvidedBy() string {
|
|
|
|
return resourceProvider(n.Resource.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable impl.
|
|
|
|
func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
2015-02-09 20:15:54 +01:00
|
|
|
seq := &EvalSequence{Nodes: make([]EvalNode, 0, 5)}
|
|
|
|
|
|
|
|
// Validate the resource
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalValidateResource{
|
|
|
|
Provider: &EvalGetProvider{Name: n.ProvidedBy()},
|
|
|
|
Config: &EvalInterpolate{Config: n.Resource.RawConfig},
|
|
|
|
ResourceName: n.Resource.Name,
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Validate all the provisioners
|
|
|
|
for _, p := range n.Resource.Provisioners {
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalValidateProvisioner{
|
|
|
|
Provisioner: &EvalGetProvisioner{Name: p.Type},
|
|
|
|
Config: &EvalInterpolate{Config: p.RawConfig},
|
|
|
|
})
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
2015-02-09 20:15:54 +01:00
|
|
|
|
2015-02-11 17:48:45 +01:00
|
|
|
// Refresh the resource
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkRefresh},
|
|
|
|
Node: &EvalWriteState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
Dependencies: n.DependentOn(),
|
|
|
|
State: &EvalRefresh{
|
|
|
|
Provider: &EvalGetProvider{Name: n.ProvidedBy()},
|
|
|
|
State: &EvalReadState{Name: n.stateId()},
|
|
|
|
Info: &InstanceInfo{Id: n.stateId(), Type: n.Resource.Type},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-02-09 20:15:54 +01:00
|
|
|
return seq
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
2015-02-10 23:12:49 +01:00
|
|
|
|
|
|
|
// stateId is the name used for the state key
|
|
|
|
func (n *graphNodeExpandedResource) stateId() string {
|
2015-02-11 17:48:45 +01:00
|
|
|
if n.Index == 0 {
|
|
|
|
return n.Resource.Id()
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:12:49 +01:00
|
|
|
return fmt.Sprintf("%s.%d", n.Resource.Id(), n.Index)
|
|
|
|
}
|