Skip to content

Commit 1153bba

Browse files
authored
[Feature] Add support for filters in databricks_clusters data source (#4014)
## Changes Version 1.50 of the Terraform Provider featured an upgrade to the Go SDK affecting cluster listing. The new List Clusters API returns all terminated clusters in the last 30 days without a limit. This results in the list operation taking considerably longer for some workspaces, especially workspaces with many jobs where clusters are frequently created. This impacts the `databricks_clusters` data source, which can be slow. This PR partially addresses this by adding support for `filter_by` to the `databricks_clusters` API. Filters expressed here are pushed to the server and result in fewer clusters being returned by the API. Users of this data source can specify a particular cluster state, cluster source, pinned status, or cluster policy ID to limit the number of clusters returned by the API, drastically speeding up performance. ## Tests Integration tests for `databricks_cluster` data source test setting the `filter_by` parameter's attributes. - [ ] `make test` run locally - [ ] relevant change in `docs/` folder - [ ] covered with integration tests in `internal/acceptance` - [ ] relevant acceptance tests are passing - [ ] using Go SDK
1 parent 1e62576 commit 1153bba

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

clusters/data_clusters.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ import (
1111

1212
func DataSourceClusters() common.Resource {
1313
return common.WorkspaceData(func(ctx context.Context, data *struct {
14-
Id string `json:"id,omitempty" tf:"computed"`
15-
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"`
16-
ClusterNameContains string `json:"cluster_name_contains,omitempty"`
14+
Id string `json:"id,omitempty" tf:"computed"`
15+
Ids []string `json:"ids,omitempty" tf:"computed,slice_set"`
16+
ClusterNameContains string `json:"cluster_name_contains,omitempty"`
17+
FilterBy *compute.ListClustersFilterBy `json:"filter_by,omitempty"`
1718
}, w *databricks.WorkspaceClient) error {
18-
clusters, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{})
19+
clusters, err := w.Clusters.ListAll(ctx, compute.ListClustersRequest{
20+
FilterBy: data.FilterBy,
21+
})
1922
if err != nil {
2023
return err
2124
}

docs/data-sources/clusters.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ data "databricks_clusters" "all_shared" {
2727
## Argument Reference
2828

2929
* `cluster_name_contains` - (Optional) Only return [databricks_cluster](../resources/cluster.md#cluster_id) ids that match the given name string.
30+
* `filter_by` - (Optional) Filters to apply to the listed clusters. See [filter_by Configuration Block](#filter_by-configuration-block) below for details.
31+
32+
### filter_by Configuration Block
33+
34+
The `filter_by` block controls the filtering of the listed clusters. It supports the following arguments:
35+
36+
* `cluster_sources` - (Optional) List of cluster sources to filter by. Possible values are `API`, `JOB`, `MODELS`, `PIPELINE`, `PIPELINE_MAINTENANCE`, `SQL`, and `UI`.
37+
* `cluster_states` - (Optional) List of cluster states to filter by. Possible values are `RUNNING`, `PENDING`, `RESIZING`, `RESTARTING`, `TERMINATING`, `TERMINATED`, `ERROR`, and `UNKNOWN`.
38+
* `is_pinned` - (Optional) Whether to filter by pinned clusters.
39+
* `policy_id` - (Optional) Filter by [databricks_cluster_policy](../resources/cluster_policy.md) id.
3040

3141
## Attribute Reference
3242

internal/acceptance/data_clusters_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package acceptance
22

33
import (
4+
"context"
45
"testing"
6+
7+
"github.com/databricks/databricks-sdk-go"
8+
"github.com/databricks/databricks-sdk-go/service/compute"
9+
"github.com/hashicorp/terraform-plugin-testing/terraform"
10+
"github.com/stretchr/testify/assert"
511
)
612

713
func TestAccDataSourceClustersNoFilter(t *testing.T) {
@@ -20,3 +26,67 @@ func TestAccDataSourceClustersWithFilter(t *testing.T) {
2026
}`,
2127
})
2228
}
29+
30+
func checkFirstCluster(t *testing.T, f func(*compute.ClusterDetails)) func(*terraform.State) error {
31+
return func(s *terraform.State) error {
32+
w := databricks.Must(databricks.NewWorkspaceClient())
33+
firstClusterId, ok := s.RootModule().Resources["data.databricks_clusters.this"].Primary.Attributes["ids.0"]
34+
if ok {
35+
firstCluster, err := w.Clusters.GetByClusterId(context.Background(), firstClusterId)
36+
assert.NoError(t, err)
37+
f(firstCluster)
38+
}
39+
return nil
40+
}
41+
}
42+
43+
func TestAccDataSourceClusters_FilterBy(t *testing.T) {
44+
WorkspaceLevel(t, Step{
45+
Template: `
46+
data "databricks_clusters" "this" {
47+
filter_by {
48+
cluster_sources = ["UI", "API"]
49+
}
50+
}`,
51+
Check: checkFirstCluster(t, func(c *compute.ClusterDetails) {
52+
assert.Contains(t, []compute.ClusterSource{"UI", "API"}, c.ClusterSource)
53+
}),
54+
}, Step{
55+
Template: `
56+
data "databricks_clusters" "this" {
57+
filter_by {
58+
cluster_states = ["RUNNING", "RESIZING"]
59+
}
60+
}`,
61+
Check: checkFirstCluster(t, func(c *compute.ClusterDetails) {
62+
assert.Contains(t, []compute.State{"RUNNING", "RESIZING"}, c.State)
63+
}),
64+
}, Step{
65+
Template: `
66+
data "databricks_clusters" "this" {
67+
filter_by {
68+
is_pinned = true
69+
}
70+
}`,
71+
// Not possible to get whether a cluster is pinned or not
72+
}, Step{
73+
Template: `
74+
resource "databricks_cluster_policy" "this" {
75+
name = "test"
76+
definition = jsonencode({
77+
"spark_conf.spark.hadoop.javax.jdo.option.ConnectionURL": {
78+
"type": "fixed",
79+
"value": "jdbc:sqlserver://<jdbc-url>"
80+
}
81+
})
82+
}
83+
data "databricks_clusters" "this" {
84+
filter_by {
85+
policy_id = databricks_cluster_policy.this.id
86+
}
87+
}`,
88+
Check: checkFirstCluster(t, func(c *compute.ClusterDetails) {
89+
assert.Equal(t, "abc-123", c.PolicyId)
90+
}),
91+
})
92+
}

0 commit comments

Comments
 (0)