2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-02-09 17:32:49 -06:00
|
|
|
package configload
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
"path/filepath"
|
2018-02-09 17:32:49 -06:00
|
|
|
|
2024-07-29 15:15:51 -05:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2019-10-11 04:34:26 -05:00
|
|
|
"github.com/hashicorp/terraform-svchost/disco"
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
|
|
|
"github.com/opentofu/opentofu/internal/registry"
|
2018-02-09 17:32:49 -06:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Loader instance is the main entry-point for loading configurations via
|
|
|
|
// this package.
|
|
|
|
//
|
|
|
|
// It extends the general config-loading functionality in the parent package
|
|
|
|
// "configs" to support installation of modules from remote sources and
|
|
|
|
// loading full configurations using modules that were previously installed.
|
|
|
|
type Loader struct {
|
|
|
|
// parser is used to read configuration
|
|
|
|
parser *configs.Parser
|
|
|
|
|
|
|
|
// modules is used to install and locate descendent modules that are
|
|
|
|
// referenced (directly or indirectly) from the root module.
|
|
|
|
modules moduleMgr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is used with NewLoader to specify configuration arguments for the
|
|
|
|
// loader.
|
|
|
|
type Config struct {
|
|
|
|
// ModulesDir is a path to a directory where descendent modules are
|
|
|
|
// (or should be) installed. (This is usually the
|
|
|
|
// .terraform/modules directory, in the common case where this package
|
2023-09-26 12:09:27 -05:00
|
|
|
// is being loaded from the main OpenTofu CLI package.)
|
2018-02-09 17:32:49 -06:00
|
|
|
ModulesDir string
|
|
|
|
|
|
|
|
// Services is the service discovery client to use when locating remote
|
|
|
|
// module registry endpoints. If this is nil then registry sources are
|
|
|
|
// not supported, which should be true only in specialized circumstances
|
|
|
|
// such as in tests.
|
|
|
|
Services *disco.Disco
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLoader creates and returns a loader that reads configuration from the
|
|
|
|
// real OS filesystem.
|
|
|
|
//
|
|
|
|
// The loader has some internal state about the modules that are currently
|
|
|
|
// installed, which is read from disk as part of this function. If that
|
|
|
|
// manifest cannot be read then an error will be returned.
|
|
|
|
func NewLoader(config *Config) (*Loader, error) {
|
|
|
|
fs := afero.NewOsFs()
|
|
|
|
parser := configs.NewParser(fs)
|
2018-07-05 14:28:29 -05:00
|
|
|
reg := registry.NewClient(config.Services, nil)
|
2018-02-09 17:32:49 -06:00
|
|
|
|
|
|
|
ret := &Loader{
|
|
|
|
parser: parser,
|
|
|
|
modules: moduleMgr{
|
2018-02-14 16:35:03 -06:00
|
|
|
FS: afero.Afero{Fs: fs},
|
|
|
|
CanInstall: true,
|
|
|
|
Dir: config.ModulesDir,
|
|
|
|
Services: config.Services,
|
|
|
|
Registry: reg,
|
2018-02-09 17:32:49 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ret.modules.readModuleManifestSnapshot()
|
|
|
|
if err != nil {
|
2023-09-18 07:53:49 -05:00
|
|
|
return nil, fmt.Errorf("failed to read module manifest: %w", err)
|
2018-02-09 17:32:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2019-01-08 20:39:14 -06:00
|
|
|
// ModulesDir returns the path to the directory where the loader will look for
|
|
|
|
// the local cache of remote module packages.
|
|
|
|
func (l *Loader) ModulesDir() string {
|
|
|
|
return l.modules.Dir
|
|
|
|
}
|
|
|
|
|
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 13:11:00 -06:00
|
|
|
// RefreshModules updates the in-memory cache of the module manifest from the
|
|
|
|
// module manifest file on disk. This is not necessary in normal use because
|
|
|
|
// module installation and configuration loading are separate steps, but it
|
|
|
|
// can be useful in tests where module installation is done as a part of
|
|
|
|
// configuration loading by a helper function.
|
|
|
|
//
|
|
|
|
// Call this function after any module installation where an existing loader
|
|
|
|
// is already alive and may be used again later.
|
|
|
|
//
|
|
|
|
// An error is returned if the manifest file cannot be read.
|
|
|
|
func (l *Loader) RefreshModules() error {
|
|
|
|
if l == nil {
|
|
|
|
// Nothing to do, then.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return l.modules.readModuleManifestSnapshot()
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:32:49 -06:00
|
|
|
// Parser returns the underlying parser for this loader.
|
|
|
|
//
|
|
|
|
// This is useful for loading other sorts of files than the module directories
|
|
|
|
// that a loader deals with, since then they will share the source code cache
|
|
|
|
// for this loader and can thus be shown as snippets in diagnostic messages.
|
|
|
|
func (l *Loader) Parser() *configs.Parser {
|
|
|
|
return l.parser
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sources returns the source code cache for the underlying parser of this
|
|
|
|
// loader. This is a shorthand for l.Parser().Sources().
|
2024-07-29 15:15:51 -05:00
|
|
|
func (l *Loader) Sources() map[string]*hcl.File {
|
2018-02-09 17:32:49 -06:00
|
|
|
return l.parser.Sources()
|
|
|
|
}
|
2018-03-20 20:45:21 -05:00
|
|
|
|
|
|
|
// IsConfigDir returns true if and only if the given directory contains at
|
2023-09-26 12:09:27 -05:00
|
|
|
// least one OpenTofu configuration file. This is a wrapper around calling
|
2018-03-20 20:45:21 -05:00
|
|
|
// the same method name on the loader's parser.
|
|
|
|
func (l *Loader) IsConfigDir(path string) bool {
|
|
|
|
return l.parser.IsConfigDir(path)
|
|
|
|
}
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
|
2022-08-17 13:46:02 -05:00
|
|
|
// ImportSources writes into the receiver's source code map the given source
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
// code buffers.
|
|
|
|
//
|
|
|
|
// This is useful in the situation where an ancillary loader is created for
|
|
|
|
// some reason (e.g. loading config from a plan file) but the cached source
|
|
|
|
// code from that loader must be imported into the "main" loader in order
|
|
|
|
// to return source code snapshots in diagnostic messages.
|
|
|
|
//
|
2022-08-17 13:46:02 -05:00
|
|
|
// loader.ImportSources(otherLoader.Sources())
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 16:24:45 -05:00
|
|
|
func (l *Loader) ImportSources(sources map[string][]byte) {
|
|
|
|
p := l.Parser()
|
|
|
|
for name, src := range sources {
|
|
|
|
p.ForceFileSource(name, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportSourcesFromSnapshot writes into the receiver's source code the
|
|
|
|
// source files from the given snapshot.
|
|
|
|
//
|
|
|
|
// This is similar to ImportSources but knows how to unpack and flatten a
|
|
|
|
// snapshot data structure to get the corresponding flat source file map.
|
|
|
|
func (l *Loader) ImportSourcesFromSnapshot(snap *Snapshot) {
|
|
|
|
p := l.Parser()
|
|
|
|
for _, m := range snap.Modules {
|
|
|
|
baseDir := m.Dir
|
|
|
|
for fn, src := range m.Files {
|
|
|
|
fullPath := filepath.Join(baseDir, fn)
|
|
|
|
p.ForceFileSource(fullPath, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Experiments supported only in alpha/dev builds
We originally introduced the idea of language experiments as a way to get
early feedback on not-yet-proven feature ideas, ideally as part of the
initial exploration of the solution space rather than only after a
solution has become relatively clear.
Unfortunately, our tradeoff of making them available in normal releases
behind an explicit opt-in in order to make it easier to participate in the
feedback process had the unintended side-effect of making it feel okay
to use experiments in production and endure the warnings they generate.
This in turn has made us reluctant to make use of the experiments feature
lest experiments become de-facto production features which we then feel
compelled to preserve even though we aren't yet ready to graduate them
to stable features.
In an attempt to tweak that compromise, here we make the availability of
experiments _at all_ a build-time flag which will not be set by default,
and therefore experiments will not be available in most release builds.
The intent (not yet implemented in this PR) is for our release process to
set this flag only when it knows it's building an alpha release or a
development snapshot not destined for release at all, which will therefore
allow us to still use the alpha releases as a vehicle for giving feedback
participants access to a feature (without needing to install a Go
toolchain) but will not encourage pretending that these features are
production-ready before they graduate from experimental.
Only language experiments have an explicit framework for dealing with them
which outlives any particular experiment, so most of the changes here are
to that generalized mechanism. However, the intent is that non-language
experiments, such as experimental CLI commands, would also in future
check Meta.AllowExperimentalFeatures and gate the use of those experiments
too, so that we can be consistent that experimental features will never
be available unless you explicitly choose to use an alpha release or
a custom build from source code.
Since there are already some experiments active at the time of this commit
which were not previously subject to this restriction, we'll pragmatically
leave those as exceptions that will remain generally available for now,
and so this new approach will apply only to new experiments started in the
future. Once those experiments have all concluded, we will be left with
no more exceptions unless we explicitly choose to make an exception for
some reason we've not imagined yet.
It's important that we be able to write tests that rely on experiments
either being available or not being available, so here we're using our
typical approach of making "package main" deal with the global setting
that applies to Terraform CLI executables while making the layers below
all support fine-grain selection of this behavior so that tests with
different needs can run concurrently without trampling on one another.
As a compromise, the integration tests in the terraform package will
run with experiments enabled _by default_ since we commonly need to
exercise experiments in those tests, but they can selectively opt-out
if they need to by overriding the loader setting back to false again.
2022-04-27 13:14:51 -05:00
|
|
|
|
|
|
|
// AllowLanguageExperiments specifies whether subsequent LoadConfig (and
|
|
|
|
// similar) calls will allow opting in to experimental language features.
|
|
|
|
//
|
|
|
|
// If this method is never called for a particular loader, the default behavior
|
|
|
|
// is to disallow language experiments.
|
|
|
|
//
|
|
|
|
// Main code should set this only for alpha or development builds. Test code
|
|
|
|
// is responsible for deciding for itself whether and how to call this
|
|
|
|
// method.
|
|
|
|
func (l *Loader) AllowLanguageExperiments(allowed bool) {
|
|
|
|
l.parser.AllowLanguageExperiments(allowed)
|
|
|
|
}
|