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

48 lines
1.3 KiB
Go
Raw Normal View History

package variable
import (
"context"
"fmt"
"github.com/databricks/databricks-sdk-go"
"github.com/databricks/databricks-sdk-go/service/compute"
)
2024-11-20 22:12:16 +00:00
type resolveCluster struct {
2024-11-20 21:40:33 +00:00
name string
}
// We added a custom resolver for the cluster to add filtering for the cluster source when we list all clusters.
// Without the filtering listing could take a very long time (5-10 mins) which leads to lookup timeouts.
2024-11-20 22:12:16 +00:00
func (l resolveCluster) Resolve(ctx context.Context, w *databricks.WorkspaceClient) (string, error) {
result, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{
FilterBy: &compute.ListClustersFilterBy{
ClusterSources: []compute.ClusterSource{compute.ClusterSourceApi, compute.ClusterSourceUi},
},
})
if err != nil {
return "", err
}
tmp := map[string][]compute.ClusterDetails{}
for _, v := range result {
key := v.ClusterName
tmp[key] = append(tmp[key], v)
}
2024-11-20 21:40:33 +00:00
name := l.name
alternatives, ok := tmp[name]
if !ok || len(alternatives) == 0 {
return "", fmt.Errorf("cluster named '%s' does not exist", name)
}
if len(alternatives) > 1 {
return "", fmt.Errorf("there are %d instances of clusters named '%s'", len(alternatives), name)
}
return alternatives[0].ClusterId, nil
2024-11-20 21:40:33 +00:00
}
2024-11-20 22:12:16 +00:00
func (l resolveCluster) String() string {
2024-11-20 21:40:33 +00:00
return fmt.Sprintf("cluster: %s", l.name)
}