2022-11-18 09:57:31 +00:00
|
|
|
package config
|
|
|
|
|
2022-12-15 12:00:41 +00:00
|
|
|
import (
|
2024-02-14 18:04:45 +00:00
|
|
|
"context"
|
2023-04-17 10:21:21 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-05-16 16:35:39 +00:00
|
|
|
"github.com/databricks/cli/bundle/config/resources"
|
2024-02-14 18:04:45 +00:00
|
|
|
"github.com/databricks/databricks-sdk-go"
|
2022-12-15 12:00:41 +00:00
|
|
|
)
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// Resources defines Databricks resources associated with the bundle.
|
|
|
|
type Resources struct {
|
2022-12-15 12:00:41 +00:00
|
|
|
Jobs map[string]*resources.Job `json:"jobs,omitempty"`
|
|
|
|
Pipelines map[string]*resources.Pipeline `json:"pipelines,omitempty"`
|
2023-03-20 20:28:43 +00:00
|
|
|
|
2023-09-07 21:54:31 +00:00
|
|
|
Models map[string]*resources.MlflowModel `json:"models,omitempty"`
|
|
|
|
Experiments map[string]*resources.MlflowExperiment `json:"experiments,omitempty"`
|
|
|
|
ModelServingEndpoints map[string]*resources.ModelServingEndpoint `json:"model_serving_endpoints,omitempty"`
|
2023-10-16 15:32:49 +00:00
|
|
|
RegisteredModels map[string]*resources.RegisteredModel `json:"registered_models,omitempty"`
|
2024-05-31 09:42:25 +00:00
|
|
|
QualityMonitors map[string]*resources.QualityMonitor `json:"quality_monitors,omitempty"`
|
2024-07-31 12:16:28 +00:00
|
|
|
Schemas map[string]*resources.Schema `json:"schemas,omitempty"`
|
2024-09-23 10:42:34 +00:00
|
|
|
Clusters map[string]*resources.Cluster `json:"clusters,omitempty"`
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
2023-04-12 14:17:13 +00:00
|
|
|
|
2024-02-14 18:04:45 +00:00
|
|
|
type ConfigResource interface {
|
2024-08-13 12:50:15 +00:00
|
|
|
// Function to assert if the resource exists in the workspace configured in
|
|
|
|
// the input workspace client.
|
2024-02-14 18:04:45 +00:00
|
|
|
Exists(ctx context.Context, w *databricks.WorkspaceClient, id string) (bool, error)
|
2024-08-13 12:50:15 +00:00
|
|
|
|
|
|
|
// Terraform equivalent name of the resource. For example "databricks_job"
|
|
|
|
// for jobs and "databricks_pipeline" for pipelines.
|
2024-02-14 18:04:45 +00:00
|
|
|
TerraformResourceName() string
|
2024-08-24 18:48:36 +00:00
|
|
|
|
|
|
|
// GetName returns the in-product name of the resource.
|
|
|
|
GetName() string
|
|
|
|
|
|
|
|
// GetURL returns the URL of the resource.
|
|
|
|
GetURL() string
|
|
|
|
|
|
|
|
// InitializeURL initializes the URL field of the resource.
|
|
|
|
InitializeURL(urlPrefix string, urlSuffix string)
|
|
|
|
}
|
|
|
|
|
2024-10-17 14:39:17 +00:00
|
|
|
// ResourceGroup represents a group of resources of the same type.
|
|
|
|
// It includes a description of the resource type and a map of resources.
|
|
|
|
type ResourceGroup struct {
|
|
|
|
Description ResourceDescription
|
|
|
|
Resources map[string]ConfigResource
|
|
|
|
}
|
2024-08-24 18:48:36 +00:00
|
|
|
|
2024-10-17 14:39:17 +00:00
|
|
|
// collectResourceMap collects resources of a specific type into a ResourceGroup.
|
|
|
|
func collectResourceMap[T ConfigResource](
|
|
|
|
description ResourceDescription,
|
|
|
|
input map[string]T,
|
|
|
|
) ResourceGroup {
|
|
|
|
resources := make(map[string]ConfigResource)
|
|
|
|
for key, resource := range input {
|
|
|
|
resources[key] = resource
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|
2024-10-17 14:39:17 +00:00
|
|
|
return ResourceGroup{
|
|
|
|
Description: description,
|
|
|
|
Resources: resources,
|
2024-08-24 18:48:36 +00:00
|
|
|
}
|
2024-10-17 14:39:17 +00:00
|
|
|
}
|
2024-08-24 18:48:36 +00:00
|
|
|
|
2024-10-17 14:39:17 +00:00
|
|
|
// AllResources returns all resources in the bundle grouped by their resource type.
|
|
|
|
func (r *Resources) AllResources() []ResourceGroup {
|
|
|
|
descriptions := SupportedResources()
|
|
|
|
return []ResourceGroup{
|
|
|
|
collectResourceMap(descriptions["jobs"], r.Jobs),
|
|
|
|
collectResourceMap(descriptions["pipelines"], r.Pipelines),
|
|
|
|
collectResourceMap(descriptions["models"], r.Models),
|
|
|
|
collectResourceMap(descriptions["experiments"], r.Experiments),
|
|
|
|
collectResourceMap(descriptions["model_serving_endpoints"], r.ModelServingEndpoints),
|
|
|
|
collectResourceMap(descriptions["registered_models"], r.RegisteredModels),
|
|
|
|
collectResourceMap(descriptions["quality_monitors"], r.QualityMonitors),
|
|
|
|
collectResourceMap(descriptions["schemas"], r.Schemas),
|
|
|
|
collectResourceMap(descriptions["clusters"], r.Clusters),
|
2024-10-05 04:03:05 +00:00
|
|
|
}
|
2024-02-14 18:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resources) FindResourceByConfigKey(key string) (ConfigResource, error) {
|
|
|
|
found := make([]ConfigResource, 0)
|
|
|
|
for k := range r.Jobs {
|
|
|
|
if k == key {
|
|
|
|
found = append(found, r.Jobs[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k := range r.Pipelines {
|
|
|
|
if k == key {
|
|
|
|
found = append(found, r.Pipelines[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(found) == 0 {
|
|
|
|
return nil, fmt.Errorf("no such resource: %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(found) > 1 {
|
|
|
|
keys := make([]string, 0, len(found))
|
|
|
|
for _, r := range found {
|
|
|
|
keys = append(keys, fmt.Sprintf("%s:%s", r.TerraformResourceName(), key))
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("ambiguous: %s (can resolve to all of %s)", key, keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
return found[0], nil
|
|
|
|
}
|
2024-10-07 09:16:20 +00:00
|
|
|
|
|
|
|
type ResourceDescription struct {
|
2024-10-17 13:58:04 +00:00
|
|
|
// Singular and plural name when used to refer to the configuration.
|
2024-10-07 09:16:20 +00:00
|
|
|
SingularName string
|
2024-10-17 13:58:04 +00:00
|
|
|
PluralName string
|
|
|
|
|
|
|
|
// Singular and plural title when used in summaries / terminal UI.
|
|
|
|
SingularTitle string
|
|
|
|
PluralTitle string
|
2024-10-07 09:16:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The keys of the map corresponds to the resource key in the bundle configuration.
|
|
|
|
func SupportedResources() map[string]ResourceDescription {
|
|
|
|
return map[string]ResourceDescription{
|
2024-10-17 13:58:04 +00:00
|
|
|
"jobs": {
|
|
|
|
SingularName: "job",
|
|
|
|
PluralName: "jobs",
|
|
|
|
SingularTitle: "Job",
|
|
|
|
PluralTitle: "Jobs",
|
|
|
|
},
|
|
|
|
"pipelines": {
|
|
|
|
SingularName: "pipeline",
|
|
|
|
PluralName: "pipelines",
|
|
|
|
SingularTitle: "Pipeline",
|
|
|
|
PluralTitle: "Pipelines",
|
|
|
|
},
|
|
|
|
"models": {
|
|
|
|
SingularName: "model",
|
|
|
|
PluralName: "models",
|
|
|
|
SingularTitle: "Model",
|
|
|
|
PluralTitle: "Models",
|
|
|
|
},
|
|
|
|
"experiments": {
|
|
|
|
SingularName: "experiment",
|
|
|
|
PluralName: "experiments",
|
|
|
|
SingularTitle: "Experiment",
|
|
|
|
PluralTitle: "Experiments",
|
|
|
|
},
|
|
|
|
"model_serving_endpoints": {
|
|
|
|
SingularName: "model_serving_endpoint",
|
|
|
|
PluralName: "model_serving_endpoints",
|
|
|
|
SingularTitle: "Model Serving Endpoint",
|
|
|
|
PluralTitle: "Model Serving Endpoints",
|
|
|
|
},
|
|
|
|
"registered_models": {
|
|
|
|
SingularName: "registered_model",
|
|
|
|
PluralName: "registered_models",
|
|
|
|
SingularTitle: "Registered Model",
|
|
|
|
PluralTitle: "Registered Models",
|
|
|
|
},
|
|
|
|
"quality_monitors": {
|
|
|
|
SingularName: "quality_monitor",
|
|
|
|
PluralName: "quality_monitors",
|
|
|
|
SingularTitle: "Quality Monitor",
|
|
|
|
PluralTitle: "Quality Monitors",
|
|
|
|
},
|
|
|
|
"schemas": {
|
|
|
|
SingularName: "schema",
|
|
|
|
PluralName: "schemas",
|
|
|
|
SingularTitle: "Schema",
|
|
|
|
PluralTitle: "Schemas",
|
|
|
|
},
|
|
|
|
"clusters": {
|
|
|
|
SingularName: "cluster",
|
|
|
|
PluralName: "clusters",
|
|
|
|
SingularTitle: "Cluster",
|
|
|
|
PluralTitle: "Clusters",
|
|
|
|
},
|
2024-10-07 09:16:20 +00:00
|
|
|
}
|
|
|
|
}
|