forked from hashicorp/terraform
/
graph_config_node_provider.go
133 lines (108 loc) · 3.25 KB
/
graph_config_node_provider.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/dag"
)
// GraphNodeConfigProvider represents a configured provider within the
// configuration graph. These are only immediately in the graph when an
// explicit `provider` configuration block is in the configuration.
type GraphNodeConfigProvider struct {
Provider *config.ProviderConfig
}
func (n *GraphNodeConfigProvider) Name() string {
return fmt.Sprintf("provider.%s", n.ProviderName())
}
func (n *GraphNodeConfigProvider) ConfigType() GraphNodeConfigType {
return GraphNodeConfigTypeProvider
}
func (n *GraphNodeConfigProvider) DependableName() []string {
return []string{n.Name()}
}
func (n *GraphNodeConfigProvider) DependentOn() []string {
vars := n.Provider.RawConfig.Variables
result := make([]string, 0, len(vars))
for _, v := range vars {
if vn := varNameForVar(v); vn != "" {
result = append(result, vn)
}
}
return result
}
// GraphNodeEvalable impl.
func (n *GraphNodeConfigProvider) EvalTree() EvalNode {
return ProviderEvalTree(n.ProviderName(), n.Provider.RawConfig)
}
// GraphNodeProvider implementation
func (n *GraphNodeConfigProvider) ProviderName() string {
if n.Provider.Alias == "" {
return n.Provider.Name
} else {
return fmt.Sprintf("%s.%s", n.Provider.Name, n.Provider.Alias)
}
}
// GraphNodeProvider implementation
func (n *GraphNodeConfigProvider) ProviderConfig() *config.RawConfig {
return n.Provider.RawConfig
}
// GraphNodeDotter impl.
func (n *GraphNodeConfigProvider) DotNode(name string, opts *dag.DotOpts) *dag.DotNode {
return &dag.DotNode{
Name: name,
Attrs: map[string]string{
"label": n.Name(),
"shape": "diamond",
},
}
}
// GraphNodeDotterOrigin impl.
func (n *GraphNodeConfigProvider) DotOrigin() bool {
return true
}
// GraphNodeFlattenable impl.
func (n *GraphNodeConfigProvider) Flatten(p []string) (dag.Vertex, error) {
return &GraphNodeConfigProviderFlat{
GraphNodeConfigProvider: n,
PathValue: p,
}, nil
}
// Same as GraphNodeConfigProvider, but for flattening
type GraphNodeConfigProviderFlat struct {
*GraphNodeConfigProvider
PathValue []string
}
func (n *GraphNodeConfigProviderFlat) Name() string {
return fmt.Sprintf(
"%s.%s", modulePrefixStr(n.PathValue), n.GraphNodeConfigProvider.Name())
}
func (n *GraphNodeConfigProviderFlat) Path() []string {
return n.PathValue
}
func (n *GraphNodeConfigProviderFlat) DependableName() []string {
return modulePrefixList(
n.GraphNodeConfigProvider.DependableName(),
modulePrefixStr(n.PathValue))
}
func (n *GraphNodeConfigProviderFlat) DependentOn() []string {
prefixed := modulePrefixList(
n.GraphNodeConfigProvider.DependentOn(),
modulePrefixStr(n.PathValue))
result := make([]string, len(prefixed), len(prefixed)+1)
copy(result, prefixed)
// If we're in a module, then depend on our parent's provider
if len(n.PathValue) > 1 {
prefix := modulePrefixStr(n.PathValue[:len(n.PathValue)-1])
if prefix != "" {
prefix += "."
}
result = append(result, fmt.Sprintf(
"%s%s",
prefix, n.GraphNodeConfigProvider.Name()))
}
return result
}
func (n *GraphNodeConfigProviderFlat) ProviderName() string {
return fmt.Sprintf(
"%s.%s", modulePrefixStr(n.PathValue),
n.GraphNodeConfigProvider.ProviderName())
}