Skip to content

Commit

Permalink
apply: allow use of -destroy flag for compatible terraform versions
Browse files Browse the repository at this point in the history
Implement the ApplyOption interface for the DestroyFlagOption
struct which enables the user to run `terraform apply -destroy`
as they would using the terraform binary directly by calling
`func (tf *Terraform) Apply`.
  • Loading branch information
rstandt committed Apr 15, 2022
1 parent 6861367 commit 404a028
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tfexec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type applyConfig struct {
backup string
destroy bool
dirOrPlan string
lock bool

Expand All @@ -28,6 +29,7 @@ type applyConfig struct {
}

var defaultApplyOptions = applyConfig{
destroy: false,
lock: true,
parallelism: 10,
refresh: true,
Expand Down Expand Up @@ -90,6 +92,10 @@ func (opt *ReattachOption) configureApply(conf *applyConfig) {
conf.reattachInfo = opt.info
}

func (opt *DestroyFlagOption) configureApply(conf *applyConfig) {
conf.destroy = opt.destroy
}

// Apply represents the terraform apply subcommand.
func (tf *Terraform) Apply(ctx context.Context, opts ...ApplyOption) error {
cmd, err := tf.applyCmd(ctx, opts...)
Expand Down Expand Up @@ -140,6 +146,14 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C
args = append(args, "-replace="+addr)
}
}
if c.destroy {
err := tf.compatible(ctx, tf0_15_2, nil)
if err != nil {
return nil, fmt.Errorf("-destroy option was introduced in Terraform 0.15.2: %w", err)
}
args = append(args, "-destroy")
}

if c.targets != nil {
for _, ta := range c.targets {
args = append(args, "-target="+ta)
Expand Down
2 changes: 2 additions & 0 deletions tfexec/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestApplyCmd(t *testing.T) {
Target("target2"),
Var("var1=foo"),
Var("var2=bar"),
Destroy(true),
DirOrPlan("testfile"),
)
if err != nil {
Expand All @@ -57,6 +58,7 @@ func TestApplyCmd(t *testing.T) {
"-refresh=false",
"-replace=aws_instance.test",
"-replace=google_pubsub_topic.test",
"-destroy",
"-target=target1",
"-target=target2",
"-var", "var1=foo",
Expand Down
26 changes: 26 additions & 0 deletions tfexec/internal/e2etest/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/hashicorp/terraform-exec/tfexec"
)

var (
validateMinApplyDestroyVersion = version.Must(version.NewVersion("0.15.2"))
)

func TestDestroy(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
Expand All @@ -27,3 +31,25 @@ func TestDestroy(t *testing.T) {
}
})
}

func TestApplyDestroy(t *testing.T) {
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
if tfv.LessThan(validateMinApplyDestroyVersion) {
t.Skip("terraform apply -destroy was added in Terraform 0.15.2, so test is not valid")
}
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init in test directory: %s", err)
}

err = tf.Apply(context.Background())
if err != nil {
t.Fatalf("error running Apply: %s", err)
}

err = tf.Apply(context.Background(), tfexec.Destroy(true))
if err != nil {
t.Fatalf("error running Apply -destroy: %s", err)
}
})
}

0 comments on commit 404a028

Please sign in to comment.