2023-04-06 10:54:58 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2023-04-06 10:54:58 +00:00
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type destroy struct{}
|
|
|
|
|
|
|
|
func (w *destroy) Name() string {
|
|
|
|
return "terraform.Destroy"
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
func (w *destroy) Apply(ctx context.Context, b *bundle.Bundle) error {
|
2023-04-18 14:55:06 +00:00
|
|
|
// return early if plan is empty
|
2023-04-06 10:54:58 +00:00
|
|
|
if b.Plan.IsEmpty {
|
2023-09-14 14:58:06 +00:00
|
|
|
cmdio.LogString(ctx, "No resources to destroy")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tf := b.Terraform
|
|
|
|
if tf == nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return fmt.Errorf("terraform not initialized")
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read plan file
|
|
|
|
plan, err := tf.ShowPlanFile(ctx, b.Plan.Path)
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// print the resources that will be destroyed
|
2023-09-14 14:43:49 +00:00
|
|
|
err = printPlanSummary(ctx, plan)
|
2023-04-06 10:54:58 +00:00
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask for confirmation, if needed
|
|
|
|
if !b.Plan.ConfirmApply {
|
|
|
|
red := color.New(color.FgRed).SprintFunc()
|
2023-09-14 14:43:49 +00:00
|
|
|
b.Plan.ConfirmApply, err = cmdio.AskYesOrNo(ctx, fmt.Sprintf("This will permanently %s resources! Proceed?", red("destroy")))
|
2023-04-06 10:54:58 +00:00
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return err
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return if confirmation was not provided
|
|
|
|
if !b.Plan.ConfirmApply {
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.Plan.Path == "" {
|
2023-05-24 12:45:19 +00:00
|
|
|
return fmt.Errorf("no plan found")
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 14:43:49 +00:00
|
|
|
cmdio.LogString(ctx, "Destroying resources")
|
2023-04-18 14:55:06 +00:00
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
// Apply terraform according to the computed destroy plan
|
|
|
|
err = tf.Apply(ctx, tfexec.DirOrPlan(b.Plan.Path))
|
|
|
|
if err != nil {
|
2023-05-24 12:45:19 +00:00
|
|
|
return fmt.Errorf("terraform destroy: %w", err)
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 14:43:49 +00:00
|
|
|
cmdio.LogString(ctx, "Destruction complete")
|
2023-05-24 12:45:19 +00:00
|
|
|
return nil
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy returns a [bundle.Mutator] that runs the conceptual equivalent of
|
|
|
|
// `terraform destroy ./plan` from the bundle's ephemeral working directory for Terraform.
|
|
|
|
func Destroy() bundle.Mutator {
|
|
|
|
return &destroy{}
|
|
|
|
}
|