From 6173eec852ecd96918b31056830ef02eeb3752e3 Mon Sep 17 00:00:00 2001 From: Jan Rose Date: Wed, 4 Nov 2026 16:05:54 +0000 Subject: [PATCH 1/2] Fail a create that returns no resource id An empty id would be saved as-is, and the next plan would treat the resource as missing and create it again. Co-authored-by: Isaac --- bundle/direct/apply.go | 8 ++++ bundle/direct/apply_test.go | 87 +++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 bundle/direct/apply_test.go diff --git a/bundle/direct/apply.go b/bundle/direct/apply.go index 9cdd5a4f505..dc0751f03b3 100644 --- a/bundle/direct/apply.go +++ b/bundle/direct/apply.go @@ -77,6 +77,10 @@ func (d *DeploymentUnit) Create(ctx context.Context, db *dstate.DeploymentState, // No need to prefix error, there is no ambiguity (only one operation - DoCreate) and no additional context (like id) return err } + // An empty id would be saved as-is, and the next plan would treat the resource as missing and create it again. + if newID == "" { + return errors.New("create returned an empty resource id") + } log.Infof(ctx, "Created %s id=%#v", d.ResourceKey, newID) @@ -216,6 +220,10 @@ func (d *DeploymentUnit) UpdateWithID(ctx context.Context, db *dstate.Deployment if err != nil { return fmt.Errorf("updating id=%s: %w", oldID, err) } + // Same reason as in Create: an empty id must not replace the saved one. + if newID == "" { + return errors.New("update returned an empty resource id") + } if oldID != newID { log.Infof(ctx, "Updated %s id=%#v (previously %#v)", d.ResourceKey, newID, oldID) diff --git a/bundle/direct/apply_test.go b/bundle/direct/apply_test.go new file mode 100644 index 00000000000..24304292ddc --- /dev/null +++ b/bundle/direct/apply_test.go @@ -0,0 +1,87 @@ +package direct + +import ( + "context" + "path/filepath" + "testing" + + "github.com/databricks/cli/bundle/direct/dresources" + "github.com/databricks/cli/bundle/direct/dstate" + "github.com/databricks/cli/libs/structs/structpath" + "github.com/databricks/databricks-sdk-go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// emptyIDResource is a fake resource whose create and update return an empty id. +type emptyIDResource struct{} + +type emptyIDState struct { + Name string `json:"name"` +} + +func (*emptyIDResource) New(client *databricks.WorkspaceClient) any { + return &emptyIDResource{} +} + +func (*emptyIDResource) PrepareState(input *emptyIDState) *emptyIDState { + return input +} + +func (*emptyIDResource) DoRead(ctx context.Context, id string) (*emptyIDState, error) { + return &emptyIDState{}, nil +} + +func (*emptyIDResource) DoCreate(ctx context.Context, newState *emptyIDState) (string, *emptyIDState, error) { + return "", nil, nil +} + +func (*emptyIDResource) DoUpdateWithID(ctx context.Context, id string, newState *emptyIDState, entry *dresources.PlanEntry) (string, *emptyIDState, error) { + return "", nil, nil +} + +func (*emptyIDResource) OverrideChangeDesc(ctx context.Context, path *structpath.PathNode, changedesc *dresources.ChangeDesc, remoteState any) error { + return nil +} + +func newEmptyIDDeploymentState(t *testing.T) *dstate.DeploymentState { + t.Helper() + db := &dstate.DeploymentState{} + path := filepath.Join(t.TempDir(), "resources.json") + require.NoError(t, db.Open(t.Context(), path, dstate.WritingFile, dstate.WithRecovery(true), dstate.OpenDmsArgs{})) + return db +} + +func finalizeEmptyIDState(t *testing.T, db *dstate.DeploymentState) { + t.Helper() + _, err := db.Finalize(t.Context()) + require.NoError(t, err) +} + +func TestCreateFailsOnEmptyID(t *testing.T) { + adapter, err := dresources.NewAdapter((*emptyIDResource)(nil), "fake", nil) + require.NoError(t, err) + db := newEmptyIDDeploymentState(t) + defer finalizeEmptyIDState(t, db) + d := &DeploymentUnit{ResourceKey: "resources.fakes.foo", Adapter: adapter} + + err = d.Create(t.Context(), db, &emptyIDState{Name: "foo"}) + require.EqualError(t, err, "create returned an empty resource id") + + _, ok := db.GetResourceEntry(d.ResourceKey) + assert.False(t, ok) +} + +func TestUpdateWithIDFailsOnEmptyID(t *testing.T) { + adapter, err := dresources.NewAdapter((*emptyIDResource)(nil), "fake", nil) + require.NoError(t, err) + db := newEmptyIDDeploymentState(t) + defer finalizeEmptyIDState(t, db) + d := &DeploymentUnit{ResourceKey: "resources.fakes.foo", Adapter: adapter} + require.NoError(t, db.SaveState(t.Context(), d.ResourceKey, "old-id", &emptyIDState{Name: "foo"}, nil)) + + err = d.UpdateWithID(t.Context(), db, "old-id", &emptyIDState{Name: "bar"}, nil) + require.EqualError(t, err, "update returned an empty resource id") + + assert.Equal(t, "old-id", db.GetResourceID(d.ResourceKey)) +} From cee13defed93f71d7a8233911d493d965613c337 Mon Sep 17 00:00:00 2001 From: Jan Rose Date: Tue, 10 Nov 2026 10:39:19 +0000 Subject: [PATCH 2/2] Add acceptance coverage for empty resource IDs --- .../empty-resource-id/create/databricks.yml | 12 +++++++ .../empty-resource-id/create/out.test.toml | 2 ++ .../empty-resource-id/create/output.txt | 17 +++++++++ .../deploy/empty-resource-id/create/script | 8 +++++ .../deploy/empty-resource-id/create/test.toml | 3 ++ .../empty-resource-id/update/databricks.yml | 12 +++++++ .../empty-resource-id/update/out.test.toml | 2 ++ .../empty-resource-id/update/output.txt | 35 +++++++++++++++++++ .../deploy/empty-resource-id/update/script | 11 ++++++ .../deploy/empty-resource-id/update/test.toml | 3 ++ 10 files changed, 105 insertions(+) create mode 100644 acceptance/bundle/deploy/empty-resource-id/create/databricks.yml create mode 100644 acceptance/bundle/deploy/empty-resource-id/create/out.test.toml create mode 100644 acceptance/bundle/deploy/empty-resource-id/create/output.txt create mode 100644 acceptance/bundle/deploy/empty-resource-id/create/script create mode 100644 acceptance/bundle/deploy/empty-resource-id/create/test.toml create mode 100644 acceptance/bundle/deploy/empty-resource-id/update/databricks.yml create mode 100644 acceptance/bundle/deploy/empty-resource-id/update/out.test.toml create mode 100644 acceptance/bundle/deploy/empty-resource-id/update/output.txt create mode 100644 acceptance/bundle/deploy/empty-resource-id/update/script create mode 100644 acceptance/bundle/deploy/empty-resource-id/update/test.toml diff --git a/acceptance/bundle/deploy/empty-resource-id/create/databricks.yml b/acceptance/bundle/deploy/empty-resource-id/create/databricks.yml new file mode 100644 index 00000000000..aa7e01aea1e --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/create/databricks.yml @@ -0,0 +1,12 @@ +bundle: + name: test-bundle + +sync: + paths: [] + +resources: + volumes: + volume1: + catalog_name: main + schema_name: myschema + name: myvolume diff --git a/acceptance/bundle/deploy/empty-resource-id/create/out.test.toml b/acceptance/bundle/deploy/empty-resource-id/create/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/create/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/deploy/empty-resource-id/create/output.txt b/acceptance/bundle/deploy/empty-resource-id/create/output.txt new file mode 100644 index 00000000000..b11534af573 --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/create/output.txt @@ -0,0 +1,17 @@ + +=== A successful create response without an ID fails the deployment +>>> musterr [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Error: cannot create resources.volumes.volume1: create returned an empty resource id + +Files: 0 uploaded, 0 deleted + +=== The failed create does not save resource state +>>> print_state.py +{} + +=== The next plan still creates the resource +>>> [CLI] bundle plan +create volumes.volume1 + +Plan: 1 to add, 0 to change, 0 to delete, 0 unchanged diff --git a/acceptance/bundle/deploy/empty-resource-id/create/script b/acceptance/bundle/deploy/empty-resource-id/create/script new file mode 100644 index 00000000000..e8155a51794 --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/create/script @@ -0,0 +1,8 @@ +title "A successful create response without an ID fails the deployment" +trace musterr $CLI bundle deploy + +title "The failed create does not save resource state" +trace print_state.py | jq -s '.[0].state // {}' + +title "The next plan still creates the resource" +trace $CLI bundle plan diff --git a/acceptance/bundle/deploy/empty-resource-id/create/test.toml b/acceptance/bundle/deploy/empty-resource-id/create/test.toml new file mode 100644 index 00000000000..14211f5e8b5 --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/create/test.toml @@ -0,0 +1,3 @@ +[[Server]] +Pattern = "POST /api/2.1/unity-catalog/volumes" +Response.Body = '{}' diff --git a/acceptance/bundle/deploy/empty-resource-id/update/databricks.yml b/acceptance/bundle/deploy/empty-resource-id/update/databricks.yml new file mode 100644 index 00000000000..aa7e01aea1e --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/update/databricks.yml @@ -0,0 +1,12 @@ +bundle: + name: test-bundle + +sync: + paths: [] + +resources: + volumes: + volume1: + catalog_name: main + schema_name: myschema + name: myvolume diff --git a/acceptance/bundle/deploy/empty-resource-id/update/out.test.toml b/acceptance/bundle/deploy/empty-resource-id/update/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/update/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/deploy/empty-resource-id/update/output.txt b/acceptance/bundle/deploy/empty-resource-id/update/output.txt new file mode 100644 index 00000000000..9650d45d203 --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/update/output.txt @@ -0,0 +1,35 @@ + +>>> [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Created volumes.volume1 +Files: 0 uploaded, 0 deleted +Resources: 1 created, 0 changed, 0 deleted, 0 unchanged + +=== A successful rename response without an ID fails the deployment +>>> update_file.py databricks.yml myvolume mynewvolume + +>>> musterr [CLI] bundle deploy +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Error: cannot update_id resources.volumes.volume1: update returned an empty resource id + +Files: 0 uploaded, 0 deleted + +=== The failed rename preserves the original ID and state +>>> print_state.py +{ + "resources.volumes.volume1": { + "__id__": "main.myschema.myvolume", + "state": { + "catalog_name": "main", + "name": "myvolume", + "schema_name": "myschema", + "volume_type": "MANAGED" + } + } +} + +=== The next plan still updates the existing resource +>>> [CLI] bundle plan +update volumes.volume1 + +Plan: 0 to add, 1 to change, 0 to delete, 0 unchanged diff --git a/acceptance/bundle/deploy/empty-resource-id/update/script b/acceptance/bundle/deploy/empty-resource-id/update/script new file mode 100644 index 00000000000..167a8d77bdf --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/update/script @@ -0,0 +1,11 @@ +trace $CLI bundle deploy + +title "A successful rename response without an ID fails the deployment" +trace update_file.py databricks.yml myvolume mynewvolume +trace musterr $CLI bundle deploy + +title "The failed rename preserves the original ID and state" +trace print_state.py | jq -S .state + +title "The next plan still updates the existing resource" +trace $CLI bundle plan diff --git a/acceptance/bundle/deploy/empty-resource-id/update/test.toml b/acceptance/bundle/deploy/empty-resource-id/update/test.toml new file mode 100644 index 00000000000..d6454ec15df --- /dev/null +++ b/acceptance/bundle/deploy/empty-resource-id/update/test.toml @@ -0,0 +1,3 @@ +[[Server]] +Pattern = "PATCH /api/2.1/unity-catalog/volumes/main.myschema.myvolume" +Response.Body = '{}'