Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of bugfix: terraform providers mirror command should honor terraform lock file into v1.4 #32749

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion internal/command/e2etest/providers_mirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
// compromise for now to keep these tests relatively simple.

func TestTerraformProvidersMirror(t *testing.T) {
testTerraformProvidersMirror(t, "terraform-providers-mirror")
}

func TestTerraformProvidersMirrorWithLockFile(t *testing.T) {
testTerraformProvidersMirror(t, "terraform-providers-mirror-with-lock-file")
}

func testTerraformProvidersMirror(t *testing.T, fixture string) {
// This test reaches out to releases.hashicorp.com to download the
// template and null providers, so it can only run if network access is
// allowed.
Expand All @@ -25,7 +33,7 @@ func TestTerraformProvidersMirror(t *testing.T) {
outputDir := t.TempDir()
t.Logf("creating mirror directory in %s", outputDir)

fixturePath := filepath.Join("testdata", "terraform-providers-mirror")
fixturePath := filepath.Join("testdata", fixture)
tf := e2e.NewBinary(t, terraformBin, fixturePath)

stdout, stderr, err := tf.Run("providers", "mirror", "-platform=linux_amd64", "-platform=windows_386", outputDir)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_providers {
template = { source = "hashicorp/template" }
null = { source = "hashicorp/null" }
terraform = { source = "terraform.io/builtin/terraform" }
}
}
20 changes: 19 additions & 1 deletion internal/command/providers_mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,27 @@ func (c *ProvidersMirrorCommand) Run(args []string) int {
reqs, moreDiags := config.ProviderRequirements()
diags = diags.Append(moreDiags)

// Read lock file
lockedDeps, lockedDepsDiags := c.Meta.lockedDependencies()
diags = diags.Append(lockedDepsDiags)

// If we have any error diagnostics already then we won't proceed further.
if diags.HasErrors() {
c.showDiagnostics(diags)
return 1
}

// If lock file is present, validate it against configuration
if !lockedDeps.Empty() {
if errs := config.VerifyDependencySelections(lockedDeps); len(errs) > 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Inconsistent dependency lock file",
fmt.Sprintf("To update the locked dependency selections to match a changed configuration, run:\n terraform init -upgrade\n got:%v", errs),
))
}
}

// Unlike other commands, this command always consults the origin registry
// for every provider so that it can be used to update a local mirror
// directory without needing to first disable that local mirror
Expand Down Expand Up @@ -140,7 +155,10 @@ func (c *ProvidersMirrorCommand) Run(args []string) int {
continue
}
selected := candidates.Newest()
if len(constraintsStr) > 0 {
if !lockedDeps.Empty() {
selected = lockedDeps.Provider(provider).Version()
c.Ui.Output(fmt.Sprintf(" - Selected v%s to match dependency lock file", selected.String()))
} else if len(constraintsStr) > 0 {
c.Ui.Output(fmt.Sprintf(" - Selected v%s to meet constraints %s", selected.String(), constraintsStr))
} else {
c.Ui.Output(fmt.Sprintf(" - Selected v%s with no constraints", selected.String()))
Expand Down