2018-02-01 22:33:06 -06:00
|
|
|
package configs
|
|
|
|
|
2018-02-07 18:40:58 -06:00
|
|
|
import (
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-21 19:39:27 -05:00
|
|
|
"sort"
|
|
|
|
|
2018-02-07 18:40:58 -06:00
|
|
|
version "github.com/hashicorp/go-version"
|
2019-09-09 17:58:44 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-04-06 13:10:21 -05:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-02-07 18:40:58 -06:00
|
|
|
)
|
|
|
|
|
2018-02-01 22:33:06 -06:00
|
|
|
// A Config is a node in the tree of modules within a configuration.
|
|
|
|
//
|
|
|
|
// The module tree is constructed by following ModuleCall instances recursively
|
|
|
|
// through the root module transitively into descendent modules.
|
|
|
|
//
|
|
|
|
// A module tree described in *this* package represents the static tree
|
|
|
|
// represented by configuration. During evaluation a static ModuleNode may
|
|
|
|
// expand into zero or more module instances depending on the use of count and
|
|
|
|
// for_each configuration attributes within each call.
|
|
|
|
type Config struct {
|
|
|
|
// RootModule points to the Config for the root module within the same
|
|
|
|
// module tree as this module. If this module _is_ the root module then
|
|
|
|
// this is self-referential.
|
|
|
|
Root *Config
|
|
|
|
|
|
|
|
// ParentModule points to the Config for the module that directly calls
|
|
|
|
// this module. If this is the root module then this field is nil.
|
|
|
|
Parent *Config
|
|
|
|
|
2018-02-09 17:32:49 -06:00
|
|
|
// Path is a sequence of module logical names that traverse from the root
|
|
|
|
// module to this config. Path is empty for the root module.
|
|
|
|
//
|
2018-04-06 13:10:21 -05:00
|
|
|
// This should only be used to display paths to the end-user in rare cases
|
|
|
|
// where we are talking about the static module tree, before module calls
|
2019-03-21 16:05:41 -05:00
|
|
|
// have been resolved. In most cases, an addrs.ModuleInstance describing
|
2018-04-06 13:10:21 -05:00
|
|
|
// a node in the dynamic module tree is better, since it will then include
|
|
|
|
// any keys resulting from evaluating "count" and "for_each" arguments.
|
|
|
|
Path addrs.Module
|
2018-02-09 17:32:49 -06:00
|
|
|
|
2018-02-01 22:33:06 -06:00
|
|
|
// ChildModules points to the Config for each of the direct child modules
|
|
|
|
// called from this module. The keys in this map match the keys in
|
|
|
|
// Module.ModuleCalls.
|
|
|
|
Children map[string]*Config
|
|
|
|
|
2018-02-07 18:40:58 -06:00
|
|
|
// Module points to the object describing the configuration for the
|
2018-02-01 22:33:06 -06:00
|
|
|
// various elements (variables, resources, etc) defined by this module.
|
2018-02-07 18:40:58 -06:00
|
|
|
Module *Module
|
|
|
|
|
2018-02-08 20:56:48 -06:00
|
|
|
// CallRange is the source range for the header of the module block that
|
|
|
|
// requested this module.
|
|
|
|
//
|
|
|
|
// This field is meaningless for the root module, where its contents are undefined.
|
|
|
|
CallRange hcl.Range
|
|
|
|
|
2018-02-07 18:40:58 -06:00
|
|
|
// SourceAddr is the source address that the referenced module was requested
|
|
|
|
// from, as specified in configuration.
|
|
|
|
//
|
|
|
|
// This field is meaningless for the root module, where its contents are undefined.
|
|
|
|
SourceAddr string
|
|
|
|
|
|
|
|
// SourceAddrRange is the location in the configuration source where the
|
|
|
|
// SourceAddr value was set, for use in diagnostic messages.
|
|
|
|
//
|
|
|
|
// This field is meaningless for the root module, where its contents are undefined.
|
|
|
|
SourceAddrRange hcl.Range
|
|
|
|
|
|
|
|
// Version is the specific version that was selected for this module,
|
|
|
|
// based on version constraints given in configuration.
|
|
|
|
//
|
2018-02-08 20:56:48 -06:00
|
|
|
// This field is nil if the module was loaded from a non-registry source,
|
|
|
|
// since versions are not supported for other sources.
|
|
|
|
//
|
|
|
|
// This field is meaningless for the root module, where it will always
|
|
|
|
// be nil.
|
2018-02-07 18:40:58 -06:00
|
|
|
Version *version.Version
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:57:21 -05:00
|
|
|
// NewEmptyConfig constructs a single-node configuration tree with an empty
|
|
|
|
// root module. This is generally a pretty useless thing to do, so most callers
|
|
|
|
// should instead use BuildConfig.
|
|
|
|
func NewEmptyConfig() *Config {
|
|
|
|
ret := &Config{}
|
|
|
|
ret.Root = ret
|
|
|
|
ret.Children = make(map[string]*Config)
|
|
|
|
ret.Module = &Module{}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2018-02-07 18:40:58 -06:00
|
|
|
// Depth returns the number of "hops" the receiver is from the root of its
|
|
|
|
// module tree, with the root module having a depth of zero.
|
|
|
|
func (c *Config) Depth() int {
|
|
|
|
ret := 0
|
|
|
|
this := c
|
|
|
|
for this.Parent != nil {
|
|
|
|
ret++
|
|
|
|
this = this.Parent
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeepEach calls the given function once for each module in the tree, starting
|
|
|
|
// with the receiver.
|
|
|
|
//
|
|
|
|
// A parent is always called before its children and children of a particular
|
|
|
|
// node are visited in lexicographic order by their names.
|
|
|
|
func (c *Config) DeepEach(cb func(c *Config)) {
|
|
|
|
cb(c)
|
|
|
|
|
|
|
|
names := make([]string, 0, len(c.Children))
|
|
|
|
for name := range c.Children {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range names {
|
|
|
|
c.Children[name].DeepEach(cb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllModules returns a slice of all the receiver and all of its descendent
|
|
|
|
// nodes in the module tree, in the same order they would be visited by
|
|
|
|
// DeepEach.
|
|
|
|
func (c *Config) AllModules() []*Config {
|
|
|
|
var ret []*Config
|
|
|
|
c.DeepEach(func(c *Config) {
|
|
|
|
ret = append(ret, c)
|
|
|
|
})
|
|
|
|
return ret
|
2018-02-01 22:33:06 -06:00
|
|
|
}
|
2018-03-27 17:31:05 -05:00
|
|
|
|
|
|
|
// Descendent returns the descendent config that has the given path beneath
|
|
|
|
// the receiver, or nil if there is no such module.
|
|
|
|
//
|
|
|
|
// The path traverses the static module tree, prior to any expansion to handle
|
|
|
|
// count and for_each arguments.
|
|
|
|
//
|
|
|
|
// An empty path will just return the receiver, and is therefore pointless.
|
2018-04-06 13:10:21 -05:00
|
|
|
func (c *Config) Descendent(path addrs.Module) *Config {
|
2018-03-27 17:31:05 -05:00
|
|
|
current := c
|
|
|
|
for _, name := range path {
|
|
|
|
current = current.Children[name]
|
|
|
|
if current == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return current
|
|
|
|
}
|
2018-04-06 13:10:21 -05:00
|
|
|
|
|
|
|
// DescendentForInstance is like Descendent except that it accepts a path
|
|
|
|
// to a particular module instance in the dynamic module graph, returning
|
|
|
|
// the node from the static module graph that corresponds to it.
|
|
|
|
//
|
|
|
|
// All instances created by a particular module call share the same
|
|
|
|
// configuration, so the keys within the given path are disregarded.
|
|
|
|
func (c *Config) DescendentForInstance(path addrs.ModuleInstance) *Config {
|
|
|
|
current := c
|
|
|
|
for _, step := range path {
|
|
|
|
current = current.Children[step.Name]
|
|
|
|
if current == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return current
|
|
|
|
}
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-21 19:39:27 -05:00
|
|
|
|
|
|
|
// ProviderTypes returns the names of each distinct provider type referenced
|
|
|
|
// in the receiving configuration.
|
|
|
|
//
|
|
|
|
// This is a helper for easily determining which provider types are required
|
|
|
|
// to fully interpret the configuration, though it does not include version
|
|
|
|
// information and so callers are expected to have already dealt with
|
|
|
|
// provider version selection in an earlier step and have identified suitable
|
|
|
|
// versions for each provider.
|
|
|
|
func (c *Config) ProviderTypes() []string {
|
|
|
|
m := make(map[string]struct{})
|
|
|
|
c.gatherProviderTypes(m)
|
|
|
|
|
|
|
|
ret := make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
ret = append(ret, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
func (c *Config) gatherProviderTypes(m map[string]struct{}) {
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pc := range c.Module.ProviderConfigs {
|
|
|
|
m[pc.Name] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, rc := range c.Module.ManagedResources {
|
|
|
|
providerAddr := rc.ProviderConfigAddr()
|
2019-12-06 07:00:18 -06:00
|
|
|
m[providerAddr.Type.LegacyString()] = struct{}{}
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-21 19:39:27 -05:00
|
|
|
}
|
|
|
|
for _, rc := range c.Module.DataResources {
|
|
|
|
providerAddr := rc.ProviderConfigAddr()
|
2019-12-06 07:00:18 -06:00
|
|
|
m[providerAddr.Type.LegacyString()] = struct{}{}
|
various: helpers for collecting necessary provider types
Since schemas are required to interpret provider, resource, and
provisioner attributes in configs, states, and plans, these helpers intend
to make it easier to gather up the the necessary provider types in order
to preload all of the needed schemas before beginning further processing.
Config.ProviderTypes returns directly the list of provider types, since
at this level further detail is not useful: we've not yet run the
provider allocation algorithm, and so the only thing we can reliably
extract here is provider types themselves.
State.ProviderAddrs and Plan.ProviderAddrs each return a list of
absolute provider addresses, which can then be turned into a list of
provider types using the new helper providers.AddressedTypesAbs.
Since we're already using configs.Config throughout core, this also
updates the terraform.LoadSchemas helper to use Config.ProviderTypes
to find the necessary providers, rather than implementing its own
discovery logic. states.State is not yet plumbed in, so we cannot yet
use State.ProviderAddrs to deal with the state but there's a TODO comment
to remind us to update that in a later commit when we swap out
terraform.State for states.State.
A later commit will probably refactor this further so that we can easily
obtain schema for the providers needed to interpret a plan too, but that
is deferred here because further work is required to make core work with
the new plan types first. At that point, terraform.LoadSchemas may become
providers.LoadSchemas with a different interface that just accepts lists
of provider and provisioner names that have been gathered by the caller
using these new helpers.
2018-06-21 19:39:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Must also visit our child modules, recursively.
|
|
|
|
for _, cc := range c.Children {
|
|
|
|
cc.gatherProviderTypes(m)
|
|
|
|
}
|
|
|
|
}
|