github.com/hashicorp/terraform-plugin-sdk@v1.17.2/terraform/transform.go (about) 1 package terraform 2 3 import ( 4 "log" 5 6 "github.com/hashicorp/terraform-plugin-sdk/internal/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 var lastStepStr string 42 for _, t := range t.Transforms { 43 log.Printf("[TRACE] (graphTransformerMulti) Executing graph transform %T", t) 44 if err := t.Transform(g); err != nil { 45 return err 46 } 47 if thisStepStr := g.StringWithNodeTypes(); thisStepStr != lastStepStr { 48 log.Printf("[TRACE] (graphTransformerMulti) Completed graph transform %T with new graph:\n%s------", t, thisStepStr) 49 lastStepStr = thisStepStr 50 } else { 51 log.Printf("[TRACE] (graphTransformerMulti) Completed graph transform %T (no changes)", t) 52 } 53 } 54 55 return nil 56 } 57 58 // GraphTransformMulti combines multiple graph transformers into a single 59 // GraphTransformer that runs all the individual graph transformers. 60 func GraphTransformMulti(ts ...GraphTransformer) GraphTransformer { 61 return &graphTransformerMulti{Transforms: ts} 62 }