2023-09-04 09:55:01 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-18 16:23:39 +00:00
|
|
|
"slices"
|
2023-09-04 09:55:01 +00:00
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
"github.com/databricks/cli/bundle/libraries"
|
2024-03-18 16:23:39 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-09-04 09:55:01 +00:00
|
|
|
)
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
type jobRewritePattern struct {
|
|
|
|
pattern dyn.Pattern
|
|
|
|
fn rewriteFunc
|
|
|
|
skipRewrite func(string) bool
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
func noSkipRewrite(string) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:04:22 +00:00
|
|
|
func rewritePatterns(t *translateContext, base dyn.Pattern) []jobRewritePattern {
|
2024-04-22 11:44:34 +00:00
|
|
|
return []jobRewritePattern{
|
2024-03-18 16:23:39 +00:00
|
|
|
{
|
|
|
|
base.Append(dyn.Key("notebook_task"), dyn.Key("notebook_path")),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateNotebookPath,
|
2024-04-22 11:44:34 +00:00
|
|
|
noSkipRewrite,
|
2024-03-18 16:23:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
base.Append(dyn.Key("spark_python_task"), dyn.Key("python_file")),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateFilePath,
|
2024-04-22 11:44:34 +00:00
|
|
|
noSkipRewrite,
|
2024-03-18 16:23:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
base.Append(dyn.Key("dbt_task"), dyn.Key("project_directory")),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateDirectoryPath,
|
2024-04-22 11:44:34 +00:00
|
|
|
noSkipRewrite,
|
2024-03-18 16:23:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
base.Append(dyn.Key("sql_task"), dyn.Key("file"), dyn.Key("path")),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateFilePath,
|
2024-04-22 11:44:34 +00:00
|
|
|
noSkipRewrite,
|
2024-03-18 16:23:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
base.Append(dyn.Key("libraries"), dyn.AnyIndex(), dyn.Key("whl")),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateNoOp,
|
2024-04-22 11:44:34 +00:00
|
|
|
noSkipRewrite,
|
2024-03-18 16:23:39 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
base.Append(dyn.Key("libraries"), dyn.AnyIndex(), dyn.Key("jar")),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateNoOp,
|
2024-04-22 11:44:34 +00:00
|
|
|
noSkipRewrite,
|
2024-03-18 16:23:39 +00:00
|
|
|
},
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:04:22 +00:00
|
|
|
func (t *translateContext) jobRewritePatterns() []jobRewritePattern {
|
2024-03-18 16:23:39 +00:00
|
|
|
// Base pattern to match all tasks in all jobs.
|
|
|
|
base := dyn.NewPattern(
|
|
|
|
dyn.Key("resources"),
|
|
|
|
dyn.Key("jobs"),
|
|
|
|
dyn.AnyKey(),
|
|
|
|
dyn.Key("tasks"),
|
|
|
|
dyn.AnyIndex(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Compile list of patterns and their respective rewrite functions.
|
2024-04-22 11:44:34 +00:00
|
|
|
jobEnvironmentsPatterns := []jobRewritePattern{
|
|
|
|
{
|
|
|
|
dyn.NewPattern(
|
|
|
|
dyn.Key("resources"),
|
|
|
|
dyn.Key("jobs"),
|
|
|
|
dyn.AnyKey(),
|
|
|
|
dyn.Key("environments"),
|
|
|
|
dyn.AnyIndex(),
|
|
|
|
dyn.Key("spec"),
|
|
|
|
dyn.Key("dependencies"),
|
|
|
|
dyn.AnyIndex(),
|
|
|
|
),
|
2024-06-25 10:04:22 +00:00
|
|
|
t.translateNoOpWithPrefix,
|
2024-04-22 11:44:34 +00:00
|
|
|
func(s string) bool {
|
2024-08-14 09:03:44 +00:00
|
|
|
return !libraries.IsLibraryLocal(s)
|
2024-04-22 11:44:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2024-06-25 10:04:22 +00:00
|
|
|
|
|
|
|
taskPatterns := rewritePatterns(t, base)
|
|
|
|
forEachPatterns := rewritePatterns(t, base.Append(dyn.Key("for_each_task"), dyn.Key("task")))
|
2024-04-22 11:44:34 +00:00
|
|
|
allPatterns := append(taskPatterns, jobEnvironmentsPatterns...)
|
|
|
|
allPatterns = append(allPatterns, forEachPatterns...)
|
2024-06-25 10:04:22 +00:00
|
|
|
return allPatterns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *translateContext) applyJobTranslations(v dyn.Value) (dyn.Value, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
fallback, err := gatherFallbackPaths(v, "jobs")
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not translate job task paths if using Git source
|
|
|
|
var ignore []string
|
|
|
|
for key, job := range t.b.Config.Resources.Jobs {
|
|
|
|
if job.GitSource != nil {
|
|
|
|
ignore = append(ignore, key)
|
|
|
|
}
|
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
2024-06-25 10:04:22 +00:00
|
|
|
for _, rewritePattern := range t.jobRewritePatterns() {
|
|
|
|
v, err = dyn.MapByPattern(v, rewritePattern.pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
|
2024-03-18 16:23:39 +00:00
|
|
|
key := p[2].Key()
|
|
|
|
|
|
|
|
// Skip path translation if the job is using git source.
|
|
|
|
if slices.Contains(ignore, key) {
|
|
|
|
return v, nil
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
|
|
|
dir, err := v.Location().Directory()
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for job %s: %w", key, err)
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
2024-03-18 16:23:39 +00:00
|
|
|
|
2024-04-22 11:44:34 +00:00
|
|
|
sv := v.MustString()
|
2024-06-25 10:04:22 +00:00
|
|
|
if rewritePattern.skipRewrite(sv) {
|
2024-04-22 11:44:34 +00:00
|
|
|
return v, nil
|
|
|
|
}
|
2024-06-25 10:04:22 +00:00
|
|
|
return t.rewriteRelativeTo(p, v, rewritePattern.fn, dir, fallback[key])
|
2024-03-18 16:23:39 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return dyn.InvalidValue, err
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-18 16:23:39 +00:00
|
|
|
return v, nil
|
2023-09-04 09:55:01 +00:00
|
|
|
}
|