"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/dag"
"github.com/hashicorp/terraform/states"
+ "github.com/zclconf/go-cty/cty"
)
// OrphanResourceCountTransformer is a GraphTransformer that adds orphans
type OrphanResourceCountTransformer struct {
Concrete ConcreteResourceInstanceNodeFunc
- Count int // Actual count of the resource, or -1 if count is not set at all
- Addr addrs.AbsResource // Addr of the resource to look for orphans
- State *states.State // Full global state
+ Count int // Actual count of the resource, or -1 if count is not set at all
+ ForEach map[string]cty.Value // The ForEach map on the resource
+ Addr addrs.AbsResource // Addr of the resource to look for orphans
+ State *states.State // Full global state
}
func (t *OrphanResourceCountTransformer) Transform(g *Graph) error {
haveKeys[key] = struct{}{}
}
+ // if for_each is set, use that transformer
+ if t.ForEach != nil {
+ return t.transformForEach(haveKeys, g)
+ }
if t.Count < 0 {
return t.transformNoCount(haveKeys, g)
}
return t.transformCount(haveKeys, g)
}
+func (t *OrphanResourceCountTransformer) transformForEach(haveKeys map[addrs.InstanceKey]struct{}, g *Graph) error {
+ for key := range haveKeys {
+ s, _ := key.(addrs.StringKey)
+ // If the key is present in our current for_each, carry on
+ if _, ok := t.ForEach[string(s)]; ok {
+ continue
+ }
+
+ abstract := NewNodeAbstractResourceInstance(t.Addr.Instance(key))
+ var node dag.Vertex = abstract
+ if f := t.Concrete; f != nil {
+ node = f(abstract)
+ }
+ log.Printf("[TRACE] OrphanResourceCount(non-zero): adding %s as %T", t.Addr, node)
+ g.Add(node)
+ }
+ return nil
+}
+
func (t *OrphanResourceCountTransformer) transformCount(haveKeys map[addrs.InstanceKey]struct{}, g *Graph) error {
// Due to the logic in Transform, we only get in here if our count is
// at least one.