databricks-cli/bundle/config/variable/lookup.go

111 lines
2.7 KiB
Go
Raw Normal View History

package variable
import (
"context"
"fmt"
"github.com/databricks/databricks-sdk-go"
)
type Lookup struct {
Alert string `json:"alert,omitempty"`
ClusterPolicy string `json:"cluster_policy,omitempty"`
Cluster string `json:"cluster,omitempty"`
Dashboard string `json:"dashboard,omitempty"`
InstancePool string `json:"instance_pool,omitempty"`
Job string `json:"job,omitempty"`
Metastore string `json:"metastore,omitempty"`
NotificationDestination string `json:"notification_destination,omitempty"`
Pipeline string `json:"pipeline,omitempty"`
Query string `json:"query,omitempty"`
ServicePrincipal string `json:"service_principal,omitempty"`
Warehouse string `json:"warehouse,omitempty"`
}
2024-11-20 22:24:03 +00:00
type resolver interface {
// Resolve resolves the underlying entity's ID.
Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error)
// String returns a human-readable representation of the resolver.
String() string
}
2024-11-20 21:40:43 +00:00
func (l *Lookup) constructResolver() (resolver, error) {
2024-11-20 21:53:24 +00:00
var resolvers []resolver
if l.Alert != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveAlert{name: l.Alert})
}
if l.ClusterPolicy != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveClusterPolicy{name: l.ClusterPolicy})
}
if l.Cluster != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveCluster{name: l.Cluster})
}
if l.Dashboard != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveDashboard{name: l.Dashboard})
}
if l.InstancePool != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveInstancePool{name: l.InstancePool})
}
if l.Job != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveJob{name: l.Job})
}
if l.Metastore != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveMetastore{name: l.Metastore})
}
if l.NotificationDestination != "" {
resolvers = append(resolvers, resolveNotificationDestination{name: l.NotificationDestination})
}
if l.Pipeline != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolvePipeline{name: l.Pipeline})
}
if l.Query != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveQuery{name: l.Query})
}
if l.ServicePrincipal != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveServicePrincipal{name: l.ServicePrincipal})
}
if l.Warehouse != "" {
2024-11-20 22:12:16 +00:00
resolvers = append(resolvers, resolveWarehouse{name: l.Warehouse})
}
2024-11-20 21:53:24 +00:00
switch len(resolvers) {
case 0:
return nil, fmt.Errorf("no valid lookup fields provided")
case 1:
return resolvers[0], nil
default:
return nil, fmt.Errorf("exactly one lookup field must be provided")
}
}
2024-11-20 21:53:24 +00:00
func (l *Lookup) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
r, err := l.constructResolver()
if err != nil {
return "", err
}
2024-11-20 21:53:24 +00:00
return r.Resolve(ctx, w)
}
2024-11-20 21:53:24 +00:00
func (l *Lookup) String() string {
r, _ := l.constructResolver()
if r == nil {
return ""
}
2024-11-20 21:53:24 +00:00
return r.String()
}