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"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-04-06 10:54:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-07-08 15:35:47 +00:00
|
|
|
if b.Plan.IsEmpty() {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-07-08 15:35:47 +00:00
|
|
|
cmdio.LogString(ctx, "Destroying resources...")
|
2023-04-18 14:55:06 +00:00
|
|
|
|
2024-07-08 15:35:47 +00:00
|
|
|
err := b.Plan.Apply()
|
2023-04-06 10:54:58 +00:00
|
|
|
if err != nil {
|
2024-07-08 15:35:47 +00:00
|
|
|
return diag.Errorf("terraform apply: %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{}
|
|
|
|
}
|