Skip to content

Commit

Permalink
Remove a positional dir parameter from TerraformCLI.Apply()
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Oct 25, 2021
1 parent e5e3e8d commit 35c0534
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 63 deletions.
2 changes: 1 addition & 1 deletion tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type TerraformCLI interface {

// Apply applies changes.
// If a plan is given, use it for the input plan.
Apply(ctx context.Context, plan *Plan, dir string, opts ...string) error
Apply(ctx context.Context, plan *Plan, opts ...string) error

// Destroy destroys resources.
Destroy(ctx context.Context, dir string, opts ...string) error
Expand Down
10 changes: 1 addition & 9 deletions tfexec/terraform_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ package tfexec

import (
"context"
"fmt"
"os"
)

// Apply applies changes.
// If a plan is given, use it for the input plan.
func (c *terraformCLI) Apply(ctx context.Context, plan *Plan, dir string, opts ...string) error {
func (c *terraformCLI) Apply(ctx context.Context, plan *Plan, opts ...string) error {
args := []string{"apply"}
args = append(args, opts...)

if plan != nil {
if len(dir) > 0 {
return fmt.Errorf("failed to build options. The plan argument (!= nil) and the dir argument cannot be set at the same time: plan=%v, dir=%v", plan, dir)
}
tmpPlan, err := writeTempFile(plan.Bytes())
defer os.Remove(tmpPlan.Name())
if err != nil {
Expand All @@ -24,10 +20,6 @@ func (c *terraformCLI) Apply(ctx context.Context, plan *Plan, dir string, opts .
args = append(args, tmpPlan.Name())
}

if len(dir) > 0 {
args = append(args, dir)
}

_, _, err := c.Run(ctx, args...)

return err
Expand Down
44 changes: 3 additions & 41 deletions tfexec/terraform_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ func TestTerraformCLIApply(t *testing.T) {
desc string
mockCommands []*mockCommand
plan *Plan
dir string
opts []string
ok bool
}{
{
desc: "no dir and no opts",
desc: "no opts",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "apply"},
Expand All @@ -38,17 +37,6 @@ func TestTerraformCLIApply(t *testing.T) {
},
ok: false,
},
{
desc: "with dir",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "apply", "foo"},
exitCode: 0,
},
},
dir: "foo",
ok: true,
},
{
desc: "with opts",
mockCommands: []*mockCommand{
Expand All @@ -60,18 +48,6 @@ func TestTerraformCLIApply(t *testing.T) {
opts: []string{"-input=false", "-no-color"},
ok: true,
},
{
desc: "with dir and opts",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "apply", "-input=false", "-no-color", "foo"},
exitCode: 0,
},
},
dir: "foo",
opts: []string{"-input=false", "-no-color"},
ok: true,
},
{
desc: "with plan",
mockCommands: []*mockCommand{
Expand All @@ -85,27 +61,13 @@ func TestTerraformCLIApply(t *testing.T) {
opts: []string{"-input=false", "-no-color"},
ok: true,
},
{
desc: "with plan and dir (conflict error)",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "apply", "-input=false", "-no-color", "/path/to/planfile", "foo"},
argsRe: regexp.MustCompile(`^terraform apply -input=false -no-color \S+ foo$`),
exitCode: 0,
},
},
plan: plan,
dir: "foo",
opts: []string{"-input=false", "-state=foo.tfstate"},
ok: false,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
e := NewMockExecutor(tc.mockCommands)
terraformCLI := NewTerraformCLI(e)
err := terraformCLI.Apply(context.Background(), tc.plan, tc.dir, tc.opts...)
err := terraformCLI.Apply(context.Background(), tc.plan, tc.opts...)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
Expand Down Expand Up @@ -133,7 +95,7 @@ func TestAccTerraformCLIApply(t *testing.T) {
t.Fatalf("failed to run terraform plan: %s", err)
}

err = terraformCLI.Apply(context.Background(), plan, "", "-input=false", "-no-color")
err = terraformCLI.Apply(context.Background(), plan, "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -96,7 +96,7 @@ func TestAccTerraformCLIDestroy(t *testing.T) {
t.Fatalf("failed to run terraform init: %s", err)
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -170,7 +170,7 @@ resource "null_resource" "bar" {}
t.Fatalf("failed to run terraform init: %s", err)
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -245,7 +245,7 @@ resource "null_resource" "bar" {}
t.Fatalf("failed to run terraform init: %s", err)
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %s", err)
}
Expand Down Expand Up @@ -288,7 +288,7 @@ resource "null_resource" "bar" {}
t.Fatalf("failed to run terraform init: %s", err)
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -83,7 +83,7 @@ func TestAccTerraformCLIStatePull(t *testing.T) {
t.Fatalf("failed to run terraform init: %s", err)
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -84,7 +84,7 @@ func TestAccTerraformCLIStatePush(t *testing.T) {
t.Fatalf("failed to run terraform init: %s", err)
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -157,7 +157,7 @@ resource "null_resource" "bar" {}
t.Fatalf("failed to run terraform init: %s", err)
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -182,7 +182,7 @@ func TestAccTerraformCLIPlanHasChange(t *testing.T) {
t.Fatalf("expect not to have changes")
}

err = terraformCLI.Apply(context.Background(), nil, "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Apply(context.Background(), nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %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 @@ -304,7 +304,7 @@ func SetupTestAccWithApply(t *testing.T, workspace string, source string) Terraf
}
}

err = tf.Apply(ctx, nil, "", "-input=false", "-no-color", "-auto-approve")
err = tf.Apply(ctx, nil, "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform apply: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions tfmigrate/multi_state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,15 @@ func TestAccMultiStateMigratorApply(t *testing.T) {
if err != nil {
t.Fatalf("failed to read a saved plan file in fromDir: %s", err)
}
err = fromTf.Apply(ctx, tfexec.NewPlan(fromPlan), "", "-input=false", "-no-color")
err = fromTf.Apply(ctx, tfexec.NewPlan(fromPlan), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to apply the saved plan file in fromDir: %s", err)
}
toPlan, err := ioutil.ReadFile(filepath.Join(toTf.Dir(), o.PlanOut))
if err != nil {
t.Fatalf("failed to read a saved plan file in toDir: %s", err)
}
err = toTf.Apply(ctx, tfexec.NewPlan(toPlan), "", "-input=false", "-no-color")
err = toTf.Apply(ctx, tfexec.NewPlan(toPlan), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to apply the saved plan file in toDir: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion tfmigrate/state_migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ resource "aws_security_group" "baz" {}
if err != nil {
t.Fatalf("failed to read a saved plan file: %s", err)
}
err = tf.Apply(ctx, tfexec.NewPlan(plan), "", "-input=false", "-no-color")
err = tf.Apply(ctx, tfexec.NewPlan(plan), "-input=false", "-no-color")
if err != nil {
t.Fatalf("failed to apply the saved plan file: %s", err)
}
Expand Down

0 comments on commit 35c0534

Please sign in to comment.