github.com/ctrox/terraform@v0.11.12-beta1/terraform/transform.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform/dag"
     7  )
     8  
     9  // GraphTransformer is the interface that transformers implement. This
    10  // interface is only for transforms that need entire graph visibility.
    11  type GraphTransformer interface {
    12  	Transform(*Graph) error
    13  }
    14  
    15  // GraphVertexTransformer is an interface that transforms a single
    16  // Vertex within with graph. This is a specialization of GraphTransformer
    17  // that makes it easy to do vertex replacement.
    18  //
    19  // The GraphTransformer that runs through the GraphVertexTransformers is
    20  // VertexTransformer.
    21  type GraphVertexTransformer interface {
    22  	Transform(dag.Vertex) (dag.Vertex, error)
    23  }
    24  
    25  // GraphTransformIf is a helper function that conditionally returns a
    26  // GraphTransformer given. This is useful for calling inline a sequence
    27  // of transforms without having to split it up into multiple append() calls.
    28  func GraphTransformIf(f func() bool, then GraphTransformer) GraphTransformer {
    29  	if f() {
    30  		return then
    31  	}
    32  
    33  	return nil
    34  }
    35  
    36  type graphTransformerMulti struct {
    37  	Transforms []GraphTransformer
    38  }
    39  
    40  func (t *graphTransformerMulti) Transform(g *Graph) error {
    41  	for _, t := range t.Transforms {
    42  		if err := t.Transform(g); err != nil {
    43  			return err
    44  		}
    45  		log.Printf(
    46  			"[TRACE] Graph after step %T:\n\n%s",
    47  			t, g.StringWithNodeTypes())
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // GraphTransformMulti combines multiple graph transformers into a single
    54  // GraphTransformer that runs all the individual graph transformers.
    55  func GraphTransformMulti(ts ...GraphTransformer) GraphTransformer {
    56  	return &graphTransformerMulti{Transforms: ts}
    57  }