Skip to content

Commit

Permalink
tfmigrate can work with terraform cloud, need to get backend control …
Browse files Browse the repository at this point in the history
…flow working
  • Loading branch information
GoodmanBen committed Mar 4, 2022
1 parent 47bdc5f commit 7541751
Show file tree
Hide file tree
Showing 16 changed files with 26 additions and 23 deletions.
13 changes: 8 additions & 5 deletions tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,17 @@ terraform {
log.Printf("[ERROR] [executor@%s] please remove the local workspace directory(%s) and re-run terraform init -reconfigure\n", c.Dir(), workspacePath)
}
log.Printf("[INFO] [executor@%s] switch back to remote\n", c.Dir())
stdOut, err := c.Init(ctx, "-input=false", "-no-color", "-reconfigure")

// TODO: First just see if removing the -reconfigure statement will work?
// TODO: Need to detect if the configuration uses Terraform cloud as the backend, instead of the
// TODO: hacky fix
stdOut, err := c.Init(ctx, "-input=false", "-no-color") //, "-reconfigure")
// TODO: Debugging this new functionality here
fmt.Println(stdOut)
log.Printf(stdOut)

if (err != nil) && (stdOut != "Initializing Terraform Cloud...") {
if (err != nil) && (stdOut == "\nInitializing Terraform Cloud...") {
_, err = c.Init(ctx, "-input=false", "-no-color")
}
if err != nil {
} else if err != nil {
// we cannot return error here.
log.Printf("[ERROR] [executor@%s] failed to switch back to remote: %s\n", c.Dir(), err)
log.Printf("[ERROR] [executor@%s] please re-run terraform init -reconfigure\n", c.Dir())
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestAccTerraformCLIApply(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestAccTerraformCLIDestroy(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestAccTerraformCLIImport(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tfexec/terraform_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestTerraformCLIInit(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
e := NewMockExecutor(tc.mockCommands)
terraformCLI := NewTerraformCLI(e)
err := terraformCLI.Init(context.Background(), tc.opts...)
_, err := terraformCLI.Init(context.Background(), tc.opts...)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
Expand All @@ -69,7 +69,7 @@ func TestAccTerraformCLIInit(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tfexec/terraform_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestAccTerraformCLIPlan(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand All @@ -156,7 +156,7 @@ func TestAccTerraformCLIPlanWithOut(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tfexec/terraform_state_mv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down Expand Up @@ -289,7 +289,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAccTerraformCLIStatePull(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestAccTerraformCLIStatePush(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_state_rm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ resource "null_resource" "bar" {}
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestAccTerraformCLIPlanHasChange(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_workspace_new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestAccTerraformCLIWorkspaceNew(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/terraform_workspace_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestAccTerraformCLIWorkspaceSelect(t *testing.T) {
e := SetupTestAcc(t, source)
terraformCLI := NewTerraformCLI(e)

err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
_, err := terraformCLI.Init(context.Background(), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfexec/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func SetupTestAccWithApply(t *testing.T, workspace string, source string) Terraf
tf := NewTerraformCLI(e)
ctx := context.Background()

err := tf.Init(ctx, "-input=false", "-no-color")
_, err := tf.Init(ctx, "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform init: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfmigrate/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func setupWorkDir(ctx context.Context, tf tfexec.TerraformCLI, workspace string)

// init folder
log.Printf("[INFO] [migrator@%s] initialize work dir\n", tf.Dir())
err = tf.Init(ctx, "-input=false", "-no-color")
_, err = tf.Init(ctx, "-input=false", "-no-color")
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit 7541751

Please sign in to comment.