aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/terraform/transform_provider_disable.go
blob: d9919f3a774da32067cd8f235d5016119f4386ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package terraform

import (
	"fmt"

	"github.com/hashicorp/terraform/dag"
)

// DisableProviderTransformer "disables" any providers that are not actually
// used by anything. This avoids the provider being initialized and configured.
// This both saves resources but also avoids errors since configuration
// may imply initialization which may require auth.
type DisableProviderTransformer struct{}

func (t *DisableProviderTransformer) Transform(g *Graph) error {
	for _, v := range g.Vertices() {
		// We only care about providers
		pn, ok := v.(GraphNodeProvider)
		if !ok || pn.ProviderName() == "" {
			continue
		}

		// If we have dependencies, then don't disable
		if g.UpEdges(v).Len() > 0 {
			continue
		}

		// Get the path
		var path []string
		if pn, ok := v.(GraphNodeSubPath); ok {
			path = pn.Path()
		}

		// Disable the provider by replacing it with a "disabled" provider
		disabled := &NodeDisabledProvider{
			NodeAbstractProvider: &NodeAbstractProvider{
				NameValue: pn.ProviderName(),
				PathValue: path,
			},
		}

		if !g.Replace(v, disabled) {
			panic(fmt.Sprintf(
				"vertex disappeared from under us: %s",
				dag.VertexName(v)))
		}
	}

	return nil
}