address comments

This commit is contained in:
Shreyas Goenka 2024-09-26 17:47:07 +02:00
parent 4cc0dd1fa1
commit fec73db111
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
4 changed files with 41 additions and 38 deletions

View File

@ -13,23 +13,12 @@ import (
"github.com/databricks/cli/libs/dyn" "github.com/databricks/cli/libs/dyn"
) )
var resourceTypes = []string{
"job",
"pipeline",
"model",
"experiment",
"model_serving_endpoint",
"registered_model",
"quality_monitor",
"schema",
"cluster",
}
func validateFileFormat(configRoot dyn.Value, filePath string) diag.Diagnostics { func validateFileFormat(configRoot dyn.Value, filePath string) diag.Diagnostics {
for _, typ := range resourceTypes { for _, resourceDescription := range config.SupportedResources() {
for _, ext := range []string{fmt.Sprintf(".%s.yml", typ), fmt.Sprintf(".%s.yaml", typ)} { singularName := resourceDescription.SingularName
for _, ext := range []string{fmt.Sprintf(".%s.yml", singularName), fmt.Sprintf(".%s.yaml", singularName)} {
if strings.HasSuffix(filePath, ext) { if strings.HasSuffix(filePath, ext) {
return validateSingleResourceDefined(configRoot, ext, typ) return validateSingleResourceDefined(configRoot, ext, singularName)
} }
} }
} }
@ -46,6 +35,7 @@ func validateSingleResourceDefined(configRoot dyn.Value, ext, typ string) diag.D
} }
resources := []resource{} resources := []resource{}
supportedResources := config.SupportedResources()
// Gather all resources defined in the resources block. // Gather all resources defined in the resources block.
_, err := dyn.MapByPattern( _, err := dyn.MapByPattern(
@ -55,7 +45,7 @@ func validateSingleResourceDefined(configRoot dyn.Value, ext, typ string) diag.D
// The key for the resource. Eg: "my_job" for jobs.my_job. // The key for the resource. Eg: "my_job" for jobs.my_job.
k := p[2].Key() k := p[2].Key()
// The type of the resource. Eg: "job" for jobs.my_job. // The type of the resource. Eg: "job" for jobs.my_job.
typ := strings.TrimSuffix(p[1].Key(), "s") typ := supportedResources[p[1].Key()].SingularName
resources = append(resources, resource{path: p, value: v, typ: typ, key: k}) resources = append(resources, resource{path: p, value: v, typ: typ, key: k})
return v, nil return v, nil
@ -72,7 +62,7 @@ func validateSingleResourceDefined(configRoot dyn.Value, ext, typ string) diag.D
// The key for the resource. Eg: "my_job" for jobs.my_job. // The key for the resource. Eg: "my_job" for jobs.my_job.
k := p[4].Key() k := p[4].Key()
// The type of the resource. Eg: "job" for jobs.my_job. // The type of the resource. Eg: "job" for jobs.my_job.
typ := strings.TrimSuffix(p[3].Key(), "s") typ := supportedResources[p[3].Key()].SingularName
resources = append(resources, resource{path: p, value: v, typ: typ, key: k}) resources = append(resources, resource{path: p, value: v, typ: typ, key: k})
return v, nil return v, nil

View File

@ -3,8 +3,6 @@ package loader
import ( import (
"context" "context"
"path/filepath" "path/filepath"
"reflect"
"strings"
"testing" "testing"
"github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle"
@ -171,22 +169,3 @@ func TestProcessIncludeFormatFail(t *testing.T) {
}) })
} }
} }
func TestResourceNames(t *testing.T) {
names := []string{}
typ := reflect.TypeOf(config.Resources{})
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
jsonTags := strings.Split(field.Tag.Get("json"), ",")
singularName := strings.TrimSuffix(jsonTags[0], "s")
names = append(names, singularName)
}
// Assert the contents of the two lists are equal. Please add the singular
// name of your resource to resourceNames global if you are adding a new
// resource.
assert.Equal(t, len(resourceTypes), len(names))
for _, name := range names {
assert.Contains(t, resourceTypes, name)
}
}

View File

@ -59,3 +59,21 @@ func (r *Resources) FindResourceByConfigKey(key string) (ConfigResource, error)
return found[0], nil return found[0], nil
} }
type ResourceDescription struct {
SingularName string
}
func SupportedResources() map[string]ResourceDescription {
return map[string]ResourceDescription{
"jobs": {SingularName: "job"},
"pipelines": {SingularName: "pipeline"},
"models": {SingularName: "model"},
"experiments": {SingularName: "experiment"},
"model_serving_endpoints": {SingularName: "model_serving_endpoint"},
"registered_models": {SingularName: "registered_model"},
"quality_monitors": {SingularName: "quality_monitor"},
"schemas": {SingularName: "schema"},
"clusters": {SingularName: "cluster"},
}
}

View File

@ -3,6 +3,7 @@ package config
import ( import (
"encoding/json" "encoding/json"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -61,3 +62,18 @@ func TestCustomMarshallerIsImplemented(t *testing.T) {
}, "Resource %s does not have a custom unmarshaller", field.Name) }, "Resource %s does not have a custom unmarshaller", field.Name)
} }
} }
func TestSupportedResources(t *testing.T) {
expected := map[string]ResourceDescription{}
typ := reflect.TypeOf(Resources{})
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
jsonTags := strings.Split(field.Tag.Get("json"), ",")
singularName := strings.TrimSuffix(jsonTags[0], "s")
expected[jsonTags[0]] = ResourceDescription{SingularName: singularName}
}
// Please add your resource to the SupportedResources() function in resources.go
// if you are adding a new resource.
assert.Equal(t, expected, SupportedResources())
}