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 {
|
|
|
|
task := &j.Tasks[i]
|
2023-08-28 07:51:35 +00:00
|
|
|
if task.NewCluster != nil || task.ExistingClusterId != "" || task.ComputeKey != "" || task.JobClusterKey != "" {
|
2023-07-12 06:51:54 +00:00
|
|
|
task.NewCluster = nil
|
2023-08-28 07:51:35 +00:00
|
|
|
task.JobClusterKey = ""
|
|
|
|
task.ComputeKey = ""
|
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
|
|
|
|
}
|