diff --git a/terraform/graph_config_node_resource.go b/terraform/graph_config_node_resource.go index cecedb6ec6..70f83bb471 100644 --- a/terraform/graph_config_node_resource.go +++ b/terraform/graph_config_node_resource.go @@ -2,7 +2,6 @@ package terraform import ( "fmt" - "log" "strings" "github.com/hashicorp/terraform/config" @@ -262,55 +261,6 @@ func (n *GraphNodeConfigResource) DestroyNode() GraphNodeDestroy { return result } -// GraphNodeNoopPrunable -func (n *GraphNodeConfigResource) Noop(opts *NoopOpts) bool { - log.Printf("[DEBUG] Checking resource noop: %s", n.Name()) - // We don't have any noop optimizations for destroy nodes yet - if n.Destroy { - log.Printf("[DEBUG] Destroy node, not a noop") - return false - } - - // If there is no diff, then we aren't a noop since something needs to - // be done (such as a plan). We only check if we're a noop in a diff. - if opts.Diff == nil || opts.Diff.Empty() { - log.Printf("[DEBUG] No diff, not a noop") - return false - } - - // If the count has any interpolations, we can't prune this node since - // we need to be sure to evaluate the count so that splat variables work - // later (which need to know the full count). - if len(n.Resource.RawCount.Interpolations) > 0 { - log.Printf("[DEBUG] Count has interpolations, not a noop") - return false - } - - // If we have no module diff, we're certainly a noop. This is because - // it means there is a diff, and that the module we're in just isn't - // in it, meaning we're not doing anything. - if opts.ModDiff == nil || opts.ModDiff.Empty() { - log.Printf("[DEBUG] No mod diff, treating resource as a noop") - return true - } - - // Grab the ID which is the prefix (in the case count > 0 at some point) - prefix := n.Resource.Id() - - // Go through the diff and if there are any with our name on it, keep us - found := false - for k, _ := range opts.ModDiff.Resources { - if strings.HasPrefix(k, prefix) { - log.Printf("[DEBUG] Diff has %s, resource is not a noop", k) - found = true - break - } - } - - log.Printf("[DEBUG] Final noop value: %t", !found) - return !found -} - // Same as GraphNodeConfigResource, but for flattening type GraphNodeConfigResourceFlat struct { *GraphNodeConfigResource diff --git a/terraform/graph_config_node_variable.go b/terraform/graph_config_node_variable.go index ba62eb0567..9c5f9531b0 100644 --- a/terraform/graph_config_node_variable.go +++ b/terraform/graph_config_node_variable.go @@ -89,81 +89,6 @@ func (n *GraphNodeConfigVariable) DestroyEdgeInclude(v dag.Vertex) bool { return false } -// GraphNodeNoopPrunable -func (n *GraphNodeConfigVariable) Noop(opts *NoopOpts) bool { - log.Printf("[DEBUG] Checking variable noop: %s", n.Name()) - // If we have no diff, always keep this in the graph. We have to do - // this primarily for validation: we want to validate that variable - // interpolations are valid even if there are no resources that - // depend on them. - if opts.Diff == nil || opts.Diff.Empty() { - log.Printf("[DEBUG] No diff, not a noop") - return false - } - - // We have to find our our module diff since we do funky things with - // the flat node's implementation of Path() below. - modDiff := opts.Diff.ModuleByPath(n.ModulePath) - - // If we're destroying, we have no need of variables unless they are depended - // on by the count of a resource. - if modDiff != nil && modDiff.Destroy { - if n.hasDestroyEdgeInPath(opts, nil) { - log.Printf("[DEBUG] Variable has destroy edge from %s, not a noop", - dag.VertexName(opts.Vertex)) - return false - } - log.Printf("[DEBUG] Variable has no included destroy edges: noop!") - return true - } - - for _, v := range opts.Graph.UpEdges(opts.Vertex).List() { - // This is terrible, but I can't think of a better way to do this. - if dag.VertexName(v) == rootNodeName { - continue - } - - log.Printf("[DEBUG] Found up edge to %s, var is not noop", dag.VertexName(v)) - return false - } - - log.Printf("[DEBUG] No up edges, treating variable as a noop") - return true -} - -// hasDestroyEdgeInPath recursively walks for a destroy edge, ensuring that -// a variable both has no immediate destroy edges or any in its full module -// path, ensuring that links do not get severed in the middle. -func (n *GraphNodeConfigVariable) hasDestroyEdgeInPath(opts *NoopOpts, vertex dag.Vertex) bool { - if vertex == nil { - vertex = opts.Vertex - } - - log.Printf("[DEBUG] hasDestroyEdgeInPath: Looking for destroy edge: %s - %T", dag.VertexName(vertex), vertex) - for _, v := range opts.Graph.UpEdges(vertex).List() { - if len(opts.Graph.UpEdges(v).List()) > 1 { - if n.hasDestroyEdgeInPath(opts, v) == true { - return true - } - } - - // Here we borrow the implementation of DestroyEdgeInclude, whose logic - // and semantics are exactly what we want here. We add a check for the - // the root node, since we have to always depend on its existance. - if cv, ok := vertex.(*GraphNodeConfigVariableFlat); ok { - if dag.VertexName(v) == rootNodeName || cv.DestroyEdgeInclude(v) { - return true - } - } - } - return false -} - -// GraphNodeProxy impl. -func (n *GraphNodeConfigVariable) Proxy() bool { - return true -} - // GraphNodeEvalable impl. func (n *GraphNodeConfigVariable) EvalTree() EvalNode { // If we have no value, do nothing @@ -251,24 +176,3 @@ func (n *GraphNodeConfigVariableFlat) Path() []string { return nil } - -func (n *GraphNodeConfigVariableFlat) Noop(opts *NoopOpts) bool { - // First look for provider nodes that depend on this variable downstream - modDiff := opts.Diff.ModuleByPath(n.ModulePath) - if modDiff != nil && modDiff.Destroy { - ds, err := opts.Graph.Descendents(n) - if err != nil { - log.Printf("[ERROR] Error looking up descendents of %s: %s", n.Name(), err) - } else { - for _, d := range ds.List() { - if _, ok := d.(GraphNodeProvider); ok { - log.Printf("[DEBUG] This variable is depended on by a provider, can't be a noop.") - return false - } - } - } - } - - // Then fall back to existing impl - return n.GraphNodeConfigVariable.Noop(opts) -} diff --git a/terraform/transform_noop.go b/terraform/transform_noop.go deleted file mode 100644 index e36b619377..0000000000 --- a/terraform/transform_noop.go +++ /dev/null @@ -1,104 +0,0 @@ -package terraform - -import ( - "github.com/hashicorp/terraform/dag" -) - -// GraphNodeNoopPrunable can be implemented by nodes that can be -// pruned if they are noops. -type GraphNodeNoopPrunable interface { - Noop(*NoopOpts) bool -} - -// NoopOpts are the options available to determine if your node is a noop. -type NoopOpts struct { - Graph *Graph - Vertex dag.Vertex - Diff *Diff - State *State - ModDiff *ModuleDiff - ModState *ModuleState -} - -// PruneNoopTransformer is a graph transform that prunes nodes that -// consider themselves no-ops. This is done to both simplify the graph -// as well as to remove graph nodes that might otherwise cause problems -// during the graph run. Therefore, this transformer isn't completely -// an optimization step, and can instead be considered critical to -// Terraform operations. -// -// Example of the above case: variables for modules interpolate their values. -// Interpolation will fail on destruction (since attributes are being deleted), -// but variables shouldn't even eval if there is nothing that will consume -// the variable. Therefore, variables can note that they can be omitted -// safely in this case. -// -// The PruneNoopTransformer will prune nodes depth first, and will automatically -// create connect through the dependencies of pruned nodes. For example, -// if we have a graph A => B => C (A depends on B, etc.), and B decides to -// be removed, we'll still be left with A => C; the edge will be properly -// connected. -type PruneNoopTransformer struct { - Diff *Diff - State *State -} - -func (t *PruneNoopTransformer) Transform(g *Graph) error { - // Find the leaves. - leaves := make([]dag.Vertex, 0, 10) - for _, v := range g.Vertices() { - if g.DownEdges(v).Len() == 0 { - leaves = append(leaves, v) - } - } - - // Do a depth first walk from the leaves and remove things. - return g.ReverseDepthFirstWalk(leaves, func(v dag.Vertex, depth int) error { - // We need a prunable - pn, ok := v.(GraphNodeNoopPrunable) - if !ok { - return nil - } - - // Start building the noop opts - path := g.Path - if pn, ok := v.(GraphNodeSubPath); ok { - path = pn.Path() - } - - var modDiff *ModuleDiff - var modState *ModuleState - if t.Diff != nil { - modDiff = t.Diff.ModuleByPath(path) - } - if t.State != nil { - modState = t.State.ModuleByPath(path) - } - - // Determine if its a noop. If it isn't, just return - noop := pn.Noop(&NoopOpts{ - Graph: g, - Vertex: v, - Diff: t.Diff, - State: t.State, - ModDiff: modDiff, - ModState: modState, - }) - if !noop { - return nil - } - - // It is a noop! We first preserve edges. - up := g.UpEdges(v).List() - for _, downV := range g.DownEdges(v).List() { - for _, upV := range up { - g.Connect(dag.BasicEdge(upV, downV)) - } - } - - // Then remove it - g.Remove(v) - - return nil - }) -} diff --git a/terraform/transform_noop_test.go b/terraform/transform_noop_test.go deleted file mode 100644 index 65db95fda1..0000000000 --- a/terraform/transform_noop_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package terraform - -import ( - "strings" - "testing" - - "github.com/hashicorp/terraform/dag" -) - -func TestPruneNoopTransformer(t *testing.T) { - g := Graph{Path: RootModulePath} - - a := &testGraphNodeNoop{NameValue: "A"} - b := &testGraphNodeNoop{NameValue: "B", Value: true} - c := &testGraphNodeNoop{NameValue: "C"} - - g.Add(a) - g.Add(b) - g.Add(c) - g.Connect(dag.BasicEdge(a, b)) - g.Connect(dag.BasicEdge(b, c)) - - { - tf := &PruneNoopTransformer{} - if err := tf.Transform(&g); err != nil { - t.Fatalf("err: %s", err) - } - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testTransformPruneNoopStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - -const testTransformPruneNoopStr = ` -A - C -C -` - -type testGraphNodeNoop struct { - NameValue string - Value bool -} - -func (v *testGraphNodeNoop) Name() string { - return v.NameValue -} - -func (v *testGraphNodeNoop) Noop(*NoopOpts) bool { - return v.Value -}