Skip to content

Commit

Permalink
fixup! Add pulumi state delete --target-dependents
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Oct 27, 2022
1 parent 2d2c118 commit 38f1570
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions pkg/cmd/pulumi/state_delete.go
Expand Up @@ -60,28 +60,29 @@ pulumi state delete 'urn:pulumi:stage::demo::eks:index:Cluster$pulumi:providers:
res := runStateEdit(ctx, stack, showPrompt, urn, func(snap *deploy.Snapshot, res *resource.State) error {
var handleProtected func(*resource.State) error
if force {
handleProtected = func(s *resource.State) error {
cmdutil.Diag().Warningf(diag.RawMessage(s.URN, "deleting protected resource due to presence of --force"))
return edit.UnprotectResource(snap, res)
handleProtected = func(res *resource.State) error {
cmdutil.Diag().Warningf(diag.Message(res.URN,
"deleting protected resource %s due to presence of --force"), res.URN)
return edit.UnprotectResource(nil, res)
}
}
return edit.DeleteResource(snap, res, handleProtected, targetDepenedents)
})
if res != nil {
switch e := res.Error().(type) {
case edit.ResourceHasDependenciesError:
message := "This resource can't be safely deleted because the following resources depend on it:\n"
message := string(e.Condemned.URN) + " can't be safely deleted because the following resources depend on it:\n"
for _, dependentResource := range e.Dependencies {
depUrn := dependentResource.URN
message += fmt.Sprintf(" * %-15q (%s)\n", depUrn.Name(), depUrn)
}

message += "\nDelete those resources first before deleting this one."
message += "\nDelete those resources first or pass --target-dependents."
return result.Error(message)
case edit.ResourceProtectedError:
return result.Error(
"This resource can't be safely deleted because it is protected. " +
"Re-run this command with --force to force deletion")
return result.Errorf(
"%s can't be safely deleted because it is protected. "+
"Re-run this command with --force to force deletion", string(e.Condemned.URN))
default:
return res
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/resource/edit/operations.go
Expand Up @@ -36,7 +36,7 @@ type OperationFunc func(*deploy.Snapshot, *resource.State) error
//
// If non-nil, onProtected will be called on all protected resources planed for deletion.
//
// If a resource is marked protected, after onProtected is called, an error instance of
// If a resource is marked protected after onProtected is called, an error instance of
// `ResourceHasDependenciesError` will be returned.
func DeleteResource(
snapshot *deploy.Snapshot, condemnedRes *resource.State,
Expand Down Expand Up @@ -67,8 +67,8 @@ func DeleteResource(
deleteSet := map[resource.URN]bool{
condemnedRes.URN: true,
}
dependencies := dg.DependingOn(condemnedRes, nil, true)
if len(dependencies) != 0 {

if dependencies := dg.DependingOn(condemnedRes, nil, true); len(dependencies) != 0 {
if !targetDependents {
return ResourceHasDependenciesError{Condemned: condemnedRes, Dependencies: dependencies}
}
Expand Down

0 comments on commit 38f1570

Please sign in to comment.