2023-04-06 10:54:58 +00:00
|
|
|
package phases
|
|
|
|
|
|
|
|
import (
|
2024-07-09 15:08:38 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2024-07-24 13:02:19 +00:00
|
|
|
"fmt"
|
2024-07-09 15:08:38 +00:00
|
|
|
"net/http"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/deploy/files"
|
|
|
|
"github.com/databricks/cli/bundle/deploy/lock"
|
|
|
|
"github.com/databricks/cli/bundle/deploy/terraform"
|
2024-07-24 13:02:19 +00:00
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
|
2024-07-09 15:08:38 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
2024-07-24 13:02:19 +00:00
|
|
|
terraformlib "github.com/databricks/cli/libs/terraform"
|
2024-07-09 15:08:38 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go/apierr"
|
2023-04-06 10:54:58 +00:00
|
|
|
)
|
|
|
|
|
2024-07-09 15:08:38 +00:00
|
|
|
func assertRootPathExists(ctx context.Context, b *bundle.Bundle) (bool, error) {
|
|
|
|
w := b.WorkspaceClient()
|
|
|
|
_, err := w.Workspace.GetStatusByPath(ctx, b.Config.Workspace.RootPath)
|
|
|
|
|
|
|
|
var aerr *apierr.APIError
|
|
|
|
if errors.As(err, &aerr) && aerr.StatusCode == http.StatusNotFound {
|
|
|
|
log.Infof(ctx, "Root path does not exist: %s", b.Config.Workspace.RootPath)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2024-07-24 13:02:19 +00:00
|
|
|
func approvalForDestroy(ctx context.Context, b *bundle.Bundle) (bool, error) {
|
|
|
|
tf := b.Terraform
|
|
|
|
if tf == nil {
|
|
|
|
return false, fmt.Errorf("terraform not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
// read plan file
|
|
|
|
plan, err := tf.ShowPlanFile(ctx, b.Plan.Path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteActions := make([]terraformlib.Action, 0)
|
|
|
|
for _, rc := range plan.ResourceChanges {
|
|
|
|
if rc.Change.Actions.Delete() {
|
|
|
|
deleteActions = append(deleteActions, terraformlib.Action{
|
|
|
|
Action: terraformlib.ActionTypeDelete,
|
|
|
|
ResourceType: rc.Type,
|
|
|
|
ResourceName: rc.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(deleteActions) > 0 {
|
|
|
|
cmdio.LogString(ctx, "The following resources will be deleted:")
|
|
|
|
for _, a := range deleteActions {
|
|
|
|
cmdio.Log(ctx, a)
|
|
|
|
}
|
|
|
|
cmdio.LogString(ctx, "")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdio.LogString(ctx, fmt.Sprintf("All files and directories at the following location will be deleted: %s", b.Config.Workspace.RootPath))
|
|
|
|
cmdio.LogString(ctx, "")
|
|
|
|
|
|
|
|
if b.AutoApprove {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
approved, err := cmdio.AskYesOrNo(ctx, "Would you like to proceed?")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return approved, nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
// The destroy phase deletes artifacts and resources.
|
|
|
|
func Destroy() bundle.Mutator {
|
2024-07-24 13:02:19 +00:00
|
|
|
// Core destructive mutators for destroy. These require informed user consent.
|
|
|
|
destroyCore := bundle.Seq(
|
2024-07-31 14:07:25 +00:00
|
|
|
terraform.Apply(),
|
2024-07-24 13:02:19 +00:00
|
|
|
files.Delete(),
|
|
|
|
bundle.LogString("Destroy complete!"),
|
|
|
|
)
|
|
|
|
|
2023-05-24 12:45:19 +00:00
|
|
|
destroyMutator := bundle.Seq(
|
2023-05-16 16:01:50 +00:00
|
|
|
lock.Acquire(),
|
2023-05-24 12:45:19 +00:00
|
|
|
bundle.Defer(
|
|
|
|
bundle.Seq(
|
2024-02-14 18:04:45 +00:00
|
|
|
terraform.StatePull(),
|
2023-07-05 19:58:06 +00:00
|
|
|
terraform.Interpolate(),
|
|
|
|
terraform.Write(),
|
2023-05-24 12:45:19 +00:00
|
|
|
terraform.Plan(terraform.PlanGoal("destroy")),
|
2024-07-24 13:02:19 +00:00
|
|
|
bundle.If(
|
|
|
|
approvalForDestroy,
|
|
|
|
destroyCore,
|
|
|
|
bundle.LogString("Destroy cancelled!"),
|
|
|
|
),
|
2023-05-24 12:45:19 +00:00
|
|
|
),
|
2023-06-19 13:57:25 +00:00
|
|
|
lock.Release(lock.GoalDestroy),
|
2023-05-24 12:45:19 +00:00
|
|
|
),
|
|
|
|
)
|
2023-05-16 16:01:50 +00:00
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
return newPhase(
|
|
|
|
"destroy",
|
2024-07-09 15:08:38 +00:00
|
|
|
[]bundle.Mutator{
|
|
|
|
// Only run deploy mutator if root path exists.
|
|
|
|
bundle.If(
|
|
|
|
assertRootPathExists,
|
|
|
|
destroyMutator,
|
|
|
|
bundle.LogString("No active deployment found to destroy!"),
|
|
|
|
),
|
|
|
|
},
|
2023-04-06 10:54:58 +00:00
|
|
|
)
|
|
|
|
}
|