Skip to content

Commit

Permalink
Allow apply to pass Plan
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Jul 21, 2020
1 parent 3e503c3 commit 9922135
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
3 changes: 2 additions & 1 deletion tfexec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ type TerraformCLI interface {
Plan(ctx context.Context, state *State, dir string, opts ...string) (*Plan, error)

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

// Destroy destroys resources.
Destroy(ctx context.Context, dir string, opts ...string) error
Expand Down
28 changes: 24 additions & 4 deletions tfexec/terraform_apply.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
package tfexec

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

// Apply applies changes.
func (c *terraformCLI) Apply(ctx context.Context, dirOrPlan string, opts ...string) error {
// 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 {
args := []string{"apply"}
args = append(args, opts...)
if len(dirOrPlan) > 0 {
args = append(args, dirOrPlan)

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 {
return err
}
args = append(args, tmpPlan.Name())
}

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

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

return err
}
45 changes: 38 additions & 7 deletions tfexec/terraform_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package tfexec

import (
"context"
"regexp"
"testing"
)

func TestTerraformCLIApply(t *testing.T) {
plan := NewPlan([]byte("dummy plan"))

cases := []struct {
desc string
mockCommands []*mockCommand
dirOrPlan string
plan *Plan
dir string
opts []string
ok bool
}{
Expand Down Expand Up @@ -41,8 +45,8 @@ func TestTerraformCLIApply(t *testing.T) {
exitCode: 0,
},
},
dirOrPlan: "foo",
ok: true,
dir: "foo",
ok: true,
},
{
desc: "with opts",
Expand All @@ -63,17 +67,44 @@ func TestTerraformCLIApply(t *testing.T) {
exitCode: 0,
},
},
dirOrPlan: "foo",
opts: []string{"-input=false", "-no-color"},
ok: true,
dir: "foo",
opts: []string{"-input=false", "-no-color"},
ok: true,
},
{
desc: "with plan",
mockCommands: []*mockCommand{
{
args: []string{"terraform", "apply", "-input=false", "-no-color", "/path/to/planfile"},
argsRe: regexp.MustCompile(`^terraform apply -input=false -no-color \S+$`),
exitCode: 0,
},
},
plan: plan,
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: 1,
},
},
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.dirOrPlan, tc.opts...)
err := terraformCLI.Apply(context.Background(), tc.plan, tc.dir, tc.opts...)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
Expand Down

0 comments on commit 9922135

Please sign in to comment.