opentofu/terraform/transform_resource_count.go
James Bardin a14fd0344c WIP reference providers by full name
This turned out to be a big messy commit, since the way providers are
referenced is tightly coupled throughout the code. That starts to unify
how providers are referenced, using the format output node Name method.

Add a new field to the internal resource data types called
ResolvedProvider. This is set by a new setter method SetProvider when a
resource is connected to a provider during graph creation. This allows
us to later lookup the provider instance a resource is connected to,
without requiring it to have the same module path.

The InitProvider context method now takes 2 arguments, one if the
provider type and the second is the full name of the provider. While the
provider type could still be parsed from the full name, this makes it
more explicit and, and changes to the name format won't effect this
code.
2017-11-02 15:00:06 -04:00

54 lines
1.1 KiB
Go

package terraform
import (
"fmt"
"github.com/hashicorp/terraform/dag"
)
// ResourceCountTransformer is a GraphTransformer that expands the count
// out for a specific resource.
//
// This assumes that the count is already interpolated.
type ResourceCountTransformer struct {
Concrete ConcreteResourceNodeFunc
Count int
Addr *ResourceAddress
}
func (t *ResourceCountTransformer) Transform(g *Graph) error {
// Don't allow the count to be negative
if t.Count < 0 {
return fmt.Errorf("negative count: %d", t.Count)
}
// For each count, build and add the node
for i := 0; i < t.Count; i++ {
// Set the index. If our count is 1 we special case it so that
// we handle the "resource.0" and "resource" boundary properly.
index := i
if t.Count == 1 {
index = -1
}
// Build the resource address
addr := t.Addr.Copy()
addr.Index = index
// Build the abstract node and the concrete one
abstract := &NodeAbstractResource{
Addr: addr,
}
var node dag.Vertex = abstract
if f := t.Concrete; f != nil {
node = f(abstract)
}
// Add it to the graph
g.Add(node)
}
return nil
}