2023-04-06 10:54:58 +00:00
|
|
|
package phases
|
|
|
|
|
|
|
|
import (
|
2024-07-09 15:08:38 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"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-09 15:08:38 +00:00
|
|
|
"github.com/databricks/cli/libs/log"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2023-04-06 10:54:58 +00:00
|
|
|
// The destroy phase deletes artifacts and resources.
|
|
|
|
func Destroy() bundle.Mutator {
|
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")),
|
|
|
|
terraform.Destroy(),
|
|
|
|
terraform.StatePush(),
|
|
|
|
files.Delete(),
|
|
|
|
),
|
2023-06-19 13:57:25 +00:00
|
|
|
lock.Release(lock.GoalDestroy),
|
2023-05-24 12:45:19 +00:00
|
|
|
),
|
2023-12-21 08:00:37 +00:00
|
|
|
bundle.LogString("Destroy complete!"),
|
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
|
|
|
)
|
|
|
|
}
|