2019-01-09 00:23:09 +01:00
|
|
|
package earlyconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
version "github.com/hashicorp/go-version"
|
|
|
|
"github.com/hashicorp/terraform-config-inspect/tfconfig"
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2020-03-26 20:04:48 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/getproviders"
|
2021-05-17 20:01:54 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/moduledeps"
|
2021-05-17 21:35:22 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/plugin/discovery"
|
2021-05-17 19:11:06 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2019-01-09 00:23:09 +01: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.
|
|
|
|
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
|
|
|
|
|
|
|
|
// Path is a sequence of module logical names that traverse from the root
|
|
|
|
// module to this config. Path is empty for the root module.
|
|
|
|
//
|
|
|
|
// 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 22:05:41 +01:00
|
|
|
// have been resolved. In most cases, an addrs.ModuleInstance describing
|
2019-01-09 00:23:09 +01: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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Module points to the object describing the configuration for the
|
|
|
|
// various elements (variables, resources, etc) defined by this module.
|
|
|
|
Module *tfconfig.Module
|
|
|
|
|
|
|
|
// CallPos is the source position for the header of the module block that
|
|
|
|
// requested this module.
|
|
|
|
//
|
|
|
|
// This field is meaningless for the root module, where its contents are undefined.
|
|
|
|
CallPos tfconfig.SourcePos
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// Version is the specific version that was selected for this module,
|
|
|
|
// based on version constraints given in configuration.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
Version *version.Version
|
|
|
|
}
|
|
|
|
|
2020-03-26 20:04:48 +01:00
|
|
|
// ProviderRequirements searches the full tree of modules under the receiver
|
|
|
|
// for both explicit and implicit dependencies on providers.
|
|
|
|
//
|
|
|
|
// The result is a full manifest of all of the providers that must be available
|
|
|
|
// in order to work with the receiving configuration.
|
|
|
|
//
|
|
|
|
// If the returned diagnostics includes errors then the resulting Requirements
|
|
|
|
// may be incomplete.
|
|
|
|
func (c *Config) ProviderRequirements() (getproviders.Requirements, tfdiags.Diagnostics) {
|
|
|
|
reqs := make(getproviders.Requirements)
|
|
|
|
diags := c.addProviderRequirements(reqs)
|
|
|
|
return reqs, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// addProviderRequirements is the main part of the ProviderRequirements
|
|
|
|
// implementation, gradually mutating a shared requirements object to
|
|
|
|
// eventually return.
|
|
|
|
func (c *Config) addProviderRequirements(reqs getproviders.Requirements) tfdiags.Diagnostics {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
// First we'll deal with the requirements directly in _our_ module...
|
|
|
|
for localName, providerReqs := range c.Module.RequiredProviders {
|
|
|
|
var fqn addrs.Provider
|
|
|
|
if source := providerReqs.Source; source != "" {
|
|
|
|
addr, moreDiags := addrs.ParseProviderSourceString(source)
|
|
|
|
if moreDiags.HasErrors() {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid provider source address",
|
|
|
|
fmt.Sprintf("Invalid source %q for provider %q in %s", source, localName, c.Path),
|
|
|
|
))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fqn = addr
|
|
|
|
}
|
|
|
|
if fqn.IsZero() {
|
2020-04-02 01:11:15 +02:00
|
|
|
fqn = addrs.ImpliedProviderForUnqualifiedType(localName)
|
2020-03-26 20:04:48 +01:00
|
|
|
}
|
|
|
|
if _, ok := reqs[fqn]; !ok {
|
|
|
|
// We'll at least have an unconstrained dependency then, but might
|
|
|
|
// add to this in the loop below.
|
|
|
|
reqs[fqn] = nil
|
|
|
|
}
|
|
|
|
for _, constraintsStr := range providerReqs.VersionConstraints {
|
|
|
|
if constraintsStr != "" {
|
|
|
|
constraints, err := getproviders.ParseVersionConstraints(constraintsStr)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(tfdiags.Sourceless(
|
|
|
|
tfdiags.Error,
|
|
|
|
"Invalid provider version constraint",
|
|
|
|
fmt.Sprintf("Provider %q in %s has invalid version constraint %q: %s.", localName, c.Path, constraintsStr, err),
|
|
|
|
))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
reqs[fqn] = append(reqs[fqn], constraints...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ...and now we'll recursively visit all of the child modules to merge
|
|
|
|
// in their requirements too.
|
|
|
|
for _, childConfig := range c.Children {
|
|
|
|
moreDiags := childConfig.addProviderRequirements(reqs)
|
|
|
|
diags = diags.Append(moreDiags)
|
|
|
|
}
|
|
|
|
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProviderDependencies is a deprecated variant of ProviderRequirements which
|
|
|
|
// uses the moduledeps models for representation. This is preserved to allow
|
|
|
|
// a gradual transition over to ProviderRequirements, but note that its
|
|
|
|
// support for fully-qualified provider addresses has some idiosyncracies.
|
2019-01-09 00:23:09 +01:00
|
|
|
func (c *Config) ProviderDependencies() (*moduledeps.Module, tfdiags.Diagnostics) {
|
|
|
|
var diags tfdiags.Diagnostics
|
|
|
|
|
|
|
|
var name string
|
|
|
|
if len(c.Path) > 0 {
|
|
|
|
name = c.Path[len(c.Path)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &moduledeps.Module{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
providers := make(moduledeps.Providers)
|
|
|
|
for name, reqs := range c.Module.RequiredProviders {
|
2020-02-15 03:10:03 +01:00
|
|
|
var fqn addrs.Provider
|
|
|
|
if source := reqs.Source; source != "" {
|
2020-12-01 22:27:29 +01:00
|
|
|
addr, parseDiags := addrs.ParseProviderSourceString(source)
|
|
|
|
if parseDiags.HasErrors() {
|
2020-02-15 03:10:03 +01:00
|
|
|
diags = diags.Append(wrapDiagnostic(tfconfig.Diagnostic{
|
|
|
|
Severity: tfconfig.DiagError,
|
|
|
|
Summary: "Invalid provider source",
|
|
|
|
Detail: fmt.Sprintf("Invalid source %q for provider", name),
|
|
|
|
}))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fqn = addr
|
|
|
|
}
|
|
|
|
if fqn.IsZero() {
|
2020-10-05 14:33:49 +02:00
|
|
|
fqn = addrs.NewDefaultProvider(name)
|
2020-02-05 15:27:32 +01:00
|
|
|
}
|
2019-01-09 00:23:09 +01:00
|
|
|
var constraints version.Constraints
|
2020-01-10 17:54:53 +01:00
|
|
|
for _, reqStr := range reqs.VersionConstraints {
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
if reqStr != "" {
|
2019-01-09 00:23:09 +01:00
|
|
|
constraint, err := version.NewConstraint(reqStr)
|
|
|
|
if err != nil {
|
|
|
|
diags = diags.Append(wrapDiagnostic(tfconfig.Diagnostic{
|
|
|
|
Severity: tfconfig.DiagError,
|
|
|
|
Summary: "Invalid provider version constraint",
|
2020-10-05 14:33:49 +02:00
|
|
|
Detail: fmt.Sprintf("Invalid version constraint %q for provider %s.", reqStr, fqn.String()),
|
2019-01-09 00:23:09 +01:00
|
|
|
}))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
constraints = append(constraints, constraint...)
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 15:27:32 +01:00
|
|
|
providers[fqn] = moduledeps.ProviderDependency{
|
2019-01-09 00:23:09 +01:00
|
|
|
Constraints: discovery.NewConstraints(constraints),
|
|
|
|
Reason: moduledeps.ProviderDependencyExplicit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.Providers = providers
|
|
|
|
|
|
|
|
childNames := make([]string, 0, len(c.Children))
|
|
|
|
for name := range c.Children {
|
|
|
|
childNames = append(childNames, name)
|
|
|
|
}
|
|
|
|
sort.Strings(childNames)
|
|
|
|
|
|
|
|
for _, name := range childNames {
|
|
|
|
child, childDiags := c.Children[name].ProviderDependencies()
|
|
|
|
ret.Children = append(ret.Children, child)
|
|
|
|
diags = diags.Append(childDiags)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, diags
|
|
|
|
}
|