@@ -54,6 +54,39 @@ func IsUnityCatalogCompatible(cluster *compute.ClusterDetails, minVersion string
5454
5555var ErrNoCompatibleClusters = errors .New ("no compatible clusters found" )
5656
57+ type compatibleCluster struct {
58+ compute.ClusterDetails
59+ versionName string
60+ }
61+
62+ func (v compatibleCluster ) Access () string {
63+ switch v .DataSecurityMode {
64+ case compute .DataSecurityModeUserIsolation :
65+ return "Shared"
66+ case compute .DataSecurityModeSingleUser :
67+ return "Assigned"
68+ default :
69+ return "Unknown"
70+ }
71+ }
72+
73+ func (v compatibleCluster ) Runtime () string {
74+ runtime , _ , _ := strings .Cut (v .versionName , " (" )
75+ return runtime
76+ }
77+
78+ func (v compatibleCluster ) State () string {
79+ state := v .ClusterDetails .State
80+ switch state {
81+ case compute .StateRunning , compute .StateResizing :
82+ return color .GreenString (state .String ())
83+ case compute .StateError , compute .StateTerminated , compute .StateTerminating , compute .StateUnknown :
84+ return color .RedString (state .String ())
85+ default :
86+ return color .BlueString (state .String ())
87+ }
88+ }
89+
5790func AskForCompatibleCluster (ctx context.Context , w * databricks.WorkspaceClient , minVersion string ) (string , error ) {
5891 all , err := w .Clusters .ListAll (ctx , compute.ListClustersRequest {
5992 CanUseClient : "NOTEBOOKS" ,
@@ -73,13 +106,6 @@ func AskForCompatibleCluster(ctx context.Context, w *databricks.WorkspaceClient,
73106 for _ , v := range sv .Versions {
74107 versions [v .Key ] = v .Name
75108 }
76- type compatibleCluster struct {
77- Id string
78- Name string
79- Access string
80- State string
81- Runtime string
82- }
83109 var compatible []compatibleCluster
84110 for _ , v := range all {
85111 if ! IsUnityCatalogCompatible (& v , minVersion ) {
@@ -88,48 +114,34 @@ func AskForCompatibleCluster(ctx context.Context, w *databricks.WorkspaceClient,
88114 if v .SingleUserName != "" && v .SingleUserName != me .UserName {
89115 continue
90116 }
91- var cluster compatibleCluster
92- switch v .DataSecurityMode {
93- case compute .DataSecurityModeUserIsolation :
94- cluster .Access = "Shared"
95- case compute .DataSecurityModeSingleUser :
96- cluster .Access = "Assigned"
97- }
98- switch v .State {
99- case compute .StateRunning , compute .StateResizing :
100- cluster .State = color .GreenString (v .State .String ())
101- case compute .StateError , compute .StateTerminated , compute .StateTerminating , compute .StateUnknown :
102- cluster .State = color .RedString (v .State .String ())
103- default :
104- cluster .State = color .BlueString (v .State .String ())
105- }
106- runtime , _ , _ := strings .Cut (versions [v .SparkVersion ], " (" )
107- cluster .Runtime = runtime
108- compatible = append (compatible , cluster )
117+ compatible = append (compatible , compatibleCluster {
118+ ClusterDetails : v ,
119+ versionName : versions [v .SparkVersion ],
120+ })
109121 }
110122 if len (compatible ) == 0 {
111123 return "" , ErrNoCompatibleClusters
112124 }
113125 if len (compatible ) == 1 {
114- return compatible [0 ].Id , nil
126+ return compatible [0 ].ClusterId , nil
115127 }
116128 i , _ , err := cmdio .RunSelect (ctx , & promptui.Select {
117129 Label : "Choose compatible cluster" ,
118130 Items : compatible ,
119131 Searcher : func (input string , idx int ) bool {
120- lower := strings .ToLower (compatible [idx ].Name )
132+ lower := strings .ToLower (compatible [idx ].ClusterName )
121133 return strings .Contains (lower , input )
122134 },
123135 StartInSearchMode : true ,
124136 Templates : & promptui.SelectTemplates {
125- Label : "{{ . | faint }}" ,
126- Active : `{{.Name | bold}} ({{.State}} {{.Access}} Runtime {{.Runtime}})` ,
127- Inactive : `{{.Name}} ` ,
128- Selected : `{{ "Configured cluster" | faint }}: {{ .Name | bold }} ({{.Id | faint}})` ,
137+ Label : "{{.ClusterName | faint}}" ,
138+ Active : `{{.ClusterName | bold}} ({{.State}} {{.Access}} Runtime {{.Runtime}})` ,
139+ Inactive : `{{.ClusterName}} ({{.State}} {{.Access}} Runtime {{.Runtime}}) ` ,
140+ Selected : `{{ "Configured cluster" | faint }}: {{ .ClusterName | bold }} ({{.ClusterId | faint}})` ,
129141 },
130142 })
131143 if err != nil {
132144 return "" , err
133145 }
134- return compatible [i ].Id , nil
146+ return compatible [i ].ClusterId , nil
135147}
0 commit comments