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

Remove a positional dir parameter from TerraformCLI #49

Merged
merged 4 commits into from
Oct 28, 2021
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
20 changes: 10 additions & 10 deletions tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ type TerraformCLI interface {
// Verison returns a version number of Terraform.
Version(ctx context.Context) (string, error)

// Init initializes a given work directory.
Init(ctx context.Context, dir string, opts ...string) error
// Init initializes the current work directory.
Init(ctx context.Context, opts ...string) error

// Plan computes expected changes.
// If a state is given, use it for the input state.
Plan(ctx context.Context, state *State, dir string, opts ...string) (*Plan, error)
Plan(ctx context.Context, state *State, opts ...string) (*Plan, error)

// 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
Destroy(ctx context.Context, opts ...string) error

// Import imports an existing resource to state.
// If a state is given, use it for the input state.
Expand Down Expand Up @@ -129,7 +129,7 @@ type TerraformCLI interface {
OverrideBackendToLocal(ctx context.Context, filename string, workspace string) (func(), error)

// PlanHasChange is a helper method which runs plan and return true if the plan has change.
PlanHasChange(ctx context.Context, state *State, dir string, opts ...string) (bool, error)
PlanHasChange(ctx context.Context, state *State, opts ...string) (bool, error)
}

// terraformCLI implements the TerraformCLI interface.
Expand Down Expand Up @@ -221,7 +221,7 @@ terraform {
}

log.Printf("[INFO] [executor@%s] switch backend to local\n", c.Dir())
err := c.Init(ctx, "", "-input=false", "-no-color", "-reconfigure")
err := c.Init(ctx, "-input=false", "-no-color", "-reconfigure")
if err != nil {
// remove the override file before return an error.
os.Remove(path)
Expand Down Expand Up @@ -253,7 +253,7 @@ 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())
err = c.Init(ctx, "", "-input=false", "-no-color", "-reconfigure")
err = c.Init(ctx, "-input=false", "-no-color", "-reconfigure")
if err != nil {
// we cannot return error here.
log.Printf("[ERROR] [executor@%s] failed to switch back to remote: %s\n", c.Dir(), err)
Expand All @@ -265,10 +265,10 @@ terraform {
}

// PlanHasChange is a helper method which runs plan and return true only if the plan has change.
func (c *terraformCLI) PlanHasChange(ctx context.Context, state *State, dir string, opts ...string) (bool, error) {
func (c *terraformCLI) PlanHasChange(ctx context.Context, state *State, opts ...string) (bool, error) {

merged := mergeOptions(opts, []string{"-input=false", "-no-color", "-detailed-exitcode"})
_, err := c.Plan(ctx, state, dir, merged...)
_, err := c.Plan(ctx, state, merged...)
if err != nil {
if exitErr, ok := err.(ExitError); ok && exitErr.ExitCode() == 2 {
return true, nil
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
48 changes: 5 additions & 43 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 All @@ -123,17 +85,17 @@ 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)
}

plan, err := terraformCLI.Plan(context.Background(), nil, "", "-input=false", "-no-color")
plan, err := terraformCLI.Plan(context.Background(), nil, "-input=false", "-no-color")
if err != nil {
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
5 changes: 1 addition & 4 deletions tfexec/terraform_destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package tfexec
import "context"

// Destroy destroys resources.
func (c *terraformCLI) Destroy(ctx context.Context, dir string, opts ...string) error {
func (c *terraformCLI) Destroy(ctx context.Context, opts ...string) error {
args := []string{"destroy"}
args = append(args, opts...)
if len(dir) > 0 {
args = append(args, dir)
}
_, _, err := c.Run(ctx, args...)
return err
}
34 changes: 5 additions & 29 deletions tfexec/terraform_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ func TestTerraformCLIDestroy(t *testing.T) {
cases := []struct {
desc string
mockCommands []*mockCommand
dir string
opts []string
ok bool
}{
{
desc: "no dir and no opts",
desc: "no opts",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "destroy"},
Expand All @@ -33,17 +32,6 @@ func TestTerraformCLIDestroy(t *testing.T) {
},
ok: false,
},
{
desc: "with dir",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "destroy", "foo"},
exitCode: 0,
},
},
dir: "foo",
ok: true,
},
{
desc: "with opts",
mockCommands: []*mockCommand{
Expand All @@ -55,25 +43,13 @@ func TestTerraformCLIDestroy(t *testing.T) {
opts: []string{"-input=false", "-no-color"},
ok: true,
},
{
desc: "with dir and opts",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "destroy", "-input=false", "-no-color", "foo"},
exitCode: 0,
},
},
dir: "foo",
opts: []string{"-input=false", "-no-color"},
ok: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
e := NewMockExecutor(tc.mockCommands)
terraformCLI := NewTerraformCLI(e)
err := terraformCLI.Destroy(context.Background(), tc.dir, tc.opts...)
err := terraformCLI.Destroy(context.Background(), tc.opts...)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
Expand All @@ -91,17 +67,17 @@ 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)
}

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)
}

err = terraformCLI.Destroy(context.Background(), "", "-input=false", "-no-color", "-auto-approve")
err = terraformCLI.Destroy(context.Background(), "-input=false", "-no-color", "-auto-approve")
if err != nil {
t.Fatalf("failed to run terraform destroy: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions tfexec/terraform_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ 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)
}

_, err = terraformCLI.Plan(context.Background(), nil, "", "-input=false", "-no-color", "-detailed-exitcode")
_, err = terraformCLI.Plan(context.Background(), nil, "-input=false", "-no-color", "-detailed-exitcode")
if err != nil {
if exitErr, ok := err.(*exitError); ok {
if exitErr.ExitCode() != 2 {
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestAccTerraformCLIImport(t *testing.T) {
t.Errorf("got: %v, want: %v", got, want)
}

_, err = terraformCLI.Plan(context.Background(), state, "", "-input=false", "-no-color", "-detailed-exitcode")
_, err = terraformCLI.Plan(context.Background(), state, "-input=false", "-no-color", "-detailed-exitcode")
if err != nil {
t.Fatalf("failed to run terraform plan after import (expected no diff): %s", err)
}
Expand Down
7 changes: 2 additions & 5 deletions tfexec/terraform_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package tfexec

import "context"

// Init initializes a given work directory.
func (c *terraformCLI) Init(ctx context.Context, dir string, opts ...string) error {
// Init initializes the current work directory.
func (c *terraformCLI) Init(ctx context.Context, opts ...string) error {
args := []string{"init"}
args = append(args, opts...)
if len(dir) > 0 {
args = append(args, dir)
}
_, _, err := c.Run(ctx, args...)
return err
}
Loading