Skip to content

Commit 65fea9e

Browse files
committed
apply: allow use of -destroy flag for compatible terraform versions
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`.
1 parent e8dfc80 commit 65fea9e

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

tfexec/apply.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
type applyConfig struct {
1111
backup string
12+
destroy bool
1213
dirOrPlan string
1314
lock bool
1415

@@ -28,6 +29,7 @@ type applyConfig struct {
2829
}
2930

3031
var defaultApplyOptions = applyConfig{
32+
destroy: false,
3133
lock: true,
3234
parallelism: 10,
3335
refresh: true,
@@ -90,6 +92,10 @@ func (opt *ReattachOption) configureApply(conf *applyConfig) {
9092
conf.reattachInfo = opt.info
9193
}
9294

95+
func (opt *DestroyFlagOption) configureApply(conf *applyConfig) {
96+
conf.destroy = opt.destroy
97+
}
98+
9399
// Apply represents the terraform apply subcommand.
94100
func (tf *Terraform) Apply(ctx context.Context, opts ...ApplyOption) error {
95101
cmd, err := tf.applyCmd(ctx, opts...)
@@ -140,6 +146,14 @@ func (tf *Terraform) applyCmd(ctx context.Context, opts ...ApplyOption) (*exec.C
140146
args = append(args, "-replace="+addr)
141147
}
142148
}
149+
if c.destroy {
150+
err := tf.compatible(ctx, tf0_15_2, nil)
151+
if err != nil {
152+
return nil, fmt.Errorf("-destroy option was introduced in Terraform 0.15.2: %w", err)
153+
}
154+
args = append(args, "-destroy")
155+
}
156+
143157
if c.targets != nil {
144158
for _, ta := range c.targets {
145159
args = append(args, "-target="+ta)

tfexec/apply_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestApplyCmd(t *testing.T) {
3535
Target("target2"),
3636
Var("var1=foo"),
3737
Var("var2=bar"),
38+
Destroy(true),
3839
DirOrPlan("testfile"),
3940
)
4041
if err != nil {
@@ -57,6 +58,7 @@ func TestApplyCmd(t *testing.T) {
5758
"-refresh=false",
5859
"-replace=aws_instance.test",
5960
"-replace=google_pubsub_topic.test",
61+
"-destroy",
6062
"-target=target1",
6163
"-target=target2",
6264
"-var", "var1=foo",

tfexec/internal/e2etest/apply_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import (
99
"github.com/hashicorp/terraform-exec/tfexec"
1010
)
1111

12+
var (
13+
validateMinApplyDestroyVersion = version.Must(version.NewVersion("0.15.2"))
14+
)
15+
1216
func TestApply(t *testing.T) {
1317
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
1418
err := tf.Init(context.Background())
@@ -22,3 +26,25 @@ func TestApply(t *testing.T) {
2226
}
2327
})
2428
}
29+
30+
func TestApplyDestroy(t *testing.T) {
31+
runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
32+
if tfv.LessThan(validateMinApplyDestroyVersion) {
33+
t.Skip("terraform apply -destroy was added in Terraform 0.15.2, so test is not valid")
34+
}
35+
err := tf.Init(context.Background())
36+
if err != nil {
37+
t.Fatalf("error running Init in test directory: %s", err)
38+
}
39+
40+
err = tf.Apply(context.Background())
41+
if err != nil {
42+
t.Fatalf("error running Apply: %s", err)
43+
}
44+
45+
err = tf.Apply(context.Background(), tfexec.Destroy(true))
46+
if err != nil {
47+
t.Fatalf("error running Apply -destroy: %s", err)
48+
}
49+
})
50+
}

0 commit comments

Comments
 (0)