github.com/trawler/terraform@v0.10.8-0.20171106022149-4b1c7a1d9b48/terraform/transform_vertex.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/dag"
     7  )
     8  
     9  // VertexTransformer is a GraphTransformer that transforms vertices
    10  // using the GraphVertexTransformers. The Transforms are run in sequential
    11  // order. If a transform replaces a vertex then the next transform will see
    12  // the new vertex.
    13  type VertexTransformer struct {
    14  	Transforms []GraphVertexTransformer
    15  }
    16  
    17  func (t *VertexTransformer) Transform(g *Graph) error {
    18  	for _, v := range g.Vertices() {
    19  		for _, vt := range t.Transforms {
    20  			newV, err := vt.Transform(v)
    21  			if err != nil {
    22  				return err
    23  			}
    24  
    25  			// If the vertex didn't change, then don't do anything more
    26  			if newV == v {
    27  				continue
    28  			}
    29  
    30  			// Vertex changed, replace it within the graph
    31  			if ok := g.Replace(v, newV); !ok {
    32  				// This should never happen, big problem
    33  				return fmt.Errorf(
    34  					"Failed to replace %s with %s!\n\nSource: %#v\n\nTarget: %#v",
    35  					dag.VertexName(v), dag.VertexName(newV), v, newV)
    36  			}
    37  
    38  			// Replace v so that future transforms use the proper vertex
    39  			v = newV
    40  		}
    41  	}
    42  
    43  	return nil
    44  }