forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 7
/
transform_reference.go
180 lines (154 loc) · 4.82 KB
/
transform_reference.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/dag"
)
// GraphNodeReferenceable must be implemented by any node that represents
// a Terraform thing that can be referenced (resource, module, etc.).
type GraphNodeReferenceable interface {
// ReferenceableName is the name by which this can be referenced.
// This can be either just the type, or include the field. Example:
// "aws_instance.bar" or "aws_instance.bar.id".
ReferenceableName() []string
}
// GraphNodeReferencer must be implemented by nodes that reference other
// Terraform items and therefore depend on them.
type GraphNodeReferencer interface {
// References are the list of things that this node references. This
// can include fields or just the type, just like GraphNodeReferenceable
// above.
References() []string
}
// GraphNodeReferenceGlobal is an interface that can optionally be
// implemented. If ReferenceGlobal returns true, then the References()
// and ReferenceableName() must be _fully qualified_ with "module.foo.bar"
// etc.
//
// This allows a node to reference and be referenced by a specific name
// that may cross module boundaries. This can be very dangerous so use
// this wisely.
//
// The primary use case for this is module boundaries (variables coming in).
type GraphNodeReferenceGlobal interface {
// Set to true to signal that references and name are fully
// qualified. See the above docs for more information.
ReferenceGlobal() bool
}
// ReferenceTransformer is a GraphTransformer that connects all the
// nodes that reference each other in order to form the proper ordering.
type ReferenceTransformer struct{}
func (t *ReferenceTransformer) Transform(g *Graph) error {
// Build a reference map so we can efficiently look up the references
vs := g.Vertices()
m := NewReferenceMap(vs)
// Find the things that reference things and connect them
for _, v := range vs {
parents, _ := m.References(v)
for _, parent := range parents {
g.Connect(dag.BasicEdge(v, parent))
}
}
return nil
}
// ReferenceMap is a structure that can be used to efficiently check
// for references on a graph.
type ReferenceMap struct {
// m is the mapping of referenceable name to list of verticies that
// implement that name. This is built on initialization.
m map[string][]dag.Vertex
}
// References returns the list of vertices that this vertex
// references along with any missing references.
func (m *ReferenceMap) References(v dag.Vertex) ([]dag.Vertex, []string) {
rn, ok := v.(GraphNodeReferencer)
if !ok {
return nil, nil
}
var matches []dag.Vertex
var missing []string
prefix := m.prefix(v)
for _, n := range rn.References() {
n = prefix + n
parents, ok := m.m[n]
if !ok {
missing = append(missing, n)
continue
}
// Make sure this isn't a self reference, which isn't included
selfRef := false
for _, p := range parents {
if p == v {
selfRef = true
break
}
}
if selfRef {
continue
}
matches = append(matches, parents...)
}
return matches, missing
}
func (m *ReferenceMap) prefix(v dag.Vertex) string {
// If the node is stating it is already fully qualified then
// we don't have to create the prefix!
if gn, ok := v.(GraphNodeReferenceGlobal); ok && gn.ReferenceGlobal() {
return ""
}
// Create the prefix based on the path
var prefix string
if pn, ok := v.(GraphNodeSubPath); ok {
if path := normalizeModulePath(pn.Path()); len(path) > 1 {
prefix = modulePrefixStr(path) + "."
}
}
return prefix
}
// NewReferenceMap is used to create a new reference map for the
// given set of vertices.
func NewReferenceMap(vs []dag.Vertex) *ReferenceMap {
var m ReferenceMap
// Build the lookup table
refMap := make(map[string][]dag.Vertex)
for _, v := range vs {
// We're only looking for referenceable nodes
rn, ok := v.(GraphNodeReferenceable)
if !ok {
continue
}
// Go through and cache them
prefix := m.prefix(v)
for _, n := range rn.ReferenceableName() {
n = prefix + n
refMap[n] = append(refMap[n], v)
}
}
m.m = refMap
return &m
}
// ReferencesFromConfig returns the references that a configuration has
// based on the interpolated variables in a configuration.
func ReferencesFromConfig(c *config.RawConfig) []string {
var result []string
for _, v := range c.Variables {
if r := ReferenceFromInterpolatedVar(v); r != "" {
result = append(result, r)
}
}
return result
}
// ReferenceFromInterpolatedVar returns the reference from this variable,
// or an empty string if there is no reference.
func ReferenceFromInterpolatedVar(v config.InterpolatedVariable) string {
switch v := v.(type) {
case *config.ModuleVariable:
return fmt.Sprintf("module.%s.output.%s", v.Name, v.Field)
case *config.ResourceVariable:
return v.ResourceId()
case *config.UserVariable:
return fmt.Sprintf("var.%s", v.Name)
default:
return ""
}
}