databricks-cli/cmd/workspace
Miles Yucht b65ce75c1f
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
..
alerts Always require path parameters as positional arguments (#1129) 2024-01-17 14:14:20 +00:00
apps Add documentation for positional args in commands generated from the Databricks OpenAPI specification (#1033) 2023-11-30 16:22:23 +00:00
artifact-allowlists Always require path parameters as positional arguments (#1129) 2024-01-17 14:14:20 +00:00
catalogs Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
clean-rooms Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
cluster-policies Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
clusters Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
connections Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
credentials-manager Bump github.com/databricks/databricks-sdk-go from 0.30.1 to 0.32.0 (#1199) 2024-02-15 14:52:17 +00:00
current-user Remove dependency on global state in generated commands (#595) 2023-07-25 20:19:07 +02:00
dashboard-widgets Always require path parameters as positional arguments (#1129) 2024-01-17 14:14:20 +00:00
dashboards Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
data-sources Remove dependency on global state in generated commands (#595) 2023-07-25 20:19:07 +02:00
experiments Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
external-locations Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
functions Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
git-credentials Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
global-init-scripts Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
grants Add documentation for positional args in commands generated from the Databricks OpenAPI specification (#1033) 2023-11-30 16:22:23 +00:00
groups Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
instance-pools Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
instance-profiles Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
ip-access-lists Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
jobs Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
lakehouse-monitors Bump github.com/databricks/databricks-sdk-go from 0.32.0 to 0.33.0 (#1222) 2024-02-19 14:30:06 +00:00
lakeview Bump github.com/databricks/databricks-sdk-go from 0.30.1 to 0.32.0 (#1199) 2024-02-15 14:52:17 +00:00
libraries Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
metastores Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
model-registry Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
model-versions Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
online-tables Bump github.com/databricks/databricks-sdk-go from 0.32.0 to 0.33.0 (#1222) 2024-02-19 14:30:06 +00:00
permissions Upgrade Go SDK to v0.29.0 (#1111) 2024-01-11 08:16:25 +00:00
pipelines Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
policy-families Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
providers Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
queries Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
query-history Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
query-visualizations Always require path parameters as positional arguments (#1129) 2024-01-17 14:14:20 +00:00
recipient-activation Add documentation for positional args in commands generated from the Databricks OpenAPI specification (#1033) 2023-11-30 16:22:23 +00:00
recipients Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
registered-models Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
repos Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
schemas Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
secrets Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
service-principals Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
serving-endpoints Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
settings Bump github.com/databricks/databricks-sdk-go from 0.30.1 to 0.32.0 (#1199) 2024-02-15 14:52:17 +00:00
shares Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
storage-credentials Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
system-schemas Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
table-constraints Add documentation for positional args in commands generated from the Databricks OpenAPI specification (#1033) 2023-11-30 16:22:23 +00:00
tables Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
token-management Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
tokens Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
users Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
vector-search-endpoints Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
vector-search-indexes Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
volumes Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
warehouses Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
workspace Use Go SDK Iterators when listing resources with the CLI (#1202) 2024-02-21 14:16:36 +00:00
workspace-bindings Add documentation for positional args in commands generated from the Databricks OpenAPI specification (#1033) 2023-11-30 16:22:23 +00:00
workspace-conf Bring back `--json` flag for workspace-conf set-status command (#1151) 2024-01-25 11:55:17 +00:00
cmd.go Bump github.com/databricks/databricks-sdk-go from 0.32.0 to 0.33.0 (#1222) 2024-02-19 14:30:06 +00:00
groups.go Upgrade Go SDK to v0.29.0 (#1111) 2024-01-11 08:16:25 +00:00