-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathtoken-management.go
More file actions
682 lines (534 loc) · 19.5 KB
/
Copy pathtoken-management.go
File metadata and controls
682 lines (534 loc) · 19.5 KB
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
// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT.
package token_management
import (
"fmt"
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdctx"
"github.com/databricks/cli/libs/cmdio"
"github.com/databricks/cli/libs/flags"
"github.com/databricks/databricks-sdk-go/service/settings"
"github.com/spf13/cobra"
)
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var cmdOverrides []func(*cobra.Command)
func New() *cobra.Command {
cmd := &cobra.Command{
Use: "token-management",
Short: `Enables administrators to get all tokens and delete tokens for other users.`,
Long: `Enables administrators to get all tokens and delete tokens for other users.
Admins can either get every token, get a specific token by ID, or get all
tokens for a particular user.`,
GroupID: "settings",
RunE: root.ReportUnknownSubcommand,
}
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
// Add methods
cmd.AddCommand(newCreateOboToken())
cmd.AddCommand(newDelete())
cmd.AddCommand(newGet())
cmd.AddCommand(newGetPermissionLevels())
cmd.AddCommand(newGetPermissions())
cmd.AddCommand(newList())
cmd.AddCommand(newSetPermissions())
cmd.AddCommand(newUpdatePermissions())
cmd.AddCommand(newUpdateTokenManagement())
// Apply optional overrides to this command.
for _, fn := range cmdOverrides {
fn(cmd)
}
return cmd
}
// start create-obo-token command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var createOboTokenOverrides []func(
*cobra.Command,
*settings.CreateOboTokenRequest,
)
func newCreateOboToken() *cobra.Command {
cmd := &cobra.Command{}
var createOboTokenReq settings.CreateOboTokenRequest
var createOboTokenJson flags.JsonFlag
cmd.Flags().Var(&createOboTokenJson, "json", `either inline JSON string or @path/to/file.json with request body`)
cmd.Flags().BoolVar(&createOboTokenReq.AutoscopeEnabled, "autoscope-enabled", createOboTokenReq.AutoscopeEnabled, `Whether to enable autoscoping for this token.`)
cmd.Flags().StringVar(&createOboTokenReq.Comment, "comment", createOboTokenReq.Comment, `Comment that describes the purpose of the token.`)
cmd.Flags().Int64Var(&createOboTokenReq.LifetimeSeconds, "lifetime-seconds", createOboTokenReq.LifetimeSeconds, `The number of seconds before the token expires.`)
// TODO: array: scopes
cmd.Use = "create-obo-token APPLICATION_ID"
cmd.Short = `Create on-behalf token.`
cmd.Long = `Create on-behalf token.
Creates a token on behalf of a service principal.
Arguments:
APPLICATION_ID: Application ID of the service principal.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.Args = func(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("json") {
err := root.ExactArgs(0)(cmd, args)
if err != nil {
return fmt.Errorf("when --json flag is specified, no positional arguments are allowed. Provide 'application_id' in your JSON input")
}
return nil
}
return nil
}
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
if cmd.Flags().Changed("json") {
diags := createOboTokenJson.Unmarshal(&createOboTokenReq)
if diags.HasError() {
return diags.Error()
}
if len(diags) > 0 {
err := cmdio.RenderDiagnostics(ctx, diags)
if err != nil {
return err
}
}
} else {
if len(args) == 0 {
sp := cmdio.NewSpinner(ctx)
sp.Update("No APPLICATION_ID argument specified. Loading names for Token Management drop-down.")
names, err := w.TokenManagement.TokenInfoCommentToTokenIdMap(ctx, settings.ListTokenManagementRequest{})
sp.Close()
if err != nil {
return fmt.Errorf("failed to load names for Token Management drop-down. Please manually specify required arguments. Original error: %w", err)
}
id, err := cmdio.Select(ctx, names, "Application ID of the service principal")
if err != nil {
return err
}
args = append(args, id)
}
if len(args) != 1 {
return fmt.Errorf("expected to have application id of the service principal")
}
createOboTokenReq.ApplicationId = args[0]
}
response, err := w.TokenManagement.CreateOboToken(ctx, createOboTokenReq)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range createOboTokenOverrides {
fn(cmd, &createOboTokenReq)
}
return cmd
}
// start delete command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var deleteOverrides []func(
*cobra.Command,
*settings.DeleteTokenManagementRequest,
)
func newDelete() *cobra.Command {
cmd := &cobra.Command{}
var deleteReq settings.DeleteTokenManagementRequest
cmd.Use = "delete TOKEN_ID"
cmd.Short = `Delete a token.`
cmd.Long = `Delete a token.
Deletes a token, specified by its ID.
Arguments:
TOKEN_ID: The ID of the token to revoke.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
if len(args) == 0 {
sp := cmdio.NewSpinner(ctx)
sp.Update("No TOKEN_ID argument specified. Loading names for Token Management drop-down.")
names, err := w.TokenManagement.TokenInfoCommentToTokenIdMap(ctx, settings.ListTokenManagementRequest{})
sp.Close()
if err != nil {
return fmt.Errorf("failed to load names for Token Management drop-down. Please manually specify required arguments. Original error: %w", err)
}
id, err := cmdio.Select(ctx, names, "The ID of the token to revoke")
if err != nil {
return err
}
args = append(args, id)
}
if len(args) != 1 {
return fmt.Errorf("expected to have the id of the token to revoke")
}
deleteReq.TokenId = args[0]
err = w.TokenManagement.Delete(ctx, deleteReq)
if err != nil {
return err
}
return nil
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range deleteOverrides {
fn(cmd, &deleteReq)
}
return cmd
}
// start get command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var getOverrides []func(
*cobra.Command,
*settings.GetTokenManagementRequest,
)
func newGet() *cobra.Command {
cmd := &cobra.Command{}
var getReq settings.GetTokenManagementRequest
cmd.Use = "get TOKEN_ID"
cmd.Short = `Get token info.`
cmd.Long = `Get token info.
Gets information about a token, specified by its ID.
Arguments:
TOKEN_ID: The ID of the token to get.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
if len(args) == 0 {
sp := cmdio.NewSpinner(ctx)
sp.Update("No TOKEN_ID argument specified. Loading names for Token Management drop-down.")
names, err := w.TokenManagement.TokenInfoCommentToTokenIdMap(ctx, settings.ListTokenManagementRequest{})
sp.Close()
if err != nil {
return fmt.Errorf("failed to load names for Token Management drop-down. Please manually specify required arguments. Original error: %w", err)
}
id, err := cmdio.Select(ctx, names, "The ID of the token to get")
if err != nil {
return err
}
args = append(args, id)
}
if len(args) != 1 {
return fmt.Errorf("expected to have the id of the token to get")
}
getReq.TokenId = args[0]
response, err := w.TokenManagement.Get(ctx, getReq)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range getOverrides {
fn(cmd, &getReq)
}
return cmd
}
// start get-permission-levels command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var getPermissionLevelsOverrides []func(
*cobra.Command,
)
func newGetPermissionLevels() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "get-permission-levels"
cmd.Short = `Get token permission levels.`
cmd.Long = `Get token permission levels.
Gets the permission levels that a user can have on an object.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
response, err := w.TokenManagement.GetPermissionLevels(ctx)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range getPermissionLevelsOverrides {
fn(cmd)
}
return cmd
}
// start get-permissions command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var getPermissionsOverrides []func(
*cobra.Command,
)
func newGetPermissions() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "get-permissions"
cmd.Short = `Get token permissions.`
cmd.Long = `Get token permissions.
Gets the permissions of all tokens. Tokens can inherit permissions from their
root object.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
response, err := w.TokenManagement.GetPermissions(ctx)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range getPermissionsOverrides {
fn(cmd)
}
return cmd
}
// start list command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var listOverrides []func(
*cobra.Command,
*settings.ListTokenManagementRequest,
)
func newList() *cobra.Command {
cmd := &cobra.Command{}
var listReq settings.ListTokenManagementRequest
// Registered for all paginated methods. Validated at call time in the
// method-call template. Paginated list methods never have Wait or LRO
// branches, so the method-call path is always reached.
var listLimit int
cmd.Flags().Int64Var(&listReq.CreatedById, "created-by-id", listReq.CreatedById, `User ID of the user that created the token.`)
cmd.Flags().StringVar(&listReq.CreatedByUsername, "created-by-username", listReq.CreatedByUsername, `Username of the user that created the token.`)
// Limit flag for total result capping.
cmd.Flags().IntVar(&listLimit, "limit", 0, `Maximum number of results to return.`)
// Hidden pagination flags (internal API parameters).
cmd.Use = "list"
cmd.Short = `List all tokens.`
cmd.Long = `List all tokens.
Lists all tokens associated with the specified workspace or user.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.Args = func(cmd *cobra.Command, args []string) error {
check := root.ExactArgs(0)
return check(cmd, args)
}
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
response := w.TokenManagement.List(ctx, listReq)
if listLimit < 0 {
return fmt.Errorf("--limit must be a non-negative integer, got %d", listLimit)
}
if listLimit > 0 {
ctx = cmdio.WithLimit(ctx, listLimit)
}
return cmdio.RenderIterator(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range listOverrides {
fn(cmd, &listReq)
}
return cmd
}
// start set-permissions command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var setPermissionsOverrides []func(
*cobra.Command,
*settings.TokenPermissionsRequest,
)
func newSetPermissions() *cobra.Command {
cmd := &cobra.Command{}
var setPermissionsReq settings.TokenPermissionsRequest
var setPermissionsJson flags.JsonFlag
cmd.Flags().Var(&setPermissionsJson, "json", `either inline JSON string or @path/to/file.json with request body`)
// TODO: array: access_control_list
cmd.Use = "set-permissions"
cmd.Short = `Set token permissions.`
cmd.Long = `Set token permissions.
Sets permissions on an object, replacing existing permissions if they exist.
Deletes all direct permissions if none are specified. Objects can inherit
permissions from their root object.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.Args = func(cmd *cobra.Command, args []string) error {
check := root.ExactArgs(0)
return check(cmd, args)
}
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
if cmd.Flags().Changed("json") {
diags := setPermissionsJson.Unmarshal(&setPermissionsReq)
if diags.HasError() {
return diags.Error()
}
if len(diags) > 0 {
err := cmdio.RenderDiagnostics(ctx, diags)
if err != nil {
return err
}
}
}
response, err := w.TokenManagement.SetPermissions(ctx, setPermissionsReq)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range setPermissionsOverrides {
fn(cmd, &setPermissionsReq)
}
return cmd
}
// start update-permissions command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var updatePermissionsOverrides []func(
*cobra.Command,
*settings.TokenPermissionsRequest,
)
func newUpdatePermissions() *cobra.Command {
cmd := &cobra.Command{}
var updatePermissionsReq settings.TokenPermissionsRequest
var updatePermissionsJson flags.JsonFlag
cmd.Flags().Var(&updatePermissionsJson, "json", `either inline JSON string or @path/to/file.json with request body`)
// TODO: array: access_control_list
cmd.Use = "update-permissions"
cmd.Short = `Update token permissions.`
cmd.Long = `Update token permissions.
Updates the permissions on all tokens. Tokens can inherit permissions from
their root object.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.Args = func(cmd *cobra.Command, args []string) error {
check := root.ExactArgs(0)
return check(cmd, args)
}
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
if cmd.Flags().Changed("json") {
diags := updatePermissionsJson.Unmarshal(&updatePermissionsReq)
if diags.HasError() {
return diags.Error()
}
if len(diags) > 0 {
err := cmdio.RenderDiagnostics(ctx, diags)
if err != nil {
return err
}
}
}
response, err := w.TokenManagement.UpdatePermissions(ctx, updatePermissionsReq)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range updatePermissionsOverrides {
fn(cmd, &updatePermissionsReq)
}
return cmd
}
// start update-token-management command
// Slice with functions to override default command behavior.
// Functions can be added from the `init()` function in manually curated files in this directory.
var updateTokenManagementOverrides []func(
*cobra.Command,
*settings.UpdateTokenManagementRequest,
)
func newUpdateTokenManagement() *cobra.Command {
cmd := &cobra.Command{}
var updateTokenManagementReq settings.UpdateTokenManagementRequest
var updateTokenManagementJson flags.JsonFlag
cmd.Flags().Var(&updateTokenManagementJson, "json", `either inline JSON string or @path/to/file.json with request body`)
cmd.Use = "update-token-management TOKEN_ID"
cmd.Short = `Update an on-behalf token.`
cmd.Long = `Update an on-behalf token.
Updates a token, specified by its ID.
Arguments:
TOKEN_ID: ID of the token.`
cmd.Annotations = make(map[string]string)
cmd.Annotations["launch_stage"] = "GA"
cmd.Annotations["launch_stage_display"] = "GA"
cmd.Args = func(cmd *cobra.Command, args []string) error {
check := root.ExactArgs(1)
return check(cmd, args)
}
cmd.PreRunE = root.MustWorkspaceClient
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
ctx := cmd.Context()
w := cmdctx.WorkspaceClient(ctx)
if cmd.Flags().Changed("json") {
diags := updateTokenManagementJson.Unmarshal(&updateTokenManagementReq)
if diags.HasError() {
return diags.Error()
}
if len(diags) > 0 {
err := cmdio.RenderDiagnostics(ctx, diags)
if err != nil {
return err
}
}
} else {
return fmt.Errorf("please provide command input in JSON format by specifying the --json flag")
}
updateTokenManagementReq.TokenId = args[0]
response, err := w.TokenManagement.UpdateTokenManagement(ctx, updateTokenManagementReq)
if err != nil {
return err
}
return cmdio.Render(ctx, response)
}
// Disable completions since they are not applicable.
// Can be overridden by manual implementation in `override.go`.
cmd.ValidArgsFunction = cobra.NoFileCompletions
// Apply optional overrides to this command.
for _, fn := range updateTokenManagementOverrides {
fn(cmd, &updateTokenManagementReq)
}
return cmd
}
// end service TokenManagement