2023-06-12 12:23:21 +00:00
|
|
|
// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT.
|
|
|
|
|
|
|
|
package connections
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/cmd/root"
|
|
|
|
"github.com/databricks/cli/libs/cmdio"
|
|
|
|
"github.com/databricks/cli/libs/flags"
|
|
|
|
"github.com/databricks/databricks-sdk-go/service/catalog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// 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: "connections",
|
|
|
|
Short: `Connections allow for creating a connection to an external data source.`,
|
|
|
|
Long: `Connections allow for creating a connection to an external data source.
|
2023-06-12 12:23:21 +00:00
|
|
|
|
|
|
|
A connection is an abstraction of an external data source that can be
|
|
|
|
connected from Databricks Compute. Creating a connection object is the first
|
|
|
|
step to managing external data sources within Unity Catalog, with the second
|
|
|
|
step being creating a data object (catalog, schema, or table) using the
|
|
|
|
connection. Data objects derived from a connection can be written to or read
|
|
|
|
from similar to other Unity Catalog data objects based on cloud storage. Users
|
|
|
|
may create different types of connections with each connection having a unique
|
|
|
|
set of configuration options to support credential management and other
|
|
|
|
settings.`,
|
2023-07-25 18:19:07 +00:00
|
|
|
GroupID: "catalog",
|
|
|
|
Annotations: map[string]string{
|
|
|
|
"package": "catalog",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-03-06 09:53:44 +00:00
|
|
|
// Add methods
|
|
|
|
cmd.AddCommand(newCreate())
|
|
|
|
cmd.AddCommand(newDelete())
|
|
|
|
cmd.AddCommand(newGet())
|
|
|
|
cmd.AddCommand(newList())
|
|
|
|
cmd.AddCommand(newUpdate())
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// Apply optional overrides to this command.
|
|
|
|
for _, fn := range cmdOverrides {
|
|
|
|
fn(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
2023-06-12 12:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start create command
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// Slice with functions to override default command behavior.
|
|
|
|
// Functions can be added from the `init()` function in manually curated files in this directory.
|
|
|
|
var createOverrides []func(
|
|
|
|
*cobra.Command,
|
|
|
|
*catalog.CreateConnection,
|
|
|
|
)
|
|
|
|
|
|
|
|
func newCreate() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{}
|
|
|
|
|
|
|
|
var createReq catalog.CreateConnection
|
|
|
|
var createJson flags.JsonFlag
|
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
// TODO: short flags
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Flags().Var(&createJson, "json", `either inline JSON string or @path/to/file.json with request body`)
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Flags().StringVar(&createReq.Comment, "comment", createReq.Comment, `User-provided free-form text description.`)
|
2023-09-05 09:43:57 +00:00
|
|
|
// TODO: map via StringToStringVar: properties
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Flags().BoolVar(&createReq.ReadOnly, "read-only", createReq.ReadOnly, `If the connection is read only.`)
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Use = "create"
|
|
|
|
cmd.Short = `Create a connection.`
|
|
|
|
cmd.Long = `Create a connection.
|
2023-06-12 12:23:21 +00:00
|
|
|
|
|
|
|
Creates a new connection
|
|
|
|
|
|
|
|
Creates a new connection to an external data source. It allows users to
|
|
|
|
specify connection details and configurations for interaction with the
|
2023-07-25 18:19:07 +00:00
|
|
|
external server.`
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Annotations = make(map[string]string)
|
|
|
|
|
|
|
|
cmd.PreRunE = root.MustWorkspaceClient
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
2023-06-12 12:23:21 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
w := root.WorkspaceClient(ctx)
|
2023-07-03 11:20:30 +00:00
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
if cmd.Flags().Changed("json") {
|
|
|
|
err = createJson.Unmarshal(&createReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-21 07:25:29 +00:00
|
|
|
return fmt.Errorf("please provide command input in JSON format by specifying the --json flag")
|
2023-06-12 12:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := w.Connections.Create(ctx, createReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cmdio.Render(ctx, response)
|
2023-07-25 18:19:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 14:56:36 +00:00
|
|
|
// Disable completions since they are not applicable.
|
|
|
|
// Can be overridden by manual implementation in `override.go`.
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.ValidArgsFunction = cobra.NoFileCompletions
|
|
|
|
|
|
|
|
// Apply optional overrides to this command.
|
|
|
|
for _, fn := range createOverrides {
|
|
|
|
fn(cmd, &createReq)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
// start delete command
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// 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,
|
|
|
|
*catalog.DeleteConnectionRequest,
|
|
|
|
)
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
func newDelete() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{}
|
|
|
|
|
|
|
|
var deleteReq catalog.DeleteConnectionRequest
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// TODO: short flags
|
|
|
|
|
2024-02-15 14:52:17 +00:00
|
|
|
cmd.Use = "delete NAME"
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Short = `Delete a connection.`
|
|
|
|
cmd.Long = `Delete a connection.
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-11-30 16:22:23 +00:00
|
|
|
Deletes the connection that matches the supplied name.
|
|
|
|
|
|
|
|
Arguments:
|
2024-02-15 14:52:17 +00:00
|
|
|
NAME: The name of the connection to be deleted.`
|
2023-07-25 18:19:07 +00:00
|
|
|
|
|
|
|
cmd.Annotations = make(map[string]string)
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.PreRunE = root.MustWorkspaceClient
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
2023-06-12 12:23:21 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
w := root.WorkspaceClient(ctx)
|
2023-07-03 11:20:30 +00:00
|
|
|
|
2023-07-27 13:23:55 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
promptSpinner := cmdio.Spinner(ctx)
|
2024-02-15 14:52:17 +00:00
|
|
|
promptSpinner <- "No NAME argument specified. Loading names for Connections drop-down."
|
2023-07-27 13:23:55 +00:00
|
|
|
names, err := w.Connections.ConnectionInfoNameToFullNameMap(ctx)
|
|
|
|
close(promptSpinner)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load names for Connections drop-down. Please manually specify required arguments. Original error: %w", err)
|
|
|
|
}
|
|
|
|
id, err := cmdio.Select(ctx, names, "The name of the connection to be deleted")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args = append(args, id)
|
|
|
|
}
|
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("expected to have the name of the connection to be deleted")
|
|
|
|
}
|
2024-02-15 14:52:17 +00:00
|
|
|
deleteReq.Name = args[0]
|
2023-06-12 12:23:21 +00:00
|
|
|
|
|
|
|
err = w.Connections.Delete(ctx, deleteReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-07-25 18:19:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 14:56:36 +00:00
|
|
|
// Disable completions since they are not applicable.
|
|
|
|
// Can be overridden by manual implementation in `override.go`.
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.ValidArgsFunction = cobra.NoFileCompletions
|
|
|
|
|
|
|
|
// Apply optional overrides to this command.
|
|
|
|
for _, fn := range deleteOverrides {
|
|
|
|
fn(cmd, &deleteReq)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
// start get command
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// 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,
|
|
|
|
*catalog.GetConnectionRequest,
|
|
|
|
)
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
func newGet() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{}
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
var getReq catalog.GetConnectionRequest
|
|
|
|
|
|
|
|
// TODO: short flags
|
|
|
|
|
2024-02-15 14:52:17 +00:00
|
|
|
cmd.Use = "get NAME"
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Short = `Get a connection.`
|
|
|
|
cmd.Long = `Get a connection.
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-11-30 16:22:23 +00:00
|
|
|
Gets a connection from it's name.
|
|
|
|
|
|
|
|
Arguments:
|
2024-02-15 14:52:17 +00:00
|
|
|
NAME: Name of the connection.`
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Annotations = make(map[string]string)
|
|
|
|
|
|
|
|
cmd.PreRunE = root.MustWorkspaceClient
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
2023-06-12 12:23:21 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
w := root.WorkspaceClient(ctx)
|
2023-07-03 11:20:30 +00:00
|
|
|
|
2023-07-27 13:23:55 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
promptSpinner := cmdio.Spinner(ctx)
|
2024-02-15 14:52:17 +00:00
|
|
|
promptSpinner <- "No NAME argument specified. Loading names for Connections drop-down."
|
2023-07-27 13:23:55 +00:00
|
|
|
names, err := w.Connections.ConnectionInfoNameToFullNameMap(ctx)
|
|
|
|
close(promptSpinner)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load names for Connections drop-down. Please manually specify required arguments. Original error: %w", err)
|
|
|
|
}
|
|
|
|
id, err := cmdio.Select(ctx, names, "Name of the connection")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args = append(args, id)
|
|
|
|
}
|
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("expected to have name of the connection")
|
|
|
|
}
|
2024-02-15 14:52:17 +00:00
|
|
|
getReq.Name = args[0]
|
2023-06-12 12:23:21 +00:00
|
|
|
|
|
|
|
response, err := w.Connections.Get(ctx, getReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cmdio.Render(ctx, response)
|
2023-07-25 18:19:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 14:56:36 +00:00
|
|
|
// Disable completions since they are not applicable.
|
|
|
|
// Can be overridden by manual implementation in `override.go`.
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.ValidArgsFunction = cobra.NoFileCompletions
|
|
|
|
|
|
|
|
// Apply optional overrides to this command.
|
|
|
|
for _, fn := range getOverrides {
|
|
|
|
fn(cmd, &getReq)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
// start list command
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// 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,
|
|
|
|
)
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
func newList() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{}
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Use = "list"
|
|
|
|
cmd.Short = `List connections.`
|
|
|
|
cmd.Long = `List connections.
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
List all connections.`
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Annotations = make(map[string]string)
|
|
|
|
|
|
|
|
cmd.PreRunE = root.MustWorkspaceClient
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
2023-06-12 12:23:21 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
w := root.WorkspaceClient(ctx)
|
Use Go SDK Iterators when listing resources with the CLI (#1202)
## Changes
Currently, when the CLI run a list API call (like list jobs), it uses
the `List*All` methods from the SDK, which list all resources in the
collection. This is very slow for large collections: if you need to list
all jobs from a workspace that has 10,000+ jobs, you'll be waiting for
at least 100 RPCs to complete before seeing any output.
Instead of using List*All() methods, the SDK recently added an iterator
data structure that allows traversing the collection without needing to
completely list it first. New pages are fetched lazily if the next
requested item belongs to the next page. Using the List() methods that
return these iterators, the CLI can proactively print out some of the
response before the complete collection has been fetched.
This involves a pretty major rewrite of the rendering logic in `cmdio`.
The idea there is to define custom rendering logic based on the type of
the provided resource. There are three renderer interfaces:
1. textRenderer: supports printing something in a textual format (i.e.
not JSON, and not templated).
2. jsonRenderer: supports printing something in a pretty-printed JSON
format.
3. templateRenderer: supports printing something using a text template.
There are also three renderer implementations:
1. readerRenderer: supports printing a reader. This only implements the
textRenderer interface.
2. iteratorRenderer: supports printing a `listing.Iterator` from the Go
SDK. This implements jsonRenderer and templateRenderer, buffering 20
resources at a time before writing them to the output.
3. defaultRenderer: supports printing arbitrary resources (the previous
implementation).
Callers will either use `cmdio.Render()` for rendering individual
resources or `io.Reader` or `cmdio.RenderIterator()` for rendering an
iterator. This separate method is needed to safely be able to match on
the type of the iterator, since Go does not allow runtime type matches
on generic types with an existential type parameter.
One other change that needs to happen is to split the templates used for
text representation of list resources into a header template and a row
template. The template is now executed multiple times for List API
calls, but the header should only be printed once. To support this, I
have added `headerTemplate` to `cmdIO`, and I have also changed
`RenderWithTemplate` to include a `headerTemplate` parameter everywhere.
## Tests
- [x] Unit tests for text rendering logic
- [x] Unit test for reflection-based iterator construction.
---------
Co-authored-by: Andrew Nester <andrew.nester@databricks.com>
2024-02-21 14:16:36 +00:00
|
|
|
response := w.Connections.List(ctx)
|
|
|
|
return cmdio.RenderIterator(ctx, response)
|
2023-07-25 18:19:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 14:56:36 +00:00
|
|
|
// Disable completions since they are not applicable.
|
|
|
|
// Can be overridden by manual implementation in `override.go`.
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.ValidArgsFunction = cobra.NoFileCompletions
|
|
|
|
|
|
|
|
// Apply optional overrides to this command.
|
|
|
|
for _, fn := range listOverrides {
|
|
|
|
fn(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
// start update command
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
// Slice with functions to override default command behavior.
|
|
|
|
// Functions can be added from the `init()` function in manually curated files in this directory.
|
|
|
|
var updateOverrides []func(
|
|
|
|
*cobra.Command,
|
|
|
|
*catalog.UpdateConnection,
|
|
|
|
)
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
func newUpdate() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{}
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
var updateReq catalog.UpdateConnection
|
|
|
|
var updateJson flags.JsonFlag
|
|
|
|
|
|
|
|
// TODO: short flags
|
|
|
|
cmd.Flags().Var(&updateJson, "json", `either inline JSON string or @path/to/file.json with request body`)
|
|
|
|
|
2023-12-14 08:15:00 +00:00
|
|
|
cmd.Flags().StringVar(&updateReq.NewName, "new-name", updateReq.NewName, `New name for the connection.`)
|
2023-10-16 06:56:06 +00:00
|
|
|
cmd.Flags().StringVar(&updateReq.Owner, "owner", updateReq.Owner, `Username of current owner of the connection.`)
|
|
|
|
|
2024-02-15 14:52:17 +00:00
|
|
|
cmd.Use = "update NAME"
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Short = `Update a connection.`
|
|
|
|
cmd.Long = `Update a connection.
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2024-01-17 14:14:20 +00:00
|
|
|
Updates the connection that matches the supplied name.
|
|
|
|
|
|
|
|
Arguments:
|
2024-02-15 14:52:17 +00:00
|
|
|
NAME: Name of the connection.`
|
2023-06-12 12:23:21 +00:00
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.Annotations = make(map[string]string)
|
|
|
|
|
2024-01-17 14:14:20 +00:00
|
|
|
cmd.Args = func(cmd *cobra.Command, args []string) error {
|
|
|
|
check := cobra.ExactArgs(1)
|
|
|
|
return check(cmd, args)
|
|
|
|
}
|
|
|
|
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.PreRunE = root.MustWorkspaceClient
|
|
|
|
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
2023-06-12 12:23:21 +00:00
|
|
|
ctx := cmd.Context()
|
|
|
|
w := root.WorkspaceClient(ctx)
|
2023-07-03 11:20:30 +00:00
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
if cmd.Flags().Changed("json") {
|
|
|
|
err = updateJson.Unmarshal(&updateReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-21 07:25:29 +00:00
|
|
|
return fmt.Errorf("please provide command input in JSON format by specifying the --json flag")
|
2023-06-12 12:23:21 +00:00
|
|
|
}
|
2024-02-15 14:52:17 +00:00
|
|
|
updateReq.Name = args[0]
|
2023-06-12 12:23:21 +00:00
|
|
|
|
|
|
|
response, err := w.Connections.Update(ctx, updateReq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cmdio.Render(ctx, response)
|
2023-07-25 18:19:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 14:56:36 +00:00
|
|
|
// Disable completions since they are not applicable.
|
|
|
|
// Can be overridden by manual implementation in `override.go`.
|
2023-07-25 18:19:07 +00:00
|
|
|
cmd.ValidArgsFunction = cobra.NoFileCompletions
|
|
|
|
|
|
|
|
// Apply optional overrides to this command.
|
|
|
|
for _, fn := range updateOverrides {
|
|
|
|
fn(cmd, &updateReq)
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-06-12 12:23:21 +00:00
|
|
|
// end service Connections
|