mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Cloud migration: ignore backend version check when empty worksapces
This commit is contained in:
parent
f5366468b4
commit
fc5863844c
@ -548,10 +548,12 @@ func (b *Cloud) StateMgr(name string) (statemgr.Full, error) {
|
|||||||
// object to do a nicely formatted message, so we're just assuming the
|
// object to do a nicely formatted message, so we're just assuming the
|
||||||
// issue was that the version wasn't available since that's probably what
|
// issue was that the version wasn't available since that's probably what
|
||||||
// happened.
|
// happened.
|
||||||
|
if b.CLI != nil {
|
||||||
versionUnavailable := fmt.Sprintf(unavailableTerraformVersion, tfversion.String(), workspace.TerraformVersion)
|
versionUnavailable := fmt.Sprintf(unavailableTerraformVersion, tfversion.String(), workspace.TerraformVersion)
|
||||||
b.CLI.Output(b.Colorize().Color(versionUnavailable))
|
b.CLI.Output(b.Colorize().Color(versionUnavailable))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This is a fallback error check. Most code paths should use other
|
// This is a fallback error check. Most code paths should use other
|
||||||
// mechanisms to check the version, then set the ignoreVersionConflict
|
// mechanisms to check the version, then set the ignoreVersionConflict
|
||||||
|
@ -47,6 +47,13 @@ func createOrganization(t *testing.T) (*tfe.Organization, func()) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = tfeClient.Admin.Organizations.Update(ctx, org.Name, tfe.AdminOrganizationUpdateOptions{
|
||||||
|
AccessBetaTools: tfe.Bool(true),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
return org, func() {
|
return org, func() {
|
||||||
if err := tfeClient.Organizations.Delete(ctx, org.Name); err != nil {
|
if err := tfeClient.Organizations.Delete(ctx, org.Name); err != nil {
|
||||||
t.Errorf("Error destroying organization! WARNING: Dangling resources\n"+
|
t.Errorf("Error destroying organization! WARNING: Dangling resources\n"+
|
||||||
|
@ -5,6 +5,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
@ -16,7 +17,7 @@ import (
|
|||||||
|
|
||||||
func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
|
func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tfVersion := "1.1.0-tfc-integration"
|
tfVersion := "1.1.0-alpha-20211027-dev-e51508be"
|
||||||
if !hasTerraformVersion(t, tfVersion) {
|
if !hasTerraformVersion(t, tfVersion) {
|
||||||
t.Skip("Skipping test because TFC does not have current terraform version.")
|
t.Skip("Skipping test because TFC does not have current terraform version.")
|
||||||
}
|
}
|
||||||
@ -162,6 +163,75 @@ func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"migrating from name to tags without ignore-version flag": {
|
||||||
|
setup: func(t *testing.T) (string, func()) {
|
||||||
|
organization, cleanup := createOrganization(t)
|
||||||
|
return organization.Name, cleanup
|
||||||
|
},
|
||||||
|
operations: []operationSets{
|
||||||
|
{
|
||||||
|
prep: func(t *testing.T, orgName, dir string) {
|
||||||
|
wsName := "prod"
|
||||||
|
_ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{
|
||||||
|
Name: tfe.String("prod"),
|
||||||
|
TerraformVersion: tfe.String(tfVersion),
|
||||||
|
})
|
||||||
|
tfBlock := terraformConfigCloudBackendName(orgName, wsName)
|
||||||
|
writeMainTF(t, tfBlock, dir)
|
||||||
|
},
|
||||||
|
commands: []tfCommand{
|
||||||
|
{
|
||||||
|
command: []string{"init"},
|
||||||
|
expectedCmdOutput: `Terraform Cloud has been successfully initialized!`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
command: []string{"apply"},
|
||||||
|
expectedCmdOutput: `Do you want to perform these actions in workspace "prod"?`,
|
||||||
|
userInput: []string{"yes"},
|
||||||
|
postInputOutput: []string{`Apply complete!`},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
prep: func(t *testing.T, orgName, dir string) {
|
||||||
|
tag := "app"
|
||||||
|
// This is only here to ensure that the updated terraform version is
|
||||||
|
// present in the workspace, and it does not default to a lower
|
||||||
|
// version that does not support `cloud`.
|
||||||
|
_ = createWorkspace(t, orgName, tfe.WorkspaceCreateOptions{
|
||||||
|
Name: tfe.String("new-workspace"),
|
||||||
|
TerraformVersion: tfe.String(tfVersion),
|
||||||
|
})
|
||||||
|
tfBlock := terraformConfigCloudBackendTags(orgName, tag)
|
||||||
|
writeMainTF(t, tfBlock, dir)
|
||||||
|
},
|
||||||
|
commands: []tfCommand{
|
||||||
|
{
|
||||||
|
command: []string{"init", "-migrate-state"},
|
||||||
|
expectedCmdOutput: `The Terraform Cloud configuration only allows named workspaces!`,
|
||||||
|
expectError: true,
|
||||||
|
userInput: []string{"new-workspace", "yes"},
|
||||||
|
postInputOutput: []string{
|
||||||
|
// this is a temporary measure till we resolve some of the
|
||||||
|
// version mismatching.
|
||||||
|
fmt.Sprintf(`Remote workspace Terraform version "%s" does not match local Terraform version`, tfVersion)},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
validations: func(t *testing.T, orgName string) {
|
||||||
|
wsList, err := tfeClient.Workspaces.List(ctx, orgName, tfe.WorkspaceListOptions{
|
||||||
|
Tags: tfe.String("app"),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// The migration never occured, so we have no workspaces with this tag.
|
||||||
|
if len(wsList.Items) != 0 {
|
||||||
|
t.Fatalf("Expected number of workspaces to be 0, but got %d", len(wsList.Items))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, tc := range cases {
|
for name, tc := range cases {
|
||||||
@ -225,7 +295,7 @@ func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = cmd.Wait()
|
err = cmd.Wait()
|
||||||
if err != nil {
|
if err != nil && !tfCmd.expectError {
|
||||||
t.Fatal(err.Error())
|
t.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,7 +309,7 @@ func Test_migrate_tfc_to_tfc_single_workspace(t *testing.T) {
|
|||||||
|
|
||||||
func Test_migrate_tfc_to_tfc_multiple_workspace(t *testing.T) {
|
func Test_migrate_tfc_to_tfc_multiple_workspace(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
tfVersion := "1.1.0-tfc-integration"
|
tfVersion := "1.1.0-alpha-20211027-dev-e51508be"
|
||||||
if !hasTerraformVersion(t, tfVersion) {
|
if !hasTerraformVersion(t, tfVersion) {
|
||||||
t.Skip("Skipping test because TFC does not have current terraform version.")
|
t.Skip("Skipping test because TFC does not have current terraform version.")
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,9 @@ func (m *Meta) backendMigrateState(opts *backendMigrateOpts) error {
|
|||||||
}
|
}
|
||||||
// If there are no specified destination workspaces, perform a remote
|
// If there are no specified destination workspaces, perform a remote
|
||||||
// backend version check with the default workspace.
|
// backend version check with the default workspace.
|
||||||
if len(destinationWorkspaces) == 0 {
|
// Ensure that we are not dealing with Terraform Cloud migrations, as it
|
||||||
|
// does not support the default name.
|
||||||
|
if len(destinationWorkspaces) == 0 && !destinationTFC {
|
||||||
diags := m.remoteVersionCheck(opts.Destination, backend.DefaultStateName)
|
diags := m.remoteVersionCheck(opts.Destination, backend.DefaultStateName)
|
||||||
if diags.HasErrors() {
|
if diags.HasErrors() {
|
||||||
return diags.Err()
|
return diags.Err()
|
||||||
|
Loading…
Reference in New Issue
Block a user