2015-03-24 11:18:15 -05:00
|
|
|
package terraform
|
|
|
|
|
2015-05-01 11:01:49 -05:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
2015-03-24 11:18:15 -05:00
|
|
|
|
|
|
|
// TargetsTransformer is a GraphTransformer that, when the user specifies a
|
|
|
|
// list of resources to target, limits the graph to only those resources and
|
|
|
|
// their dependencies.
|
|
|
|
type TargetsTransformer struct {
|
|
|
|
// List of targeted resource names specified by the user
|
|
|
|
Targets []string
|
|
|
|
|
2016-01-07 14:43:43 -06:00
|
|
|
// List of parsed targets, provided by callers like ResourceCountTransform
|
|
|
|
// that already have the targets parsed
|
|
|
|
ParsedTargets []ResourceAddress
|
|
|
|
|
2015-03-24 11:18:15 -05:00
|
|
|
// Set to true when we're in a `terraform destroy` or a
|
|
|
|
// `terraform plan -destroy`
|
|
|
|
Destroy bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TargetsTransformer) Transform(g *Graph) error {
|
2016-01-07 14:43:43 -06:00
|
|
|
if len(t.Targets) > 0 && len(t.ParsedTargets) == 0 {
|
2015-03-30 19:02:36 -05:00
|
|
|
addrs, err := t.parseTargetAddresses()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-07 14:43:43 -06:00
|
|
|
t.ParsedTargets = addrs
|
|
|
|
}
|
|
|
|
if len(t.ParsedTargets) > 0 {
|
|
|
|
targetedNodes, err := t.selectTargetedNodes(g, t.ParsedTargets)
|
2015-03-24 11:18:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range g.Vertices() {
|
2016-07-29 10:53:13 -05:00
|
|
|
removable := false
|
2015-04-15 13:53:32 -05:00
|
|
|
if _, ok := v.(GraphNodeAddressable); ok {
|
2016-07-29 10:53:13 -05:00
|
|
|
removable = true
|
|
|
|
}
|
|
|
|
if vr, ok := v.(RemovableIfNotTargeted); ok {
|
|
|
|
removable = vr.RemoveIfNotTargeted()
|
|
|
|
}
|
|
|
|
if removable && !targetedNodes.Include(v) {
|
|
|
|
log.Printf("[DEBUG] Removing %q, filtered by targeting.", dag.VertexName(v))
|
|
|
|
g.Remove(v)
|
2015-03-24 11:18:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:02:36 -05:00
|
|
|
func (t *TargetsTransformer) parseTargetAddresses() ([]ResourceAddress, error) {
|
|
|
|
addrs := make([]ResourceAddress, len(t.Targets))
|
|
|
|
for i, target := range t.Targets {
|
|
|
|
ta, err := ParseResourceAddress(target)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addrs[i] = *ta
|
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:53:32 -05:00
|
|
|
// Returns the list of targeted nodes. A targeted node is either addressed
|
|
|
|
// directly, or is an Ancestor of a targeted node. Destroy mode keeps
|
|
|
|
// Descendents instead of Ancestors.
|
2015-03-30 19:02:36 -05:00
|
|
|
func (t *TargetsTransformer) selectTargetedNodes(
|
|
|
|
g *Graph, addrs []ResourceAddress) (*dag.Set, error) {
|
2015-03-24 11:18:15 -05:00
|
|
|
targetedNodes := new(dag.Set)
|
|
|
|
for _, v := range g.Vertices() {
|
2015-04-15 13:53:32 -05:00
|
|
|
if t.nodeIsTarget(v, addrs) {
|
|
|
|
targetedNodes.Add(v)
|
2015-03-24 11:18:15 -05:00
|
|
|
|
2015-04-15 13:53:32 -05:00
|
|
|
// We inform nodes that ask about the list of targets - helps for nodes
|
|
|
|
// that need to dynamically expand. Note that this only occurs for nodes
|
|
|
|
// that are already directly targeted.
|
|
|
|
if tn, ok := v.(GraphNodeTargetable); ok {
|
|
|
|
tn.SetTargets(addrs)
|
2015-03-30 19:02:36 -05:00
|
|
|
}
|
2015-03-24 11:18:15 -05:00
|
|
|
|
|
|
|
var deps *dag.Set
|
|
|
|
var err error
|
|
|
|
if t.Destroy {
|
2015-04-15 13:53:32 -05:00
|
|
|
deps, err = g.Descendents(v)
|
2016-09-23 15:07:41 -05:00
|
|
|
|
|
|
|
// Select any variables that we depend on in case we need them later for
|
|
|
|
// interpolating in the count
|
|
|
|
ancestors, _ := g.Ancestors(v)
|
|
|
|
for _, a := range ancestors.List() {
|
|
|
|
if _, ok := a.(*GraphNodeConfigVariableFlat); ok {
|
|
|
|
deps.Add(a)
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 11:18:15 -05:00
|
|
|
} else {
|
2015-04-15 13:53:32 -05:00
|
|
|
deps, err = g.Ancestors(v)
|
2015-03-24 11:18:15 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range deps.List() {
|
|
|
|
targetedNodes.Add(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return targetedNodes, nil
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:02:36 -05:00
|
|
|
func (t *TargetsTransformer) nodeIsTarget(
|
2015-04-15 13:53:32 -05:00
|
|
|
v dag.Vertex, addrs []ResourceAddress) bool {
|
|
|
|
r, ok := v.(GraphNodeAddressable)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2015-03-30 19:02:36 -05:00
|
|
|
addr := r.ResourceAddress()
|
|
|
|
for _, targetAddr := range addrs {
|
|
|
|
if targetAddr.Equals(addr) {
|
2015-03-24 11:18:15 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-07-29 10:53:13 -05:00
|
|
|
|
|
|
|
// RemovableIfNotTargeted is a special interface for graph nodes that
|
|
|
|
// aren't directly addressable, but need to be removed from the graph when they
|
|
|
|
// are not targeted. (Nodes that are not directly targeted end up in the set of
|
|
|
|
// targeted nodes because something that _is_ targeted depends on them.) The
|
|
|
|
// initial use case for this interface is GraphNodeConfigVariable, which was
|
|
|
|
// having trouble interpolating for module variables in targeted scenarios that
|
|
|
|
// filtered out the resource node being referenced.
|
|
|
|
type RemovableIfNotTargeted interface {
|
|
|
|
RemoveIfNotTargeted() bool
|
|
|
|
}
|