Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/notable-changes/4872.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `lifecycle.prevent_destroy` now also protects a resource after it is removed from the configuration: `bundle deploy` records the flag in the deployment state, and `bundle deploy` and `bundle destroy` refuse to delete a resource whose last deployment had it set. To delete such a resource, add it back with `lifecycle.prevent_destroy: false`, deploy, then remove it again. ([#7499](https://github.com/databricks/cli/pull/7499))
7 changes: 7 additions & 0 deletions acceptance/bundle/dms/prevent-destroy/databricks.yml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
bundle:
name: dms-prevent-destroy-$UNIQUE_NAME
experimental:
deployment_history: true

include:
- resources/*.yml
2 changes: 2 additions & 0 deletions acceptance/bundle/dms/prevent-destroy/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = true
EnvMatrix.DMS = ["true"]
89 changes: 89 additions & 0 deletions acceptance/bundle/dms/prevent-destroy/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@

=== Deploy a protected job: prevent_destroy is recorded in the state the service holds

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-prevent-destroy-[UNIQUE_NAME]/default/files...
Created jobs.foo
Files: 3 uploaded, 0 deleted
Resources: 1 created, 0 changed, 0 deleted, 0 unchanged

>>> print_requests.py --dms //versions/1/operations
{
"method": "PATCH",
"path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/1/operations/jobs.foo",
"q": {
"update_mask": "state,error_message,resource_id,status"
},
"body": {
"error_message": "",
"resource_id": "[FOO_ID]",
"sequence_id": 0,
"state": "{\"state\":{\"deployment\":{\"deployment_id\":\"[DEPLOYMENT_ID]\",\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-prevent-destroy-[UNIQUE_NAME]/default/state/metadata.json\",\"version_id\":\"1\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}},\"prevent_destroy\":true}",
"status": "OPERATION_STATUS_SUCCEEDED"
}
}

=== Removing the job from the config: deploy and destroy refuse to delete it

>>> [CLI] bundle plan
delete jobs.foo

Plan: 0 to add, 0 to change, 1 to delete, 0 unchanged

>>> musterr [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-prevent-destroy-[UNIQUE_NAME]/default/files...
Error: resources.jobs.foo was removed from the configuration, but its last deployment has lifecycle.prevent_destroy set. To delete it, add it back with lifecycle.prevent_destroy: false and deploy, then remove it again

Files: 1 uploaded, 1 deleted

>>> musterr [CLI] bundle destroy --auto-approve
Error: resources.jobs.foo was removed from the configuration, but its last deployment has lifecycle.prevent_destroy set. To delete it, add it back with lifecycle.prevent_destroy: false and deploy, then remove it again


=== Adding it back with prevent_destroy: false leaves the job unchanged, but records the flag as an update of its state

>>> [CLI] bundle plan
Plan: 0 to add, 0 to change, 0 to delete, 1 unchanged

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-prevent-destroy-[UNIQUE_NAME]/default/files...
Files: 1 uploaded, 1 deleted
Resources: 0 created, 0 changed, 0 deleted, 1 unchanged

>>> print_requests.py --dms //versions/2/operations
{
"method": "PATCH",
"path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/2/operations/jobs.foo",
"q": {
"update_mask": "state,error_message,resource_id,status"
},
"body": {
"error_message": "",
"resource_id": "[FOO_ID]",
"sequence_id": 0,
"state": "{\"state\":{\"deployment\":{\"deployment_id\":\"[DEPLOYMENT_ID]\",\"kind\":\"BUNDLE\",\"metadata_file_path\":\"/Workspace/Users/[USERNAME]/.bundle/dms-prevent-destroy-[UNIQUE_NAME]/default/state/metadata.json\",\"version_id\":\"1\"},\"edit_mode\":\"UI_LOCKED\",\"format\":\"MULTI_TASK\",\"max_concurrent_runs\":1,\"name\":\"foo\",\"queue\":{\"enabled\":true}}}",
"status": "OPERATION_STATUS_SUCCEEDED"
}
}

=== The job can now be removed

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/dms-prevent-destroy-[UNIQUE_NAME]/default/files...
Deleted jobs.foo
Files: 0 uploaded, 1 deleted
Resources: 0 created, 0 changed, 1 deleted, 0 unchanged

>>> print_requests.py --dms //versions/3/operations
{
"method": "PATCH",
"path": "/api/2.0/bundle/deployments/[DEPLOYMENT_ID]/versions/3/operations/jobs.foo",
"q": {
"update_mask": "state,error_message,status"
},
"body": {
"error_message": "",
"sequence_id": 0,
"status": "OPERATION_STATUS_SUCCEEDED"
}
}
6 changes: 6 additions & 0 deletions acceptance/bundle/dms/prevent-destroy/resources/job.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources:
jobs:
foo:
name: foo
lifecycle:
prevent_destroy: true
26 changes: 26 additions & 0 deletions acceptance/bundle/dms/prevent-destroy/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
envsubst < databricks.yml.tmpl > databricks.yml

title "Deploy a protected job: prevent_destroy is recorded in the state the service holds"
trace $CLI bundle deploy
replace_ids.py
deployment_id=$(MSYS_NO_PATHCONV=1 $CLI workspace get-status "/Workspace/Users/${CURRENT_USER_NAME}/.bundle/dms-prevent-destroy-${UNIQUE_NAME}/default/state/resources.deployment.json" | python3 -c 'import sys, json; print(json.load(sys.stdin)["object_id"])')
add_repl "$deployment_id" DEPLOYMENT_ID
trace print_requests.py --dms //versions/1/operations

title "Removing the job from the config: deploy and destroy refuse to delete it"
mv resources removed
trace $CLI bundle plan
trace musterr $CLI bundle deploy
trace musterr $CLI bundle destroy --auto-approve

title "Adding it back with prevent_destroy: false leaves the job unchanged, but records the flag as an update of its state"
mv removed resources
update_file.py resources/job.yml 'prevent_destroy: true' 'prevent_destroy: false'
trace $CLI bundle plan
trace $CLI bundle deploy
trace print_requests.py --dms //versions/2/operations

title "The job can now be removed"
rm resources/job.yml
trace $CLI bundle deploy
trace print_requests.py --dms //versions/3/operations
42 changes: 42 additions & 0 deletions acceptance/bundle/lifecycle/prevent-destroy/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,48 @@ Recreated schemas.my_schema
Files: 3 uploaded, 0 deleted
Resources: 2 created, 0 changed, 2 deleted, 0 unchanged

>>> [CLI] bundle plan -o json
{
"resources.pipelines.my_pipelines": {
"action": "skip",
"prevent_destroy": true,
"prevent_destroy_changed": true
},
"resources.schemas.my_schema": {
"action": "skip",
"prevent_destroy": true,
"prevent_destroy_changed": true
}
}

>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/prevent-destroy/default/files...
Files: 3 uploaded, 0 deleted
Resources: 0 created, 0 changed, 0 deleted, 2 unchanged

>>> errcode [CLI] bundle plan
delete pipelines.my_pipelines
delete schemas.my_schema

Plan: 0 to add, 0 to change, 2 to delete, 0 unchanged

>>> musterr [CLI] bundle deploy --auto-approve
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/prevent-destroy/default/files...
Error: resources.pipelines.my_pipelines was removed from the configuration, but its last deployment has lifecycle.prevent_destroy set. To delete it, add it back with lifecycle.prevent_destroy: false and deploy, then remove it again
resources.schemas.my_schema was removed from the configuration, but its last deployment has lifecycle.prevent_destroy set. To delete it, add it back with lifecycle.prevent_destroy: false and deploy, then remove it again

Files: 3 uploaded, 2 deleted

>>> musterr [CLI] bundle destroy --auto-approve
Error: resources.pipelines.my_pipelines was removed from the configuration, but its last deployment has lifecycle.prevent_destroy set. To delete it, add it back with lifecycle.prevent_destroy: false and deploy, then remove it again
resources.schemas.my_schema was removed from the configuration, but its last deployment has lifecycle.prevent_destroy set. To delete it, add it back with lifecycle.prevent_destroy: false and deploy, then remove it again


>>> [CLI] bundle deploy
Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/prevent-destroy/default/files...
Files: 3 uploaded, 2 deleted
Resources: 0 created, 0 changed, 0 deleted, 2 unchanged

>>> errcode [CLI] bundle plan
delete pipelines.my_pipelines
delete schemas.my_schema
Expand Down
16 changes: 15 additions & 1 deletion acceptance/bundle/lifecycle/prevent-destroy/script
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,25 @@ update_file.py resources/pipeline.yml 'prevent_destroy: true' 'prevent_destroy:
update_file.py resources/schema.yml 'prevent_destroy: true' 'prevent_destroy: false'
trace errcode $CLI bundle plan >>out.txt 2>&1
trace $CLI bundle deploy --auto-approve >>out.txt 2>&1

# Setting prevent_destroy back to true leaves the resources unchanged, but the plan marks them
# so that the deploy records the flag in the state
update_file.py resources/pipeline.yml 'prevent_destroy: false' 'prevent_destroy: true'
update_file.py resources/schema.yml 'prevent_destroy: false' 'prevent_destroy: true'
{ trace $CLI bundle plan -o json | jq '.plan | map_values({action, prevent_destroy, prevent_destroy_changed})'; } >>out.txt 2>&1
trace $CLI bundle deploy >>out.txt 2>&1

# Removing the pipeline and schema: their last deployment had prevent_destroy set, so neither deploy nor destroy may delete them
mv resources removed
trace errcode $CLI bundle plan >>out.txt 2>&1
trace musterr $CLI bundle deploy --auto-approve >>out.txt 2>&1
trace musterr $CLI bundle destroy --auto-approve >>out.txt 2>&1

# Removing the pipeline and schema, deploy must succeed
# Adding them back with prevent_destroy: false and deploying records that; only then can they be removed
mv removed resources
update_file.py resources/pipeline.yml 'prevent_destroy: true' 'prevent_destroy: false'
update_file.py resources/schema.yml 'prevent_destroy: true' 'prevent_destroy: false'
trace $CLI bundle deploy >>out.txt 2>&1
rm resources/pipeline.yml resources/schema.yml
trace errcode $CLI bundle plan >>out.txt 2>&1
trace $CLI bundle deploy --auto-approve >>out.txt 2>&1
7 changes: 7 additions & 0 deletions bundle/deployplan/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ type Action struct {
// StateOnly mirrors PlanEntry.StateOnly: applying the action only changes the state
// (a delete of a resource without DoDelete, or a re-key).
StateOnly bool
// PreventDestroy mirrors PlanEntry.PreventDestroy: lifecycle.prevent_destroy as
// configured or, for a resource no longer in the configuration, as recorded by
// its last deployment.
PreventDestroy bool
// PreventDestroyChanged mirrors PlanEntry.PreventDestroyChanged: the skip still
// rewrites the state entry with the new PreventDestroy.
PreventDestroyChanged bool
}

func (a Action) String() string {
Expand Down
27 changes: 19 additions & 8 deletions bundle/deployplan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,19 @@ type PlanEntry struct {
// - an Update of an otherwise unchanged resource whose state lacks its internal ID (recorded
// before the ID was, or by bind): applying it records the ID from the remote state. It is
// likewise left out of the textual plan and counted as unchanged.
StateOnly bool `json:"state_only,omitempty"`
NewState *structvar.StructVarJSON `json:"new_state,omitempty"`
RemoteState any `json:"remote_state,omitempty"`
Changes Changes `json:"changes,omitempty"`
StateOnly bool `json:"state_only,omitempty"`
// PreventDestroy is the resource's lifecycle.prevent_destroy: as configured, or for a Delete
// entry of a resource the configuration no longer declares, as its last deployment recorded
// it in the state. Applying the entry persists it with the resource state so that a later
// plan can still read it once the resource is removed from the configuration.
PreventDestroy bool `json:"prevent_destroy,omitempty"`
// PreventDestroyChanged is set on Skip entries whose recorded PreventDestroy differs from
// the configured one. The resource itself is unchanged, so applying the entry only rewrites
// its state entry; no backend call is made.
PreventDestroyChanged bool `json:"prevent_destroy_changed,omitempty"`
NewState *structvar.StructVarJSON `json:"new_state,omitempty"`
RemoteState any `json:"remote_state,omitempty"`
Changes Changes `json:"changes,omitempty"`
}

// IsStateOnly reports whether applying this entry makes no backend call and only changes the
Expand Down Expand Up @@ -267,10 +276,12 @@ func (p *Plan) GetActions() []Action {
actions := make([]Action, 0, len(p.Plan))
for key, entry := range p.Plan {
actions = append(actions, Action{
ResourceKey: key,
ActionType: entry.Action,
Gone: entry.Gone,
StateOnly: entry.StateOnly,
ResourceKey: key,
ActionType: entry.Action,
Gone: entry.Gone,
StateOnly: entry.StateOnly,
PreventDestroy: entry.PreventDestroy,
PreventDestroyChanged: entry.PreventDestroyChanged,
})
}

Expand Down
28 changes: 20 additions & 8 deletions bundle/direct/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (d *DeploymentUnit) Create(ctx context.Context, db *dstate.DeploymentState,
return err
}

err = d.saveState(ctx, db, newID, newState, d.DependsOn, nil)
err = d.saveState(ctx, db, newID, newState, nil)
if err != nil {
return fmt.Errorf("saving state after creating id=%s: %w", newID, err)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (d *DeploymentUnit) Update(ctx context.Context, db *dstate.DeploymentState,
return fmt.Errorf("deleting state id=%s: %w", id, err)
}
} else {
err = d.saveState(ctx, db, id, newState, d.DependsOn, planEntry.RemoteState)
err = d.saveState(ctx, db, id, newState, planEntry.RemoteState)
if err != nil {
return fmt.Errorf("saving state id=%s: %w", id, err)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func (d *DeploymentUnit) UpdateWithID(ctx context.Context, db *dstate.Deployment
return err
}

err = d.saveState(ctx, db, newID, newState, d.DependsOn, planEntry.RemoteState)
err = d.saveState(ctx, db, newID, newState, planEntry.RemoteState)
if err != nil {
return fmt.Errorf("saving state id=%s: %w", oldID, err)
}
Expand Down Expand Up @@ -276,7 +276,7 @@ func (d *DeploymentUnit) Rekey(ctx context.Context, db *dstate.DeploymentState,
}
log.Infof(ctx, "Re-keyed %s id=%#v (previously %#v)", d.ResourceKey, newID, oldID)

err = d.saveState(ctx, db, newID, newState, d.DependsOn, planEntry.RemoteState)
err = d.saveState(ctx, db, newID, newState, planEntry.RemoteState)
if err != nil {
return fmt.Errorf("saving state id=%s: %w", newID, err)
}
Expand All @@ -286,7 +286,7 @@ func (d *DeploymentUnit) Rekey(ctx context.Context, db *dstate.DeploymentState,
// RecordState saves the state at its ID without a backend call. A state-only update does this to
// record the internal ID the state lacks, from the plan's remote state.
func (d *DeploymentUnit) RecordState(ctx context.Context, db *dstate.DeploymentState, id string, newState any, planEntry *deployplan.PlanEntry) error {
err := d.saveState(ctx, db, id, newState, d.DependsOn, planEntry.RemoteState)
err := d.saveState(ctx, db, id, newState, planEntry.RemoteState)
if err != nil {
return fmt.Errorf("saving state id=%s: %w", id, err)
}
Expand Down Expand Up @@ -365,14 +365,26 @@ func (d *DeploymentUnit) Resize(ctx context.Context, db *dstate.DeploymentState,
return fmt.Errorf("resizing id=%s: %w", id, err)
}

err = d.saveState(ctx, db, id, newState, d.DependsOn, entry.RemoteState)
err = d.saveState(ctx, db, id, newState, entry.RemoteState)
if err != nil {
return fmt.Errorf("saving state id=%s: %w", id, err)
}

return nil
}

// RecordPreventDestroy rewrites the resource's state entry with the planned lifecycle.prevent_destroy,
// keeping its id and state. For a resource the plan skips: nothing is sent to the backend, but the
// flag changed and the next plan must read the new value from the state.
func (d *DeploymentUnit) RecordPreventDestroy(ctx context.Context, db *dstate.DeploymentState) error {
entry, ok := db.GetResourceEntry(d.ResourceKey)
if !ok {
return errors.New("state entry not found")
}
log.Infof(ctx, "Recording lifecycle.prevent_destroy=%t for %s id=%#v", d.PreventDestroy, d.ResourceKey, entry.ID)
return db.SaveState(ctx, d.ResourceKey, entry.ID, entry.State, d.DependsOn, d.PreventDestroy)
}

// saveState compacts the state (replacing fields declared in hashed_fields
// with content hashes, see dresources.CompactState) before persisting it. Fields already
// smaller than a hash placeholder are persisted as is. Sensitive fields are replaced by a
Expand All @@ -381,7 +393,7 @@ func (d *DeploymentUnit) Resize(ctx context.Context, db *dstate.DeploymentState,
// It first records the internal ID in state (see ResourceLifecycleConfig.InternalIDField), from the
// remote state the operation returned or else from planRemote, the object as the plan read it (nil
// for a new object).
func (d *DeploymentUnit) saveState(ctx context.Context, db *dstate.DeploymentState, newID string, state any, dependsOn []deployplan.DependsOnEntry, planRemote any) error {
func (d *DeploymentUnit) saveState(ctx context.Context, db *dstate.DeploymentState, newID string, state, planRemote any) error {
remote := d.RemoteState
if remote == nil {
remote = planRemote
Expand All @@ -397,7 +409,7 @@ func (d *DeploymentUnit) saveState(ctx context.Context, db *dstate.DeploymentSta
if err := zeroSensitiveFields(d.Adapter, compacted); err != nil {
return fmt.Errorf("redacting state: %w", err)
}
return db.SaveState(ctx, d.ResourceKey, newID, compacted, dependsOn)
return db.SaveState(ctx, d.ResourceKey, newID, compacted, d.DependsOn, d.PreventDestroy)
}

func parseState(destType reflect.Type, raw json.RawMessage) (any, error) {
Expand Down
Loading
Loading