7 "github.com/hashicorp/terraform/plugin/discovery"
10 // Module represents the dependencies of a single module, as well being
11 // a node in a tree of such structures representing the dependencies of
12 // an entire configuration.
19 // WalkFunc is a callback type for use with Module.WalkTree
20 type WalkFunc func(path []string, parent *Module, current *Module) error
22 // WalkTree calls the given callback once for the receiver and then
23 // once for each descendent, in an order such that parents are called
24 // before their children and siblings are called in the order they
25 // appear in the Children slice.
27 // When calling the callback, parent will be nil for the first call
28 // for the receiving module, and then set to the direct parent of
29 // each module for the subsequent calls.
31 // The path given to the callback is valid only until the callback
32 // returns, after which it will be mutated and reused. Callbacks must
33 // therefore copy the path slice if they wish to retain it.
35 // If the given callback returns an error, the walk will be aborted at
36 // that point and that error returned to the caller.
38 // This function is not thread-safe for concurrent modifications of the
39 // data structure, so it's the caller's responsibility to arrange for that
40 // should it be needed.
42 // It is safe for a callback to modify the descendents of the "current"
43 // module, including the ordering of the Children slice itself, but the
44 // callback MUST NOT modify the parent module.
45 func (m *Module) WalkTree(cb WalkFunc) error {
46 return walkModuleTree(make([]string, 0, 1), nil, m, cb)
49 func walkModuleTree(path []string, parent *Module, current *Module, cb WalkFunc) error {
50 path = append(path, current.Name)
51 err := cb(path, parent, current)
56 for _, child := range current.Children {
57 err := walkModuleTree(path, current, child, cb)
65 // SortChildren sorts the Children slice into lexicographic order by
68 // This is primarily useful prior to calling WalkTree so that the walk
69 // will proceed in a consistent order.
70 func (m *Module) SortChildren() {
71 sort.Sort(sortModules{m.Children})
74 // SortDescendents is a convenience wrapper for calling SortChildren on
75 // the receiver and all of its descendent modules.
76 func (m *Module) SortDescendents() {
77 m.WalkTree(func(path []string, parent *Module, current *Module) error {
78 current.SortChildren()
83 type sortModules struct {
87 func (s sortModules) Len() int {
91 func (s sortModules) Less(i, j int) bool {
92 cmp := strings.Compare(s.modules[i].Name, s.modules[j].Name)
96 func (s sortModules) Swap(i, j int) {
97 s.modules[i], s.modules[j] = s.modules[j], s.modules[i]
100 // PluginRequirements produces a PluginRequirements structure that can
101 // be used with discovery.PluginMetaSet.ConstrainVersions to identify
102 // suitable plugins to satisfy the module's provider dependencies.
104 // This method only considers the direct requirements of the receiver.
105 // Use AllPluginRequirements to flatten the dependencies for the
106 // entire tree of modules.
108 // Requirements returned by this method include only version constraints,
109 // and apply no particular SHA256 hash constraint.
110 func (m *Module) PluginRequirements() discovery.PluginRequirements {
111 ret := make(discovery.PluginRequirements)
112 for inst, dep := range m.Providers {
113 // m.Providers is keyed on provider names, such as "aws.foo".
114 // a PluginRequirements wants keys to be provider *types*, such
115 // as "aws". If there are multiple aliases for the same
116 // provider then we will flatten them into a single requirement
117 // by combining their constraint sets.
119 if existing, exists := ret[pty]; exists {
120 ret[pty].Versions = existing.Versions.Append(dep.Constraints)
122 ret[pty] = &discovery.PluginConstraints{
123 Versions: dep.Constraints,
130 // AllPluginRequirements calls PluginRequirements for the receiver and all
131 // of its descendents, and merges the result into a single PluginRequirements
132 // structure that would satisfy all of the modules together.
134 // Requirements returned by this method include only version constraints,
135 // and apply no particular SHA256 hash constraint.
136 func (m *Module) AllPluginRequirements() discovery.PluginRequirements {
137 var ret discovery.PluginRequirements
138 m.WalkTree(func(path []string, parent *Module, current *Module) error {
139 ret = ret.Merge(current.PluginRequirements())
145 // Equal returns true if the receiver is the root of an identical tree
146 // to the other given Module. This is a deep comparison that considers
147 // the equality of all downstream modules too.
149 // The children are considered to be ordered, so callers may wish to use
150 // SortDescendents first to normalize the order of the slices of child nodes.
152 // The implementation of this function is not optimized since it is provided
153 // primarily for use in tests.
154 func (m *Module) Equal(other *Module) bool {
155 // take care of nils first
156 if m == nil && other == nil {
158 } else if (m == nil && other != nil) || (m != nil && other == nil) {
162 if m.Name != other.Name {
166 if len(m.Providers) != len(other.Providers) {
169 if len(m.Children) != len(other.Children) {
173 // Can't use reflect.DeepEqual on this provider structure because
174 // the nested Constraints objects contain function pointers that
175 // never compare as equal. So we'll need to walk it the long way.
176 for inst, dep := range m.Providers {
177 if _, exists := other.Providers[inst]; !exists {
181 if dep.Reason != other.Providers[inst].Reason {
185 // Constraints are not too easy to compare robustly, so
186 // we'll just use their string representations as a proxy
188 if dep.Constraints.String() != other.Providers[inst].Constraints.String() {
193 // Above we already checked that we have the same number of children
194 // in each module, so now we just need to check that they are
195 // recursively equal.
196 for i := range m.Children {
197 if !m.Children[i].Equal(other.Children[i]) {
202 // If we fall out here then they are equal