2024-01-17 14:26:33 +00:00
|
|
|
package generate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/databricks/cli/libs/dyn"
|
|
|
|
"github.com/databricks/cli/libs/dyn/yamlsaver"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/jobs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var jobOrder = yamlsaver.NewOrder([]string{"name", "job_clusters", "compute", "tasks"})
|
|
|
|
var taskOrder = yamlsaver.NewOrder([]string{"task_key", "depends_on", "existing_cluster_id", "new_cluster", "job_cluster_key"})
|
|
|
|
|
|
|
|
func ConvertJobToValue(job *jobs.Job) (dyn.Value, error) {
|
|
|
|
value := make(map[string]dyn.Value)
|
|
|
|
|
|
|
|
if job.Settings.Tasks != nil {
|
|
|
|
tasks := make([]dyn.Value, 0)
|
|
|
|
for _, task := range job.Settings.Tasks {
|
|
|
|
v, err := convertTaskToValue(task, taskOrder)
|
|
|
|
if err != nil {
|
2024-06-21 14:22:42 +00:00
|
|
|
return dyn.InvalidValue, err
|
2024-01-17 14:26:33 +00:00
|
|
|
}
|
|
|
|
tasks = append(tasks, v)
|
|
|
|
}
|
|
|
|
// We're using location lines to define the order of keys in exported YAML.
|
2024-07-16 11:27:27 +00:00
|
|
|
value["tasks"] = dyn.NewValue(tasks, []dyn.Location{{Line: jobOrder.Get("tasks")}})
|
2024-01-17 14:26:33 +00:00
|
|
|
}
|
|
|
|
|
2024-09-11 09:49:58 +00:00
|
|
|
// We're processing job.Settings.Parameters separately to retain empty default values.
|
|
|
|
if len(job.Settings.Parameters) > 0 {
|
|
|
|
params := make([]dyn.Value, 0)
|
|
|
|
for _, parameter := range job.Settings.Parameters {
|
|
|
|
p := map[string]dyn.Value{
|
|
|
|
"name": dyn.NewValue(parameter.Name, []dyn.Location{{Line: 0}}), // We use Line: 0 to ensure that the name goes first.
|
|
|
|
"default": dyn.NewValue(parameter.Default, []dyn.Location{{Line: 1}}),
|
|
|
|
}
|
|
|
|
params = append(params, dyn.NewValue(p, []dyn.Location{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
value["parameters"] = dyn.NewValue(params, []dyn.Location{{Line: jobOrder.Get("parameters")}})
|
|
|
|
}
|
|
|
|
|
2024-01-17 14:26:33 +00:00
|
|
|
return yamlsaver.ConvertToMapValue(job.Settings, jobOrder, []string{"format", "new_cluster", "existing_cluster_id"}, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertTaskToValue(task jobs.Task, order *yamlsaver.Order) (dyn.Value, error) {
|
|
|
|
dst := make(map[string]dyn.Value)
|
|
|
|
return yamlsaver.ConvertToMapValue(task, order, []string{"format"}, dst)
|
|
|
|
}
|