-
Notifications
You must be signed in to change notification settings - Fork 37
/
api.go
executable file
·1906 lines (1758 loc) · 69.1 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT.
// These APIs allow you to manage Cluster Policies, Clusters, Command Execution, Global Init Scripts, Instance Pools, Instance Profiles, Libraries, Policy Families, etc.
package compute
import (
"context"
"fmt"
"time"
"github.com/databricks/databricks-sdk-go/client"
"github.com/databricks/databricks-sdk-go/retries"
"github.com/databricks/databricks-sdk-go/useragent"
)
func NewClusterPolicies(client *client.DatabricksClient) *ClusterPoliciesAPI {
return &ClusterPoliciesAPI{
impl: &clusterPoliciesImpl{
client: client,
},
}
}
// Cluster policy limits the ability to configure clusters based on a set of
// rules. The policy rules limit the attributes or attribute values available
// for cluster creation. Cluster policies have ACLs that limit their use to
// specific users and groups.
//
// Cluster policies let you limit users to create clusters with prescribed
// settings, simplify the user interface and enable more users to create their
// own clusters (by fixing and hiding some values), control cost by limiting per
// cluster maximum cost (by setting limits on attributes whose values contribute
// to hourly price).
//
// Cluster policy permissions limit which policies a user can select in the
// Policy drop-down when the user creates a cluster: - A user who has cluster
// create permission can select the Unrestricted policy and create
// fully-configurable clusters. - A user who has both cluster create permission
// and access to cluster policies can select the Unrestricted policy and
// policies they have access to. - A user that has access to only cluster
// policies, can select the policies they have access to.
//
// If no policies have been created in the workspace, the Policy drop-down does
// not display.
//
// Only admin users can create, edit, and delete policies. Admin users also have
// access to all policies.
type ClusterPoliciesAPI struct {
// impl contains low-level REST API interface, that could be overridden
// through WithImpl(ClusterPoliciesService)
impl ClusterPoliciesService
}
// WithImpl could be used to override low-level API implementations for unit
// testing purposes with [github.com/golang/mock] or other mocking frameworks.
func (a *ClusterPoliciesAPI) WithImpl(impl ClusterPoliciesService) *ClusterPoliciesAPI {
a.impl = impl
return a
}
// Impl returns low-level ClusterPolicies API implementation
func (a *ClusterPoliciesAPI) Impl() ClusterPoliciesService {
return a.impl
}
// Create a new policy.
//
// Creates a new policy with prescribed settings.
func (a *ClusterPoliciesAPI) Create(ctx context.Context, request CreatePolicy) (*CreatePolicyResponse, error) {
return a.impl.Create(ctx, request)
}
// Delete a cluster policy.
//
// Delete a policy for a cluster. Clusters governed by this policy can still
// run, but cannot be edited.
func (a *ClusterPoliciesAPI) Delete(ctx context.Context, request DeletePolicy) error {
return a.impl.Delete(ctx, request)
}
// Delete a cluster policy.
//
// Delete a policy for a cluster. Clusters governed by this policy can still
// run, but cannot be edited.
func (a *ClusterPoliciesAPI) DeleteByPolicyId(ctx context.Context, policyId string) error {
return a.impl.Delete(ctx, DeletePolicy{
PolicyId: policyId,
})
}
// Update a cluster policy.
//
// Update an existing policy for cluster. This operation may make some clusters
// governed by the previous policy invalid.
func (a *ClusterPoliciesAPI) Edit(ctx context.Context, request EditPolicy) error {
return a.impl.Edit(ctx, request)
}
// Get entity.
//
// Get a cluster policy entity. Creation and editing is available to admins
// only.
func (a *ClusterPoliciesAPI) Get(ctx context.Context, request GetClusterPolicyRequest) (*Policy, error) {
return a.impl.Get(ctx, request)
}
// Get entity.
//
// Get a cluster policy entity. Creation and editing is available to admins
// only.
func (a *ClusterPoliciesAPI) GetByPolicyId(ctx context.Context, policyId string) (*Policy, error) {
return a.impl.Get(ctx, GetClusterPolicyRequest{
PolicyId: policyId,
})
}
// Get a cluster policy.
//
// Returns a list of policies accessible by the requesting user.
//
// This method is generated by Databricks SDK Code Generator.
func (a *ClusterPoliciesAPI) ListAll(ctx context.Context, request ListClusterPoliciesRequest) ([]Policy, error) {
response, err := a.impl.List(ctx, request)
if err != nil {
return nil, err
}
return response.Policies, nil
}
// PolicyNameToPolicyIdMap calls [ClusterPoliciesAPI.ListAll] and creates a map of results with [Policy].Name as key and [Policy].PolicyId as value.
//
// Returns an error if there's more than one [Policy] with the same .Name.
//
// Note: All [Policy] instances are loaded into memory before creating a map.
//
// This method is generated by Databricks SDK Code Generator.
func (a *ClusterPoliciesAPI) PolicyNameToPolicyIdMap(ctx context.Context, request ListClusterPoliciesRequest) (map[string]string, error) {
ctx = useragent.InContext(ctx, "sdk-feature", "name-to-id")
mapping := map[string]string{}
result, err := a.ListAll(ctx, request)
if err != nil {
return nil, err
}
for _, v := range result {
key := v.Name
_, duplicate := mapping[key]
if duplicate {
return nil, fmt.Errorf("duplicate .Name: %s", key)
}
mapping[key] = v.PolicyId
}
return mapping, nil
}
// GetByName calls [ClusterPoliciesAPI.PolicyNameToPolicyIdMap] and returns a single [Policy].
//
// Returns an error if there's more than one [Policy] with the same .Name.
//
// Note: All [Policy] instances are loaded into memory before returning matching by name.
//
// This method is generated by Databricks SDK Code Generator.
func (a *ClusterPoliciesAPI) GetByName(ctx context.Context, name string) (*Policy, error) {
ctx = useragent.InContext(ctx, "sdk-feature", "get-by-name")
result, err := a.ListAll(ctx, ListClusterPoliciesRequest{})
if err != nil {
return nil, err
}
tmp := map[string][]Policy{}
for _, v := range result {
key := v.Name
tmp[key] = append(tmp[key], v)
}
alternatives, ok := tmp[name]
if !ok || len(alternatives) == 0 {
return nil, fmt.Errorf("Policy named '%s' does not exist", name)
}
if len(alternatives) > 1 {
return nil, fmt.Errorf("there are %d instances of Policy named '%s'", len(alternatives), name)
}
return &alternatives[0], nil
}
func NewClusters(client *client.DatabricksClient) *ClustersAPI {
return &ClustersAPI{
impl: &clustersImpl{
client: client,
},
}
}
// The Clusters API allows you to create, start, edit, list, terminate, and
// delete clusters.
//
// Databricks maps cluster node instance types to compute units known as DBUs.
// See the instance type pricing page for a list of the supported instance types
// and their corresponding DBUs.
//
// A Databricks cluster is a set of computation resources and configurations on
// which you run data engineering, data science, and data analytics workloads,
// such as production ETL pipelines, streaming analytics, ad-hoc analytics, and
// machine learning.
//
// You run these workloads as a set of commands in a notebook or as an automated
// job. Databricks makes a distinction between all-purpose clusters and job
// clusters. You use all-purpose clusters to analyze data collaboratively using
// interactive notebooks. You use job clusters to run fast and robust automated
// jobs.
//
// You can create an all-purpose cluster using the UI, CLI, or REST API. You can
// manually terminate and restart an all-purpose cluster. Multiple users can
// share such clusters to do collaborative interactive analysis.
//
// IMPORTANT: Databricks retains cluster configuration information for up to 200
// all-purpose clusters terminated in the last 30 days and up to 30 job clusters
// recently terminated by the job scheduler. To keep an all-purpose cluster
// configuration even after it has been terminated for more than 30 days, an
// administrator can pin a cluster to the cluster list.
type ClustersAPI struct {
// impl contains low-level REST API interface, that could be overridden
// through WithImpl(ClustersService)
impl ClustersService
}
// WithImpl could be used to override low-level API implementations for unit
// testing purposes with [github.com/golang/mock] or other mocking frameworks.
func (a *ClustersAPI) WithImpl(impl ClustersService) *ClustersAPI {
a.impl = impl
return a
}
// Impl returns low-level Clusters API implementation
func (a *ClustersAPI) Impl() ClustersService {
return a.impl
}
// WaitGetClusterRunning repeatedly calls [ClustersAPI.Get] and waits to reach RUNNING state
func (a *ClustersAPI) WaitGetClusterRunning(ctx context.Context, clusterId string,
timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
ctx = useragent.InContext(ctx, "sdk-feature", "long-running")
return retries.Poll[ClusterDetails](ctx, timeout, func() (*ClusterDetails, *retries.Err) {
clusterDetails, err := a.Get(ctx, GetClusterRequest{
ClusterId: clusterId,
})
if err != nil {
return nil, retries.Halt(err)
}
if callback != nil {
callback(clusterDetails)
}
status := clusterDetails.State
statusMessage := clusterDetails.StateMessage
switch status {
case StateRunning: // target state
return clusterDetails, nil
case StateError, StateTerminated:
err := fmt.Errorf("failed to reach %s, got %s: %s",
StateRunning, status, statusMessage)
return nil, retries.Halt(err)
default:
return nil, retries.Continues(statusMessage)
}
})
}
// WaitGetClusterRunning is a wrapper that calls [ClustersAPI.WaitGetClusterRunning] and waits to reach RUNNING state.
type WaitGetClusterRunning[R any] struct {
Response *R
ClusterId string `json:"cluster_id"`
poll func(time.Duration, func(*ClusterDetails)) (*ClusterDetails, error)
callback func(*ClusterDetails)
timeout time.Duration
}
// OnProgress invokes a callback every time it polls for the status update.
func (w *WaitGetClusterRunning[R]) OnProgress(callback func(*ClusterDetails)) *WaitGetClusterRunning[R] {
w.callback = callback
return w
}
// Get the ClusterDetails with the default timeout of 20 minutes.
func (w *WaitGetClusterRunning[R]) Get() (*ClusterDetails, error) {
return w.poll(w.timeout, w.callback)
}
// Get the ClusterDetails with custom timeout.
func (w *WaitGetClusterRunning[R]) GetWithTimeout(timeout time.Duration) (*ClusterDetails, error) {
return w.poll(timeout, w.callback)
}
// WaitGetClusterTerminated repeatedly calls [ClustersAPI.Get] and waits to reach TERMINATED state
func (a *ClustersAPI) WaitGetClusterTerminated(ctx context.Context, clusterId string,
timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
ctx = useragent.InContext(ctx, "sdk-feature", "long-running")
return retries.Poll[ClusterDetails](ctx, timeout, func() (*ClusterDetails, *retries.Err) {
clusterDetails, err := a.Get(ctx, GetClusterRequest{
ClusterId: clusterId,
})
if err != nil {
return nil, retries.Halt(err)
}
if callback != nil {
callback(clusterDetails)
}
status := clusterDetails.State
statusMessage := clusterDetails.StateMessage
switch status {
case StateTerminated: // target state
return clusterDetails, nil
case StateError:
err := fmt.Errorf("failed to reach %s, got %s: %s",
StateTerminated, status, statusMessage)
return nil, retries.Halt(err)
default:
return nil, retries.Continues(statusMessage)
}
})
}
// WaitGetClusterTerminated is a wrapper that calls [ClustersAPI.WaitGetClusterTerminated] and waits to reach TERMINATED state.
type WaitGetClusterTerminated[R any] struct {
Response *R
ClusterId string `json:"cluster_id"`
poll func(time.Duration, func(*ClusterDetails)) (*ClusterDetails, error)
callback func(*ClusterDetails)
timeout time.Duration
}
// OnProgress invokes a callback every time it polls for the status update.
func (w *WaitGetClusterTerminated[R]) OnProgress(callback func(*ClusterDetails)) *WaitGetClusterTerminated[R] {
w.callback = callback
return w
}
// Get the ClusterDetails with the default timeout of 20 minutes.
func (w *WaitGetClusterTerminated[R]) Get() (*ClusterDetails, error) {
return w.poll(w.timeout, w.callback)
}
// Get the ClusterDetails with custom timeout.
func (w *WaitGetClusterTerminated[R]) GetWithTimeout(timeout time.Duration) (*ClusterDetails, error) {
return w.poll(timeout, w.callback)
}
// Change cluster owner.
//
// Change the owner of the cluster. You must be an admin to perform this
// operation.
func (a *ClustersAPI) ChangeOwner(ctx context.Context, request ChangeClusterOwner) error {
return a.impl.ChangeOwner(ctx, request)
}
// Create new cluster.
//
// Creates a new Spark cluster. This method will acquire new instances from the
// cloud provider if necessary. Note: Databricks may not be able to acquire some
// of the requested nodes, due to cloud provider limitations (account limits,
// spot price, etc.) or transient network issues.
//
// If Databricks acquires at least 85% of the requested on-demand nodes, cluster
// creation will succeed. Otherwise the cluster will terminate with an
// informative error message.
func (a *ClustersAPI) Create(ctx context.Context, createCluster CreateCluster) (*WaitGetClusterRunning[CreateClusterResponse], error) {
createClusterResponse, err := a.impl.Create(ctx, createCluster)
if err != nil {
return nil, err
}
return &WaitGetClusterRunning[CreateClusterResponse]{
Response: createClusterResponse,
ClusterId: createClusterResponse.ClusterId,
poll: func(timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
return a.WaitGetClusterRunning(ctx, createClusterResponse.ClusterId, timeout, callback)
},
timeout: 20 * time.Minute,
callback: nil,
}, nil
}
// Calls [ClustersAPI.Create] and waits to reach RUNNING state
//
// You can override the default timeout of 20 minutes by calling adding
// retries.Timeout[ClusterDetails](60*time.Minute) functional option.
//
// Deprecated: use [ClustersAPI.Create].Get() or [ClustersAPI.WaitGetClusterRunning]
func (a *ClustersAPI) CreateAndWait(ctx context.Context, createCluster CreateCluster, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
wait, err := a.Create(ctx, createCluster)
if err != nil {
return nil, err
}
tmp := &retries.Info[ClusterDetails]{Timeout: 20 * time.Minute}
for _, o := range options {
o(tmp)
}
wait.timeout = tmp.Timeout
wait.callback = func(info *ClusterDetails) {
for _, o := range options {
o(&retries.Info[ClusterDetails]{
Info: info,
Timeout: wait.timeout,
})
}
}
return wait.Get()
}
// Terminate cluster.
//
// Terminates the Spark cluster with the specified ID. The cluster is removed
// asynchronously. Once the termination has completed, the cluster will be in a
// `TERMINATED` state. If the cluster is already in a `TERMINATING` or
// `TERMINATED` state, nothing will happen.
func (a *ClustersAPI) Delete(ctx context.Context, deleteCluster DeleteCluster) (*WaitGetClusterTerminated[any], error) {
err := a.impl.Delete(ctx, deleteCluster)
if err != nil {
return nil, err
}
return &WaitGetClusterTerminated[any]{
ClusterId: deleteCluster.ClusterId,
poll: func(timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
return a.WaitGetClusterTerminated(ctx, deleteCluster.ClusterId, timeout, callback)
},
timeout: 20 * time.Minute,
callback: nil,
}, nil
}
// Calls [ClustersAPI.Delete] and waits to reach TERMINATED state
//
// You can override the default timeout of 20 minutes by calling adding
// retries.Timeout[ClusterDetails](60*time.Minute) functional option.
//
// Deprecated: use [ClustersAPI.Delete].Get() or [ClustersAPI.WaitGetClusterTerminated]
func (a *ClustersAPI) DeleteAndWait(ctx context.Context, deleteCluster DeleteCluster, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
wait, err := a.Delete(ctx, deleteCluster)
if err != nil {
return nil, err
}
tmp := &retries.Info[ClusterDetails]{Timeout: 20 * time.Minute}
for _, o := range options {
o(tmp)
}
wait.timeout = tmp.Timeout
wait.callback = func(info *ClusterDetails) {
for _, o := range options {
o(&retries.Info[ClusterDetails]{
Info: info,
Timeout: wait.timeout,
})
}
}
return wait.Get()
}
// Terminate cluster.
//
// Terminates the Spark cluster with the specified ID. The cluster is removed
// asynchronously. Once the termination has completed, the cluster will be in a
// `TERMINATED` state. If the cluster is already in a `TERMINATING` or
// `TERMINATED` state, nothing will happen.
func (a *ClustersAPI) DeleteByClusterId(ctx context.Context, clusterId string) error {
return a.impl.Delete(ctx, DeleteCluster{
ClusterId: clusterId,
})
}
func (a *ClustersAPI) DeleteByClusterIdAndWait(ctx context.Context, clusterId string, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
return a.DeleteAndWait(ctx, DeleteCluster{
ClusterId: clusterId,
}, options...)
}
// Update cluster configuration.
//
// Updates the configuration of a cluster to match the provided attributes and
// size. A cluster can be updated if it is in a `RUNNING` or `TERMINATED` state.
//
// If a cluster is updated while in a `RUNNING` state, it will be restarted so
// that the new attributes can take effect.
//
// If a cluster is updated while in a `TERMINATED` state, it will remain
// `TERMINATED`. The next time it is started using the `clusters/start` API, the
// new attributes will take effect. Any attempt to update a cluster in any other
// state will be rejected with an `INVALID_STATE` error code.
//
// Clusters created by the Databricks Jobs service cannot be edited.
func (a *ClustersAPI) Edit(ctx context.Context, editCluster EditCluster) (*WaitGetClusterRunning[any], error) {
err := a.impl.Edit(ctx, editCluster)
if err != nil {
return nil, err
}
return &WaitGetClusterRunning[any]{
ClusterId: editCluster.ClusterId,
poll: func(timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
return a.WaitGetClusterRunning(ctx, editCluster.ClusterId, timeout, callback)
},
timeout: 20 * time.Minute,
callback: nil,
}, nil
}
// Calls [ClustersAPI.Edit] and waits to reach RUNNING state
//
// You can override the default timeout of 20 minutes by calling adding
// retries.Timeout[ClusterDetails](60*time.Minute) functional option.
//
// Deprecated: use [ClustersAPI.Edit].Get() or [ClustersAPI.WaitGetClusterRunning]
func (a *ClustersAPI) EditAndWait(ctx context.Context, editCluster EditCluster, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
wait, err := a.Edit(ctx, editCluster)
if err != nil {
return nil, err
}
tmp := &retries.Info[ClusterDetails]{Timeout: 20 * time.Minute}
for _, o := range options {
o(tmp)
}
wait.timeout = tmp.Timeout
wait.callback = func(info *ClusterDetails) {
for _, o := range options {
o(&retries.Info[ClusterDetails]{
Info: info,
Timeout: wait.timeout,
})
}
}
return wait.Get()
}
// List cluster activity events.
//
// Retrieves a list of events about the activity of a cluster. This API is
// paginated. If there are more events to read, the response includes all the
// nparameters necessary to request the next page of events.
//
// This method is generated by Databricks SDK Code Generator.
func (a *ClustersAPI) EventsAll(ctx context.Context, request GetEvents) ([]ClusterEvent, error) {
var results []ClusterEvent
var totalCount int64 = 0
ctx = useragent.InContext(ctx, "sdk-feature", "pagination")
for {
response, err := a.impl.Events(ctx, request)
if err != nil {
return nil, err
}
if len(response.Events) == 0 {
break
}
for _, v := range response.Events {
results = append(results, v)
}
count := int64(len(response.Events))
totalCount += count
if response.NextPage == nil {
break
}
request = *response.NextPage
limit := request.Limit
if limit > 0 && totalCount >= limit {
break
}
}
return results, nil
}
// Get cluster info.
//
// Retrieves the information for a cluster given its identifier. Clusters can be
// described while they are running, or up to 60 days after they are terminated.
func (a *ClustersAPI) Get(ctx context.Context, request GetClusterRequest) (*ClusterDetails, error) {
return a.impl.Get(ctx, request)
}
// Get cluster info.
//
// Retrieves the information for a cluster given its identifier. Clusters can be
// described while they are running, or up to 60 days after they are terminated.
func (a *ClustersAPI) GetByClusterId(ctx context.Context, clusterId string) (*ClusterDetails, error) {
return a.impl.Get(ctx, GetClusterRequest{
ClusterId: clusterId,
})
}
// List all clusters.
//
// Return information about all pinned clusters, active clusters, up to 200 of
// the most recently terminated all-purpose clusters in the past 30 days, and up
// to 30 of the most recently terminated job clusters in the past 30 days.
//
// For example, if there is 1 pinned cluster, 4 active clusters, 45 terminated
// all-purpose clusters in the past 30 days, and 50 terminated job clusters in
// the past 30 days, then this API returns the 1 pinned cluster, 4 active
// clusters, all 45 terminated all-purpose clusters, and the 30 most recently
// terminated job clusters.
//
// This method is generated by Databricks SDK Code Generator.
func (a *ClustersAPI) ListAll(ctx context.Context, request ListClustersRequest) ([]ClusterDetails, error) {
response, err := a.impl.List(ctx, request)
if err != nil {
return nil, err
}
return response.Clusters, nil
}
// ClusterDetailsClusterNameToClusterIdMap calls [ClustersAPI.ListAll] and creates a map of results with [ClusterDetails].ClusterName as key and [ClusterDetails].ClusterId as value.
//
// Returns an error if there's more than one [ClusterDetails] with the same .ClusterName.
//
// Note: All [ClusterDetails] instances are loaded into memory before creating a map.
//
// This method is generated by Databricks SDK Code Generator.
func (a *ClustersAPI) ClusterDetailsClusterNameToClusterIdMap(ctx context.Context, request ListClustersRequest) (map[string]string, error) {
ctx = useragent.InContext(ctx, "sdk-feature", "name-to-id")
mapping := map[string]string{}
result, err := a.ListAll(ctx, request)
if err != nil {
return nil, err
}
for _, v := range result {
key := v.ClusterName
_, duplicate := mapping[key]
if duplicate {
return nil, fmt.Errorf("duplicate .ClusterName: %s", key)
}
mapping[key] = v.ClusterId
}
return mapping, nil
}
// GetByClusterName calls [ClustersAPI.ClusterDetailsClusterNameToClusterIdMap] and returns a single [ClusterDetails].
//
// Returns an error if there's more than one [ClusterDetails] with the same .ClusterName.
//
// Note: All [ClusterDetails] instances are loaded into memory before returning matching by name.
//
// This method is generated by Databricks SDK Code Generator.
func (a *ClustersAPI) GetByClusterName(ctx context.Context, name string) (*ClusterDetails, error) {
ctx = useragent.InContext(ctx, "sdk-feature", "get-by-name")
result, err := a.ListAll(ctx, ListClustersRequest{})
if err != nil {
return nil, err
}
tmp := map[string][]ClusterDetails{}
for _, v := range result {
key := v.ClusterName
tmp[key] = append(tmp[key], v)
}
alternatives, ok := tmp[name]
if !ok || len(alternatives) == 0 {
return nil, fmt.Errorf("ClusterDetails named '%s' does not exist", name)
}
if len(alternatives) > 1 {
return nil, fmt.Errorf("there are %d instances of ClusterDetails named '%s'", len(alternatives), name)
}
return &alternatives[0], nil
}
// List all clusters.
//
// Return information about all pinned clusters, active clusters, up to 200 of
// the most recently terminated all-purpose clusters in the past 30 days, and up
// to 30 of the most recently terminated job clusters in the past 30 days.
//
// For example, if there is 1 pinned cluster, 4 active clusters, 45 terminated
// all-purpose clusters in the past 30 days, and 50 terminated job clusters in
// the past 30 days, then this API returns the 1 pinned cluster, 4 active
// clusters, all 45 terminated all-purpose clusters, and the 30 most recently
// terminated job clusters.
func (a *ClustersAPI) ListByCanUseClient(ctx context.Context, canUseClient string) (*ListClustersResponse, error) {
return a.impl.List(ctx, ListClustersRequest{
CanUseClient: canUseClient,
})
}
// List node types.
//
// Returns a list of supported Spark node types. These node types can be used to
// launch a cluster.
func (a *ClustersAPI) ListNodeTypes(ctx context.Context) (*ListNodeTypesResponse, error) {
return a.impl.ListNodeTypes(ctx)
}
// List availability zones.
//
// Returns a list of availability zones where clusters can be created in (For
// example, us-west-2a). These zones can be used to launch a cluster.
func (a *ClustersAPI) ListZones(ctx context.Context) (*ListAvailableZonesResponse, error) {
return a.impl.ListZones(ctx)
}
// Permanently delete cluster.
//
// Permanently deletes a Spark cluster. This cluster is terminated and resources
// are asynchronously removed.
//
// In addition, users will no longer see permanently deleted clusters in the
// cluster list, and API users can no longer perform any action on permanently
// deleted clusters.
func (a *ClustersAPI) PermanentDelete(ctx context.Context, request PermanentDeleteCluster) error {
return a.impl.PermanentDelete(ctx, request)
}
// Permanently delete cluster.
//
// Permanently deletes a Spark cluster. This cluster is terminated and resources
// are asynchronously removed.
//
// In addition, users will no longer see permanently deleted clusters in the
// cluster list, and API users can no longer perform any action on permanently
// deleted clusters.
func (a *ClustersAPI) PermanentDeleteByClusterId(ctx context.Context, clusterId string) error {
return a.impl.PermanentDelete(ctx, PermanentDeleteCluster{
ClusterId: clusterId,
})
}
// Pin cluster.
//
// Pinning a cluster ensures that the cluster will always be returned by the
// ListClusters API. Pinning a cluster that is already pinned will have no
// effect. This API can only be called by workspace admins.
func (a *ClustersAPI) Pin(ctx context.Context, request PinCluster) error {
return a.impl.Pin(ctx, request)
}
// Pin cluster.
//
// Pinning a cluster ensures that the cluster will always be returned by the
// ListClusters API. Pinning a cluster that is already pinned will have no
// effect. This API can only be called by workspace admins.
func (a *ClustersAPI) PinByClusterId(ctx context.Context, clusterId string) error {
return a.impl.Pin(ctx, PinCluster{
ClusterId: clusterId,
})
}
// Resize cluster.
//
// Resizes a cluster to have a desired number of workers. This will fail unless
// the cluster is in a `RUNNING` state.
func (a *ClustersAPI) Resize(ctx context.Context, resizeCluster ResizeCluster) (*WaitGetClusterRunning[any], error) {
err := a.impl.Resize(ctx, resizeCluster)
if err != nil {
return nil, err
}
return &WaitGetClusterRunning[any]{
ClusterId: resizeCluster.ClusterId,
poll: func(timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
return a.WaitGetClusterRunning(ctx, resizeCluster.ClusterId, timeout, callback)
},
timeout: 20 * time.Minute,
callback: nil,
}, nil
}
// Calls [ClustersAPI.Resize] and waits to reach RUNNING state
//
// You can override the default timeout of 20 minutes by calling adding
// retries.Timeout[ClusterDetails](60*time.Minute) functional option.
//
// Deprecated: use [ClustersAPI.Resize].Get() or [ClustersAPI.WaitGetClusterRunning]
func (a *ClustersAPI) ResizeAndWait(ctx context.Context, resizeCluster ResizeCluster, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
wait, err := a.Resize(ctx, resizeCluster)
if err != nil {
return nil, err
}
tmp := &retries.Info[ClusterDetails]{Timeout: 20 * time.Minute}
for _, o := range options {
o(tmp)
}
wait.timeout = tmp.Timeout
wait.callback = func(info *ClusterDetails) {
for _, o := range options {
o(&retries.Info[ClusterDetails]{
Info: info,
Timeout: wait.timeout,
})
}
}
return wait.Get()
}
// Restart cluster.
//
// Restarts a Spark cluster with the supplied ID. If the cluster is not
// currently in a `RUNNING` state, nothing will happen.
func (a *ClustersAPI) Restart(ctx context.Context, restartCluster RestartCluster) (*WaitGetClusterRunning[any], error) {
err := a.impl.Restart(ctx, restartCluster)
if err != nil {
return nil, err
}
return &WaitGetClusterRunning[any]{
ClusterId: restartCluster.ClusterId,
poll: func(timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
return a.WaitGetClusterRunning(ctx, restartCluster.ClusterId, timeout, callback)
},
timeout: 20 * time.Minute,
callback: nil,
}, nil
}
// Calls [ClustersAPI.Restart] and waits to reach RUNNING state
//
// You can override the default timeout of 20 minutes by calling adding
// retries.Timeout[ClusterDetails](60*time.Minute) functional option.
//
// Deprecated: use [ClustersAPI.Restart].Get() or [ClustersAPI.WaitGetClusterRunning]
func (a *ClustersAPI) RestartAndWait(ctx context.Context, restartCluster RestartCluster, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
wait, err := a.Restart(ctx, restartCluster)
if err != nil {
return nil, err
}
tmp := &retries.Info[ClusterDetails]{Timeout: 20 * time.Minute}
for _, o := range options {
o(tmp)
}
wait.timeout = tmp.Timeout
wait.callback = func(info *ClusterDetails) {
for _, o := range options {
o(&retries.Info[ClusterDetails]{
Info: info,
Timeout: wait.timeout,
})
}
}
return wait.Get()
}
// List available Spark versions.
//
// Returns the list of available Spark versions. These versions can be used to
// launch a cluster.
func (a *ClustersAPI) SparkVersions(ctx context.Context) (*GetSparkVersionsResponse, error) {
return a.impl.SparkVersions(ctx)
}
// Start terminated cluster.
//
// Starts a terminated Spark cluster with the supplied ID. This works similar to
// `createCluster` except:
//
// * The previous cluster id and attributes are preserved. * The cluster starts
// with the last specified cluster size. * If the previous cluster was an
// autoscaling cluster, the current cluster starts with the minimum number of
// nodes. * If the cluster is not currently in a `TERMINATED` state, nothing
// will happen. * Clusters launched to run a job cannot be started.
func (a *ClustersAPI) Start(ctx context.Context, startCluster StartCluster) (*WaitGetClusterRunning[any], error) {
err := a.impl.Start(ctx, startCluster)
if err != nil {
return nil, err
}
return &WaitGetClusterRunning[any]{
ClusterId: startCluster.ClusterId,
poll: func(timeout time.Duration, callback func(*ClusterDetails)) (*ClusterDetails, error) {
return a.WaitGetClusterRunning(ctx, startCluster.ClusterId, timeout, callback)
},
timeout: 20 * time.Minute,
callback: nil,
}, nil
}
// Calls [ClustersAPI.Start] and waits to reach RUNNING state
//
// You can override the default timeout of 20 minutes by calling adding
// retries.Timeout[ClusterDetails](60*time.Minute) functional option.
//
// Deprecated: use [ClustersAPI.Start].Get() or [ClustersAPI.WaitGetClusterRunning]
func (a *ClustersAPI) StartAndWait(ctx context.Context, startCluster StartCluster, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
wait, err := a.Start(ctx, startCluster)
if err != nil {
return nil, err
}
tmp := &retries.Info[ClusterDetails]{Timeout: 20 * time.Minute}
for _, o := range options {
o(tmp)
}
wait.timeout = tmp.Timeout
wait.callback = func(info *ClusterDetails) {
for _, o := range options {
o(&retries.Info[ClusterDetails]{
Info: info,
Timeout: wait.timeout,
})
}
}
return wait.Get()
}
// Start terminated cluster.
//
// Starts a terminated Spark cluster with the supplied ID. This works similar to
// `createCluster` except:
//
// * The previous cluster id and attributes are preserved. * The cluster starts
// with the last specified cluster size. * If the previous cluster was an
// autoscaling cluster, the current cluster starts with the minimum number of
// nodes. * If the cluster is not currently in a `TERMINATED` state, nothing
// will happen. * Clusters launched to run a job cannot be started.
func (a *ClustersAPI) StartByClusterId(ctx context.Context, clusterId string) error {
return a.impl.Start(ctx, StartCluster{
ClusterId: clusterId,
})
}
func (a *ClustersAPI) StartByClusterIdAndWait(ctx context.Context, clusterId string, options ...retries.Option[ClusterDetails]) (*ClusterDetails, error) {
return a.StartAndWait(ctx, StartCluster{
ClusterId: clusterId,
}, options...)
}
// Unpin cluster.
//
// Unpinning a cluster will allow the cluster to eventually be removed from the
// ListClusters API. Unpinning a cluster that is not pinned will have no effect.
// This API can only be called by workspace admins.
func (a *ClustersAPI) Unpin(ctx context.Context, request UnpinCluster) error {
return a.impl.Unpin(ctx, request)
}
// Unpin cluster.
//
// Unpinning a cluster will allow the cluster to eventually be removed from the
// ListClusters API. Unpinning a cluster that is not pinned will have no effect.
// This API can only be called by workspace admins.
func (a *ClustersAPI) UnpinByClusterId(ctx context.Context, clusterId string) error {
return a.impl.Unpin(ctx, UnpinCluster{
ClusterId: clusterId,
})
}
func NewCommandExecution(client *client.DatabricksClient) *CommandExecutionAPI {
return &CommandExecutionAPI{
impl: &commandExecutionImpl{
client: client,
},
}
}
// This API allows execution of Python, Scala, SQL, or R commands on running
// Databricks Clusters.
type CommandExecutionAPI struct {
// impl contains low-level REST API interface, that could be overridden
// through WithImpl(CommandExecutionService)
impl CommandExecutionService
}
// WithImpl could be used to override low-level API implementations for unit
// testing purposes with [github.com/golang/mock] or other mocking frameworks.
func (a *CommandExecutionAPI) WithImpl(impl CommandExecutionService) *CommandExecutionAPI {
a.impl = impl
return a
}
// Impl returns low-level CommandExecution API implementation
func (a *CommandExecutionAPI) Impl() CommandExecutionService {
return a.impl
}
// WaitCommandStatusCommandExecutionCancelled repeatedly calls [CommandExecutionAPI.CommandStatus] and waits to reach Cancelled state
func (a *CommandExecutionAPI) WaitCommandStatusCommandExecutionCancelled(ctx context.Context, clusterId string, commandId string, contextId string,
timeout time.Duration, callback func(*CommandStatusResponse)) (*CommandStatusResponse, error) {
ctx = useragent.InContext(ctx, "sdk-feature", "long-running")
return retries.Poll[CommandStatusResponse](ctx, timeout, func() (*CommandStatusResponse, *retries.Err) {
commandStatusResponse, err := a.CommandStatus(ctx, CommandStatusRequest{
ClusterId: clusterId,
CommandId: commandId,
ContextId: contextId,
})
if err != nil {
return nil, retries.Halt(err)
}
if callback != nil {
callback(commandStatusResponse)
}
status := commandStatusResponse.Status
statusMessage := fmt.Sprintf("current status: %s", status)
if commandStatusResponse.Results != nil {
statusMessage = commandStatusResponse.Results.Cause
}
switch status {
case CommandStatusCancelled: // target state
return commandStatusResponse, nil
case CommandStatusError:
err := fmt.Errorf("failed to reach %s, got %s: %s",
CommandStatusCancelled, status, statusMessage)
return nil, retries.Halt(err)
default:
return nil, retries.Continues(statusMessage)
}
})
}
// WaitCommandStatusCommandExecutionCancelled is a wrapper that calls [CommandExecutionAPI.WaitCommandStatusCommandExecutionCancelled] and waits to reach Cancelled state.
type WaitCommandStatusCommandExecutionCancelled[R any] struct {
Response *R
ClusterId string `json:"clusterId"`
CommandId string `json:"commandId"`
ContextId string `json:"contextId"`