Skip to content

Commit 571e504

Browse files
authored
Add support for clusters in deployment bind/unbind commands (#2536)
## Changes 1. Changed `FindResourceByConfigKey` to return cluster resources 2. Added a new option for acceptance tests: `RequiresCluster` ## Why This PR adds support for cluster resources in deployment operations, enabling users to: - Bind clusters using `databricks bundle deployment bind <mycluster_key> <mycluster_id>` - Unbind experiments using `databricks bundle deployment unbind <mycluster_key>` Where: - `mycluster_key` is a resource key defined in the bundle's .yml file - `mycluster_id` references an existing cluster in the Databricks workspace These capabilities allow for more flexible resource management of clusters within bundles. ## Tests Added a new acceptance test that tests bind and unbind methods
1 parent abc54ee commit 571e504

File tree

8 files changed

+78
-0
lines changed

8 files changed

+78
-0
lines changed

NEXT_CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
* Processing 'artifacts' section is now done in "bundle validate" (adding defaults, inferring "build", asserting required fields) ([#2526])(https://github.com/databricks/cli/pull/2526))
1010
* When uploading artifacts, include relative path in log message ([#2539])(https://github.com/databricks/cli/pull/2539))
11+
* Add support for clusters in deployment bind/unbind commands ([#2536](https://github.com/databricks/cli/pull/2536))
1112

1213
### API Changes

acceptance/acceptance_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ func getSkipReason(config *internal.TestConfig, configPath string) string {
290290
return fmt.Sprintf("Disabled via RequiresUnityCatalog setting in %s (TEST_METASTORE_ID=%s)", configPath, os.Getenv("TEST_METASTORE_ID"))
291291
}
292292

293+
if isTruePtr(config.RequiresCluster) && os.Getenv("TEST_DEFAULT_CLUSTER_ID") == "" {
294+
return fmt.Sprintf("Disabled via RequiresCluster setting in %s (TEST_DEFAULT_CLUSTER_ID=%s)", configPath, os.Getenv("TEST_DEFAULT_CLUSTER_ID"))
295+
}
296+
293297
} else {
294298
// Local run
295299
if !isTruePtr(config.Local) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
bundle:
2+
name: bind-cluster-test-$UNIQUE_NAME
3+
4+
resources:
5+
clusters:
6+
cluster1:
7+
cluster_name: "DEFAULT Test Cluster"
8+
spark_version: '16.2.x-scala2.12'
9+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
>>> cat databricks.yml
3+
bundle:
4+
name: bind-cluster-test-[UNIQUE_NAME]
5+
6+
resources:
7+
clusters:
8+
cluster1:
9+
cluster_name: "DEFAULT Test Cluster"
10+
spark_version: '16.2.x-scala2.12'
11+
12+
13+
>>> [CLI] clusters get [CLUSTER-ID]
14+
{
15+
"cluster_name": "DEFAULT Test Cluster"
16+
}
17+
18+
>>> [CLI] bundle deployment bind cluster1 [CLUSTER-ID] --auto-approve
19+
Updating deployment state...
20+
Successfully bound databricks_cluster with an id '[CLUSTER-ID]'. Run 'bundle deploy' to deploy changes to your workspace
21+
22+
>>> [CLI] bundle deployment unbind cluster1
23+
Updating deployment state...
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CLUSTER_ID="0123-456789-cluster0"
2+
if [ -n "${TEST_DEFAULT_CLUSTER_ID:-}" ]; then
3+
CLUSTER_ID=${TEST_DEFAULT_CLUSTER_ID}
4+
fi
5+
export CLUSTER_ID
6+
envsubst < databricks.yml.tmpl > databricks.yml
7+
trace cat databricks.yml
8+
9+
trace $CLI clusters get ${CLUSTER_ID} | jq '{cluster_name}'
10+
11+
trace $CLI bundle deployment bind cluster1 "${CLUSTER_ID}" --auto-approve
12+
13+
trace $CLI bundle deployment unbind cluster1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Local = true
2+
Cloud = true
3+
RequiresCluster = true
4+
5+
Ignore = [
6+
"databricks.yml",
7+
]
8+
9+
[[Repls]]
10+
Old = "[0-9]{4}-[0-9]{6}-[0-9a-z]{8}"
11+
New = "[CLUSTER-ID]"
12+
13+
[[Server]]
14+
Pattern = "GET /api/2.1/clusters/get"
15+
Response.Body = '''
16+
{
17+
"cluster_name": "DEFAULT Test Cluster"
18+
}
19+
'''

acceptance/internal/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ type TestConfig struct {
3939
// If true and Cloud=true, run this test only if unity catalog is available in the cloud environment
4040
RequiresUnityCatalog *bool
4141

42+
// If true and Cloud=true, run this test only if a default test cluster is available in the cloud environment
43+
RequiresCluster *bool
44+
4245
// List of additional replacements to apply on this test.
4346
// Old is a regexp, New is a replacement expression.
4447
Repls []testdiff.Replacement

bundle/config/resources.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ func (r *Resources) FindResourceByConfigKey(key string) (ConfigResource, error)
124124
}
125125
}
126126

127+
for k := range r.Clusters {
128+
if k == key {
129+
found = append(found, r.Clusters[k])
130+
}
131+
}
132+
127133
if len(found) == 0 {
128134
return nil, fmt.Errorf("no such resource: %s", key)
129135
}

0 commit comments

Comments
 (0)