2023-07-12 06:51:54 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
|
|
|
"github.com/databricks/cli/bundle/config/resources"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-09-11 08:18:43 +00:00
|
|
|
"github.com/databricks/cli/libs/env"
|
2023-07-12 06:51:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type overrideCompute struct{}
|
|
|
|
|
|
|
|
func OverrideCompute() bundle.Mutator {
|
|
|
|
return &overrideCompute{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *overrideCompute) Name() string {
|
|
|
|
return "OverrideCompute"
|
|
|
|
}
|
|
|
|
|
|
|
|
func overrideJobCompute(j *resources.Job, compute string) {
|
|
|
|
for i := range j.Tasks {
|
2024-04-12 09:53:29 +00:00
|
|
|
var task = &j.Tasks[i]
|
|
|
|
|
|
|
|
if task.ForEachTask != nil {
|
|
|
|
task = &task.ForEachTask.Task
|
|
|
|
}
|
|
|
|
|
2024-04-16 12:03:21 +00:00
|
|
|
if task.NewCluster != nil || task.ExistingClusterId != "" || task.EnvironmentKey != "" || task.JobClusterKey != "" {
|
2023-07-12 06:51:54 +00:00
|
|
|
task.NewCluster = nil
|
2023-08-28 07:51:35 +00:00
|
|
|
task.JobClusterKey = ""
|
2024-04-16 12:03:21 +00:00
|
|
|
task.EnvironmentKey = ""
|
2023-07-12 06:51:54 +00:00
|
|
|
task.ExistingClusterId = compute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *overrideCompute) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2023-07-12 06:51:54 +00:00
|
|
|
if b.Config.Bundle.Mode != config.Development {
|
|
|
|
if b.Config.Bundle.ComputeID != "" {
|
2024-03-25 14:18:47 +00:00
|
|
|
return diag.Errorf("cannot override compute for an target that does not use 'mode: development'")
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-11 08:18:43 +00:00
|
|
|
if v := env.Get(ctx, "DATABRICKS_CLUSTER_ID"); v != "" {
|
|
|
|
b.Config.Bundle.ComputeID = v
|
2023-07-12 06:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.Config.Bundle.ComputeID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := b.Config.Resources
|
|
|
|
for i := range r.Jobs {
|
|
|
|
overrideJobCompute(r.Jobs[i], b.Config.Bundle.ComputeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|