github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/transform_provider_disable.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/dag"
     7  )
     8  
     9  // DisableProviderTransformer "disables" any providers that are not actually
    10  // used by anything. This avoids the provider being initialized and configured.
    11  // This both saves resources but also avoids errors since configuration
    12  // may imply initialization which may require auth.
    13  type DisableProviderTransformer struct{}
    14  
    15  func (t *DisableProviderTransformer) Transform(g *Graph) error {
    16  	for _, v := range g.Vertices() {
    17  		// We only care about providers
    18  		pn, ok := v.(GraphNodeProvider)
    19  		if !ok || pn.ProviderName() == "" {
    20  			continue
    21  		}
    22  
    23  		// If we have dependencies, then don't disable
    24  		if g.UpEdges(v).Len() > 0 {
    25  			continue
    26  		}
    27  
    28  		// Get the path
    29  		var path []string
    30  		if pn, ok := v.(GraphNodeSubPath); ok {
    31  			path = pn.Path()
    32  		}
    33  
    34  		// Disable the provider by replacing it with a "disabled" provider
    35  		disabled := &NodeDisabledProvider{
    36  			NodeAbstractProvider: &NodeAbstractProvider{
    37  				NameValue: pn.ProviderName(),
    38  				PathValue: path,
    39  			},
    40  		}
    41  
    42  		if !g.Replace(v, disabled) {
    43  			panic(fmt.Sprintf(
    44  				"vertex disappeared from under us: %s",
    45  				dag.VertexName(v)))
    46  		}
    47  	}
    48  
    49  	return nil
    50  }