diff --git a/terraform/transform_flatten.go b/terraform/transform_flatten.go index 206bf97bb..c727b3523 100644 --- a/terraform/transform_flatten.go +++ b/terraform/transform_flatten.go @@ -1,8 +1,6 @@ package terraform import ( - "fmt" - "github.com/hashicorp/terraform/dag" ) @@ -21,87 +19,3 @@ type GraphNodeFlatGraph interface { type GraphNodeFlattenable interface { Flatten(path []string) (dag.Vertex, error) } - -// FlattenTransformer is a transformer that goes through the graph, finds -// subgraphs that can be flattened, and flattens them into this graph, -// removing the prior subgraph node. -type FlattenTransformer struct{} - -func (t *FlattenTransformer) Transform(g *Graph) error { - for _, v := range g.Vertices() { - fn, ok := v.(GraphNodeFlatGraph) - if !ok { - continue - } - - // If we don't want to be flattened, don't do it - subgraph := fn.FlattenGraph() - if subgraph == nil { - continue - } - - // Get all the things that depend on this node. We'll re-connect - // dependents later. We have to copy these here since the UpEdges - // value will be deleted after the Remove below. - dependents := make([]dag.Vertex, 0, 5) - for _, v := range g.UpEdges(v).List() { - dependents = append(dependents, v) - } - - // Remove the old node - g.Remove(v) - - // Go through the subgraph and flatten all the nodes - for _, sv := range subgraph.Vertices() { - // If the vertex already has a subpath then we assume it has - // already been flattened. Ignore it. - if _, ok := sv.(GraphNodeSubPath); ok { - continue - } - - fn, ok := sv.(GraphNodeFlattenable) - if !ok { - return fmt.Errorf( - "unflattenable node: %s %T", - dag.VertexName(sv), sv) - } - - v, err := fn.Flatten(subgraph.Path) - if err != nil { - return fmt.Errorf( - "error flattening %s (%T): %s", - dag.VertexName(sv), sv, err) - } - - if v == nil { - subgraph.Remove(v) - } else { - subgraph.Replace(sv, v) - } - } - - // Now that we've handled any changes to the graph that are - // needed, we can add them all to our graph along with their edges. - for _, sv := range subgraph.Vertices() { - g.Add(sv) - } - for _, se := range subgraph.Edges() { - g.Connect(se) - } - - // Connect the dependencies for all the new nodes that we added. - // This will properly connect variables to their sources, for example. - for _, sv := range subgraph.Vertices() { - g.ConnectDependent(sv) - } - - // Re-connect all the things that dependent on the graph - // we just flattened. This should connect them back into the - // correct nodes if their DependentOn() is setup correctly. - for _, v := range dependents { - g.ConnectDependent(v) - } - } - - return nil -} diff --git a/terraform/transform_flatten_test.go b/terraform/transform_flatten_test.go deleted file mode 100644 index 68b49d9a5..000000000 --- a/terraform/transform_flatten_test.go +++ /dev/null @@ -1,88 +0,0 @@ -package terraform - -import ( - "strings" - "testing" -) - -func TestFlattenTransformer(t *testing.T) { - mod := testModule(t, "transform-flatten") - - var b BasicGraphBuilder - b = BasicGraphBuilder{ - Steps: []GraphTransformer{ - &ConfigTransformerOld{Module: mod}, - &VertexTransformer{ - Transforms: []GraphVertexTransformer{ - &ExpandTransform{ - Builder: &b, - }, - }, - }, - &FlattenTransformer{}, - }, - } - - g, err := b.Build(rootModulePath) - if err != nil { - t.Fatalf("err: %s", err) - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testTransformFlattenStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - -func TestFlattenTransformer_withProxy(t *testing.T) { - mod := testModule(t, "transform-flatten") - - var b BasicGraphBuilder - b = BasicGraphBuilder{ - Steps: []GraphTransformer{ - &ConfigTransformerOld{Module: mod}, - &VertexTransformer{ - Transforms: []GraphVertexTransformer{ - &ExpandTransform{ - Builder: &b, - }, - }, - }, - &FlattenTransformer{}, - &ProxyTransformer{}, - }, - } - - g, err := b.Build(rootModulePath) - if err != nil { - t.Fatalf("err: %s", err) - } - - actual := strings.TrimSpace(g.String()) - expected := strings.TrimSpace(testTransformFlattenProxyStr) - if actual != expected { - t.Fatalf("bad:\n\n%s", actual) - } -} - -const testTransformFlattenStr = ` -aws_instance.parent -aws_instance.parent-output -module.child.aws_instance.child - module.child.var.var -module.child.plan-destroy -module.child.var.var - aws_instance.parent -` - -const testTransformFlattenProxyStr = ` -aws_instance.parent -aws_instance.parent-output -module.child.aws_instance.child - aws_instance.parent - module.child.var.var -module.child.plan-destroy -module.child.var.var - aws_instance.parent -`