2024-05-15 12:41:44 +00:00
|
|
|
package terraform
|
2024-02-07 11:17:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-02-07 11:17:17 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/pipelines"
|
2024-05-15 12:41:44 +00:00
|
|
|
tfjson "github.com/hashicorp/terraform-json"
|
2024-02-07 11:17:17 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ErrResourceIsRunning struct {
|
|
|
|
resourceType string
|
|
|
|
resourceId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ErrResourceIsRunning) Error() string {
|
|
|
|
return fmt.Sprintf("%s %s is running", e.resourceType, e.resourceId)
|
|
|
|
}
|
|
|
|
|
|
|
|
type checkRunningResources struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *checkRunningResources) Name() string {
|
|
|
|
return "check-running-resources"
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (l *checkRunningResources) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-02-07 11:17:17 +00:00
|
|
|
if !b.Config.Bundle.Deployment.FailOnActiveRuns {
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-15 12:41:44 +00:00
|
|
|
|
|
|
|
state, err := ParseResourcesState(ctx, b)
|
|
|
|
if err != nil && state == nil {
|
|
|
|
return diag.FromErr(err)
|
|
|
|
}
|
|
|
|
|
2024-05-01 08:22:35 +00:00
|
|
|
w := b.WorkspaceClient()
|
2024-05-15 12:41:44 +00:00
|
|
|
err = checkAnyResourceRunning(ctx, w, state)
|
2024-02-07 11:17:17 +00:00
|
|
|
if err != nil {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.FromErr(err)
|
2024-02-07 11:17:17 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckRunningResource() *checkRunningResources {
|
|
|
|
return &checkRunningResources{}
|
|
|
|
}
|
|
|
|
|
2024-05-15 12:41:44 +00:00
|
|
|
func checkAnyResourceRunning(ctx context.Context, w *databricks.WorkspaceClient, state *resourcesState) error {
|
|
|
|
if state == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-07 11:17:17 +00:00
|
|
|
errs, errCtx := errgroup.WithContext(ctx)
|
|
|
|
|
2024-05-15 12:41:44 +00:00
|
|
|
for _, resource := range state.Resources {
|
|
|
|
if resource.Mode != tfjson.ManagedResourceMode {
|
2024-02-07 11:17:17 +00:00
|
|
|
continue
|
|
|
|
}
|
2024-05-15 12:41:44 +00:00
|
|
|
for _, instance := range resource.Instances {
|
|
|
|
id := instance.Attributes.ID
|
|
|
|
if id == "" {
|
|
|
|
continue
|
2024-05-01 08:22:35 +00:00
|
|
|
}
|
2024-02-07 11:17:17 +00:00
|
|
|
|
2024-05-15 12:41:44 +00:00
|
|
|
switch resource.Type {
|
|
|
|
case "databricks_job":
|
|
|
|
errs.Go(func() error {
|
|
|
|
isRunning, err := IsJobRunning(errCtx, w, id)
|
|
|
|
// If there's an error retrieving the job, we assume it's not running
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isRunning {
|
|
|
|
return &ErrResourceIsRunning{resourceType: "job", resourceId: id}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
case "databricks_pipeline":
|
|
|
|
errs.Go(func() error {
|
|
|
|
isRunning, err := IsPipelineRunning(errCtx, w, id)
|
|
|
|
// If there's an error retrieving the pipeline, we assume it's not running
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if isRunning {
|
|
|
|
return &ErrResourceIsRunning{resourceType: "pipeline", resourceId: id}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2024-05-01 08:22:35 +00:00
|
|
|
}
|
2024-05-15 12:41:44 +00:00
|
|
|
}
|
2024-02-07 11:17:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errs.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsJobRunning(ctx context.Context, w *databricks.WorkspaceClient, jobId string) (bool, error) {
|
|
|
|
id, err := strconv.Atoi(jobId)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
runs, err := w.Jobs.ListRunsAll(ctx, jobs.ListRunsRequest{JobId: int64(id), ActiveOnly: true})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(runs) > 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsPipelineRunning(ctx context.Context, w *databricks.WorkspaceClient, pipelineId string) (bool, error) {
|
|
|
|
resp, err := w.Pipelines.Get(ctx, pipelines.GetPipelineRequest{PipelineId: pipelineId})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
switch resp.State {
|
|
|
|
case pipelines.PipelineStateIdle, pipelines.PipelineStateFailed, pipelines.PipelineStateDeleted:
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|