2024-03-27 10:49:05 +00:00
|
|
|
package loader
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-28 09:59:43 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"fmt"
|
2024-09-19 09:41:40 +00:00
|
|
|
"strings"
|
2022-11-18 09:57:31 +00:00
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle"
|
|
|
|
"github.com/databricks/cli/bundle/config"
|
2024-03-25 14:18:47 +00:00
|
|
|
"github.com/databricks/cli/libs/diag"
|
2024-09-19 09:41:40 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2022-11-18 09:57:31 +00:00
|
|
|
)
|
|
|
|
|
2024-09-19 09:41:40 +00:00
|
|
|
// Steps:
|
|
|
|
// 1. Return info diag here if convention not followed
|
|
|
|
// 2. Add unit test for this mutator that convention is followed. Also add mutators for the dynamic extensions computation.
|
|
|
|
// 3. Add INFO rendering to the validate command
|
|
|
|
// 4. Add unit test that the INFO rendering is correct
|
|
|
|
// 5. Manually test the info diag.
|
|
|
|
|
2024-09-19 09:42:25 +00:00
|
|
|
// TODO: Should we detect and enforce this convention for .yaml files as well?
|
|
|
|
|
2024-09-19 09:41:40 +00:00
|
|
|
// TODO: Since we are skipping environemnts here, we should return a warning
|
|
|
|
// if environemnts is used (is that already the case?). And explain in the PR that
|
|
|
|
// we are choosing to not gather resources from environments.
|
|
|
|
|
|
|
|
// TODO: Talk in the PR about how this synergizes with the validate all unique
|
|
|
|
// keys mutator.
|
2024-09-24 12:39:28 +00:00
|
|
|
// Should I add a new abstraction for dyn values here?
|
2024-09-19 09:41:40 +00:00
|
|
|
|
|
|
|
var resourceTypes = []string{
|
|
|
|
"job",
|
|
|
|
"pipeline",
|
|
|
|
"model",
|
|
|
|
"experiment",
|
|
|
|
"model_serving_endpoint",
|
|
|
|
"registered_model",
|
|
|
|
"quality_monitor",
|
|
|
|
"schema",
|
|
|
|
}
|
|
|
|
|
2024-09-24 12:39:28 +00:00
|
|
|
func validateFileFormat(r *config.Root, filePath string) diag.Diagnostics {
|
|
|
|
for _, typ := range resourceTypes {
|
|
|
|
for _, ext := range []string{fmt.Sprintf(".%s.yml", typ), fmt.Sprintf(".%s.yaml", typ)} {
|
|
|
|
if strings.HasSuffix(filePath, ext) {
|
|
|
|
return validateSingleResourceDefined(r, ext, typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2024-09-19 09:41:40 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 12:39:28 +00:00
|
|
|
func validateSingleResourceDefined(r *config.Root, ext, typ string) diag.Diagnostics {
|
|
|
|
type resource struct {
|
|
|
|
path dyn.Path
|
|
|
|
value dyn.Value
|
|
|
|
typ string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := []resource{}
|
2024-09-19 09:41:40 +00:00
|
|
|
|
2024-09-24 12:39:28 +00:00
|
|
|
// Gather all resources defined in the resources block.
|
2024-09-19 09:41:40 +00:00
|
|
|
_, err := dyn.MapByPattern(
|
|
|
|
r.Value(),
|
|
|
|
dyn.NewPattern(dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey()),
|
|
|
|
func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
|
|
|
|
// The key for the resource. Eg: "my_job" for jobs.my_job.
|
|
|
|
k := p[2].Key()
|
|
|
|
// The type of the resource. Eg: "job" for jobs.my_job.
|
|
|
|
typ := strings.TrimSuffix(p[1].Key(), "s")
|
|
|
|
|
2024-09-24 12:39:28 +00:00
|
|
|
resources = append(resources, resource{path: p, value: v, typ: typ, key: k})
|
2024-09-19 09:41:40 +00:00
|
|
|
return v, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-09-24 12:39:28 +00:00
|
|
|
return diag.FromErr(err)
|
2024-09-19 09:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gather all resources defined in a target block.
|
|
|
|
_, err = dyn.MapByPattern(
|
|
|
|
r.Value(),
|
|
|
|
dyn.NewPattern(dyn.Key("targets"), dyn.AnyKey(), dyn.Key("resources"), dyn.AnyKey(), dyn.AnyKey()),
|
|
|
|
func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
|
2024-09-24 12:39:28 +00:00
|
|
|
// The key for the resource. Eg: "my_job" for jobs.my_job.
|
2024-09-19 09:41:40 +00:00
|
|
|
k := p[4].Key()
|
2024-09-24 12:39:28 +00:00
|
|
|
// The type of the resource. Eg: "job" for jobs.my_job.
|
2024-09-19 09:41:40 +00:00
|
|
|
typ := strings.TrimSuffix(p[3].Key(), "s")
|
|
|
|
|
2024-09-24 12:39:28 +00:00
|
|
|
resources = append(resources, resource{path: p, value: v, typ: typ, key: k})
|
2024-09-19 09:41:40 +00:00
|
|
|
return v, nil
|
2024-09-24 12:39:28 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return diag.FromErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
valid := true
|
|
|
|
seenKeys := map[string]struct{}{}
|
|
|
|
for _, rr := range resources {
|
|
|
|
if len(seenKeys) == 0 {
|
|
|
|
seenKeys[rr.key] = struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := seenKeys[rr.key]; !ok {
|
|
|
|
valid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if rr.typ != typ {
|
|
|
|
valid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file only contains one resource defined in its resources or targets block,
|
|
|
|
// and the resource is of the correct type.
|
|
|
|
if valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := strings.Builder{}
|
|
|
|
msg.WriteString(fmt.Sprintf("We recommend only defining a single %s when a file has the %s extension.", typ, ext))
|
|
|
|
msg.WriteString("The following resources are defined or configured in this file:\n")
|
|
|
|
for _, r := range resources {
|
|
|
|
msg.WriteString(fmt.Sprintf(" - %s (%s)\n", r.key, r.typ))
|
|
|
|
}
|
|
|
|
|
|
|
|
locations := []dyn.Location{}
|
|
|
|
paths := []dyn.Path{}
|
|
|
|
for _, rr := range resources {
|
|
|
|
locations = append(locations, rr.value.Locations()...)
|
|
|
|
paths = append(paths, rr.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return diag.Diagnostics{
|
|
|
|
{
|
|
|
|
Severity: diag.Info,
|
|
|
|
Summary: msg.String(),
|
|
|
|
Locations: locations,
|
|
|
|
Paths: paths,
|
2024-09-19 09:41:40 +00:00
|
|
|
},
|
2024-09-24 12:39:28 +00:00
|
|
|
}
|
2024-09-19 09:41:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 09:57:31 +00:00
|
|
|
type processInclude struct {
|
|
|
|
fullPath string
|
|
|
|
relPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessInclude loads the configuration at [fullPath] and merges it into the configuration.
|
2022-11-28 09:59:43 +00:00
|
|
|
func ProcessInclude(fullPath, relPath string) bundle.Mutator {
|
2022-11-18 09:57:31 +00:00
|
|
|
return &processInclude{
|
|
|
|
fullPath: fullPath,
|
|
|
|
relPath: relPath,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *processInclude) Name() string {
|
|
|
|
return fmt.Sprintf("ProcessInclude(%s)", m.relPath)
|
|
|
|
}
|
|
|
|
|
2024-03-25 14:18:47 +00:00
|
|
|
func (m *processInclude) Apply(_ context.Context, b *bundle.Bundle) diag.Diagnostics {
|
2024-03-28 10:59:03 +00:00
|
|
|
this, diags := config.Load(m.fullPath)
|
|
|
|
if diags.HasError() {
|
|
|
|
return diags
|
|
|
|
}
|
2024-09-19 09:41:40 +00:00
|
|
|
|
2024-09-24 12:39:28 +00:00
|
|
|
// Add any diagnostics associated with the file format.
|
|
|
|
diags = append(diags, validateFileFormat(this, m.relPath)...)
|
2024-09-19 09:41:40 +00:00
|
|
|
|
2024-03-28 10:59:03 +00:00
|
|
|
err := b.Config.Merge(this)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
2024-03-28 10:59:03 +00:00
|
|
|
diags = diags.Extend(diag.FromErr(err))
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
2024-03-28 10:59:03 +00:00
|
|
|
return diags
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|