2024-01-04 21:04:42 +00:00
|
|
|
package variable
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-20 21:46:07 +00:00
|
|
|
"fmt"
|
2024-01-04 21:04:42 +00:00
|
|
|
|
|
|
|
"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"`
|
|
|
|
|
2024-11-20 22:57:33 +00:00
|
|
|
NotificationDestination string `json:"notification_destination,omitempty"`
|
|
|
|
|
2024-01-04 21:04:42 +00:00
|
|
|
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
|
2024-01-04 21:04:42 +00:00
|
|
|
|
2024-11-20 21:46:07 +00:00
|
|
|
if l.Alert != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveAlert{name: l.Alert})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.ClusterPolicy != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveClusterPolicy{name: l.ClusterPolicy})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.Cluster != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveCluster{name: l.Cluster})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.Dashboard != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveDashboard{name: l.Dashboard})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.InstancePool != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveInstancePool{name: l.InstancePool})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.Job != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveJob{name: l.Job})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.Metastore != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveMetastore{name: l.Metastore})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
2024-11-20 22:57:33 +00:00
|
|
|
if l.NotificationDestination != "" {
|
|
|
|
resolvers = append(resolvers, resolveNotificationDestination{name: l.NotificationDestination})
|
|
|
|
}
|
2024-11-20 21:46:07 +00:00
|
|
|
if l.Pipeline != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolvePipeline{name: l.Pipeline})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.Query != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveQuery{name: l.Query})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.ServicePrincipal != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveServicePrincipal{name: l.ServicePrincipal})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
|
|
|
if l.Warehouse != "" {
|
2024-11-20 22:12:16 +00:00
|
|
|
resolvers = append(resolvers, resolveWarehouse{name: l.Warehouse})
|
2024-11-20 21:46:07 +00:00
|
|
|
}
|
2024-01-04 21:04:42 +00:00
|
|
|
|
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:46:07 +00:00
|
|
|
}
|
2024-01-04 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
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-01-04 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
2024-11-20 21:53:24 +00:00
|
|
|
return r.Resolve(ctx, w)
|
|
|
|
}
|
2024-01-04 21:04:42 +00:00
|
|
|
|
2024-11-20 21:53:24 +00:00
|
|
|
func (l *Lookup) String() string {
|
|
|
|
r, _ := l.constructResolver()
|
|
|
|
if r == nil {
|
|
|
|
return ""
|
2024-01-04 21:04:42 +00:00
|
|
|
}
|
|
|
|
|
2024-11-20 21:53:24 +00:00
|
|
|
return r.String()
|
2024-01-04 21:04:42 +00:00
|
|
|
}
|