2023-04-06 10:54:58 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-07-24 13:02:19 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2023-04-06 10:54:58 +00:00
|
|
|
"github.com/hashicorp/terraform-exec/tfexec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type destroy struct{}
|
|
|
|
|
|
|
|
func (w *destroy) Name() string {
|
|
|
|
return "terraform.Destroy"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (w *destroy) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
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 {
|
2024-07-24 13:02:19 +00:00
|
|
|
log.Debugf(ctx, "No resources to destroy in plan. Skipping 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 {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("terraform not initialized")
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.Plan.Path == "" {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("no plan found")
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply terraform according to the computed destroy plan
|
2024-07-24 13:02:19 +00:00
|
|
|
err := tf.Apply(ctx, tfexec.DirOrPlan(b.Plan.Path))
|
2023-04-06 10:54:58 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("terraform destroy: %v", err)
|
2023-04-06 10:54:58 +00:00
|
|
|
}
|
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{}
|
|
|
|
}
|