mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Adapt user-facing usages of terraform in internal/depsfile
This commit is contained in:
parent
9ee1c7d178
commit
a23408c2b8
@ -1,7 +1,7 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
// Package depsfile contains the logic for reading and writing Terraform's
|
||||
// Package depsfile contains the logic for reading and writing OpenTF's
|
||||
// dependency lock and development override configuration files.
|
||||
//
|
||||
// These files are separate from the main Terraform configuration files (.tf)
|
||||
@ -9,12 +9,12 @@
|
||||
// where .tf files configure a particular module while these configure
|
||||
// a whole configuration tree. Another, more practical consideration is that
|
||||
// we intend both of these files to be primarily maintained automatically by
|
||||
// Terraform itself, rather than by human-originated edits, and so keeping
|
||||
// them separate means that it's easier to distinguish the files that Terraform
|
||||
// OpenTF itself, rather than by human-originated edits, and so keeping
|
||||
// them separate means that it's easier to distinguish the files that OpenTF
|
||||
// will change automatically during normal workflow from the files that
|
||||
// Terraform only edits on direct request.
|
||||
// OpenTF only edits on direct request.
|
||||
//
|
||||
// Both files use HCL syntax, for consistency with other files in Terraform
|
||||
// Both files use HCL syntax, for consistency with other files in OpenTF
|
||||
// that we expect humans to (in this case, only occasionally) edit directly.
|
||||
// A dependency lock file tracks the most recently selected upstream versions
|
||||
// of each dependency, and is intended for checkin to version control.
|
||||
|
@ -23,13 +23,13 @@ type Locks struct {
|
||||
|
||||
// overriddenProviders is a subset of providers which we might be tracking
|
||||
// in field providers but whose lock information we're disregarding for
|
||||
// this particular run due to some feature that forces Terraform to not
|
||||
// this particular run due to some feature that forces OpenTF to not
|
||||
// use a normally-installed plugin for it. For example, the "provider dev
|
||||
// overrides" feature means that we'll be using an arbitrary directory on
|
||||
// disk as the package, regardless of what might be selected in "providers".
|
||||
//
|
||||
// overriddenProviders is an in-memory-only annotation, never stored as
|
||||
// part of a lock file and thus not persistent between Terraform runs.
|
||||
// part of a lock file and thus not persistent between OpenTF runs.
|
||||
// The CLI layer is generally the one responsible for populating this,
|
||||
// by calling SetProviderOverridden in response to CLI Configuration
|
||||
// settings, environment variables, or whatever similar sources.
|
||||
@ -118,7 +118,7 @@ func (l *Locks) RemoveProvider(addr addrs.Provider) {
|
||||
delete(l.providers, addr)
|
||||
}
|
||||
|
||||
// SetProviderOverridden records that this particular Terraform process will
|
||||
// SetProviderOverridden records that this particular OpenTF process will
|
||||
// not pay attention to the recorded lock entry for the given provider, and
|
||||
// will instead access that provider's functionality in some other special
|
||||
// way that isn't sensitive to provider version selections or checksums.
|
||||
@ -333,7 +333,7 @@ type ProviderLock struct {
|
||||
// version is the specific version that was previously selected, while
|
||||
// versionConstraints is the constraint that was used to make that
|
||||
// selection, which we can potentially use to hint to run
|
||||
// e.g. terraform init -upgrade if a user has changed a version
|
||||
// e.g. opentf init -upgrade if a user has changed a version
|
||||
// constraint but the previous selection still remains valid.
|
||||
// "version" is therefore authoritative, while "versionConstraints" is
|
||||
// just for a UI hint and not used to make any real decisions.
|
||||
@ -384,9 +384,9 @@ func (l *ProviderLock) Version() getproviders.Version {
|
||||
// being used to choose the version returned by Version.
|
||||
//
|
||||
// These version constraints are not authoritative for future selections and
|
||||
// are included only so Terraform can detect if the constraints in
|
||||
// are included only so OpenTF can detect if the constraints in
|
||||
// configuration have changed since a selection was made, and thus hint to the
|
||||
// user that they may need to run terraform init -upgrade to apply the new
|
||||
// user that they may need to run opentf init -upgrade to apply the new
|
||||
// constraints.
|
||||
func (l *ProviderLock) VersionConstraints() getproviders.VersionConstraints {
|
||||
return l.versionConstraints
|
||||
@ -398,7 +398,7 @@ func (l *ProviderLock) VersionConstraints() getproviders.VersionConstraints {
|
||||
//
|
||||
// If your intent is to verify a package against the recorded hashes, use
|
||||
// PreferredHashes to get only the hashes which the current version
|
||||
// of Terraform considers the strongest of the available hashing schemes, one
|
||||
// of OpenTF considers the strongest of the available hashing schemes, one
|
||||
// of which must match in order for verification to be considered successful.
|
||||
//
|
||||
// Do not modify the backing array of the returned slice.
|
||||
|
@ -51,7 +51,7 @@ func LoadLocksFromFile(filename string) (*Locks, tfdiags.Diagnostics) {
|
||||
// a plan file, in which case the given filename will typically be a
|
||||
// placeholder that will only be seen in the unusual case that the plan file
|
||||
// contains an invalid lock file, which should only be possible if the user
|
||||
// edited it directly (Terraform bugs notwithstanding).
|
||||
// edited it directly (OpenTF bugs notwithstanding).
|
||||
func LoadLocksFromBytes(src []byte, filename string) (*Locks, tfdiags.Diagnostics) {
|
||||
return loadLocks(func(parser *hclparse.Parser) (*hcl.File, hcl.Diagnostics) {
|
||||
return parser.ParseHCL(src, filename)
|
||||
@ -115,7 +115,7 @@ func SaveLocksToBytes(locks *Locks) ([]byte, tfdiags.Diagnostics) {
|
||||
// In other uses of the "hclwrite" package we typically try to make
|
||||
// surgical updates to the author's existing files, preserving their
|
||||
// block ordering, comments, etc. We intentionally don't do that here
|
||||
// to reinforce the fact that this file primarily belongs to Terraform,
|
||||
// to reinforce the fact that this file primarily belongs to OpenTF,
|
||||
// and to help ensure that VCS diffs of the file primarily reflect
|
||||
// changes that actually affect functionality rather than just cosmetic
|
||||
// changes, by maintaining it in a highly-normalized form.
|
||||
@ -125,12 +125,12 @@ func SaveLocksToBytes(locks *Locks) ([]byte, tfdiags.Diagnostics) {
|
||||
|
||||
// End-users _may_ edit the lock file in exceptional situations, like
|
||||
// working around potential dependency selection bugs, but we intend it
|
||||
// to be primarily maintained automatically by the "terraform init"
|
||||
// to be primarily maintained automatically by the "opentf init"
|
||||
// command.
|
||||
rootBody.AppendUnstructuredTokens(hclwrite.Tokens{
|
||||
{
|
||||
Type: hclsyntax.TokenComment,
|
||||
Bytes: []byte("# This file is maintained automatically by \"terraform init\".\n"),
|
||||
Bytes: []byte("# This file is maintained automatically by \"opentf init\".\n"),
|
||||
},
|
||||
{
|
||||
Type: hclsyntax.TokenComment,
|
||||
@ -177,7 +177,7 @@ func decodeLocksFromHCL(locks *Locks, body hcl.Body) tfdiags.Diagnostics {
|
||||
// "module" is just a placeholder for future enhancement, so we
|
||||
// can mostly-ignore the this block type we intend to add in
|
||||
// future, but warn in case someone tries to use one e.g. if they
|
||||
// downgraded to an earlier version of Terraform.
|
||||
// downgraded to an earlier version of OpenTF.
|
||||
{
|
||||
Type: "module",
|
||||
LabelNames: []string{"path"},
|
||||
@ -218,7 +218,7 @@ func decodeLocksFromHCL(locks *Locks, body hcl.Body) tfdiags.Diagnostics {
|
||||
diags = diags.Append(&hcl.Diagnostic{
|
||||
Severity: hcl.DiagWarning,
|
||||
Summary: "Dependency locks for modules are not yet supported",
|
||||
Detail: fmt.Sprintf("Terraform v%s only supports dependency locks for providers, not for modules. This configuration may be intended for a later version of Terraform that also supports dependency locks for modules.", currentVersion),
|
||||
Detail: fmt.Sprintf("OpenTF v%s only supports dependency locks for providers, not for modules. This configuration may be intended for a later version of OpenTF that also supports dependency locks for modules.", currentVersion),
|
||||
Subject: block.TypeRange.Ptr(),
|
||||
})
|
||||
seenModule = true
|
||||
@ -262,7 +262,7 @@ func decodeProviderLockFromHCL(block *hcl.Block) (*ProviderLock, tfdiags.Diagnos
|
||||
diags = diags.Append(&hcl.Diagnostic{
|
||||
Severity: hcl.DiagError,
|
||||
Summary: "Invalid provider source address",
|
||||
Detail: fmt.Sprintf("Cannot lock a version for built-in provider %s. Built-in providers are bundled inside Terraform itself, so you can't select a version for them independently of the Terraform release you are currently running.", addr),
|
||||
Detail: fmt.Sprintf("Cannot lock a version for built-in provider %s. Built-in providers are bundled inside OpenTF itself, so you can't select a version for them independently of the OpenTF release you are currently running.", addr),
|
||||
Subject: block.LabelRanges[0].Ptr(),
|
||||
})
|
||||
return nil, diags
|
||||
|
@ -164,7 +164,7 @@ func TestLoadLocksFromFile(t *testing.T) {
|
||||
|
||||
func TestLoadLocksFromFileAbsent(t *testing.T) {
|
||||
t.Run("lock file is a directory", func(t *testing.T) {
|
||||
// This can never happen when Terraform is the one generating the
|
||||
// This can never happen when OpenTF is the one generating the
|
||||
// lock file, but might arise if the user makes a directory with the
|
||||
// lock file's name for some reason. (There is no actual reason to do
|
||||
// so, so that would always be a mistake.)
|
||||
@ -244,7 +244,7 @@ func TestSaveLocksToFile(t *testing.T) {
|
||||
t.Fatalf(err.Error())
|
||||
}
|
||||
gotContent := string(gotContentBytes)
|
||||
wantContent := `# This file is maintained automatically by "terraform init".
|
||||
wantContent := `# This file is maintained automatically by "opentf init".
|
||||
# Manual edits may be lost in future updates.
|
||||
|
||||
provider "registry.terraform.io/test/bar" {
|
||||
|
@ -4,17 +4,17 @@
|
||||
package depsfile
|
||||
|
||||
// LockFilePath is the path, relative to a configuration's root module
|
||||
// directory, where Terraform expects to find the dependency lock file for
|
||||
// directory, where OpenTF expects to find the dependency lock file for
|
||||
// that configuration.
|
||||
//
|
||||
// This file is intended to be kept in version control, so it lives directly
|
||||
// in the root module directory. The ".terraform" prefix is intended to
|
||||
// suggest that it's metadata about several types of objects that ultimately
|
||||
// end up in the .terraform directory after running "terraform init".
|
||||
// end up in the .terraform directory after running "opentf init".
|
||||
const LockFilePath = ".terraform.lock.hcl"
|
||||
|
||||
// DevOverrideFilePath is the path, relative to a configuration's root module
|
||||
// directory, where Terraform will look to find a possible override file that
|
||||
// directory, where OpenTF will look to find a possible override file that
|
||||
// represents a request to temporarily (within a single working directory only)
|
||||
// use specific local directories in place of packages that would normally
|
||||
// need to be installed from a remote location.
|
||||
|
@ -39,6 +39,6 @@ provider "registry.terraform.io/-/null" { # ERROR: Invalid provider source addre
|
||||
}
|
||||
|
||||
# Built-in providers are not allowed, because they are not versioned
|
||||
# independently of the Terraform CLI release they are embedded in.
|
||||
# independently of the OpenTF CLI release they are embedded in.
|
||||
provider "terraform.io/builtin/foo" { # ERROR: Invalid provider source address
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user