Skip to content

Commit aff0ffa

Browse files
authored
do: fix --dry-run for delete (#23809)
`--dry-run` is a global flag for `pulumi do`. However there's no provider support for this in `read`, `list` and most importantly for `delete`. In the case of `delete`, the user can currently pass a `--dry-run` flag, which then rather than `--dry-run` is effectively interpreted as `--yes`, deleting the resource directly. Just reject the flag when it is passed to one of those three commands.
1 parent c9a8790 commit aff0ffa

2 files changed

Lines changed: 71 additions & 2 deletions

File tree

‎pkg/cmd/pulumi/do/do_resource.go‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,13 @@ func (pc *packageCommand) newResourceDeleteCommand(res *schema.Resource) *cobra.
325325
}
326326
urn := resourceURN(res)
327327
id := resource.ID(args[0])
328-
if err := pc.confirm(cmd, formatDeleteSummary(res, id), "delete", yes); err != nil {
328+
if err := pc.confirm(cmd, formatDeleteSummary(res, id, pc.dryrun), string(id), yes); err != nil {
329329
return err
330330
}
331+
// The provider protocol has no preview mode for Delete, so the summary above is the whole dry run.
332+
if pc.dryrun {
333+
return nil
334+
}
331335
_, err := pc.provider.Delete(ctx, plugin.DeleteRequest{
332336
URN: urn,
333337
Name: urn.Name(),
@@ -514,7 +518,10 @@ func formatCreateSummary(res *schema.Resource, inputs resource.PropertyMap, show
514518
return fmt.Sprintf("This will create %s with the following inputs:\n%s", res.Token, body), nil
515519
}
516520

517-
func formatDeleteSummary(res *schema.Resource, id resource.ID) string {
521+
func formatDeleteSummary(res *schema.Resource, id resource.ID, dryrun bool) string {
522+
if dryrun {
523+
return fmt.Sprintf("This would delete %s %q.", res.Token, id)
524+
}
518525
return fmt.Sprintf("This will delete %s %q.", res.Token, id)
519526
}
520527

‎pkg/cmd/pulumi/do/do_resource_test.go‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,68 @@ func TestDoCmdResourceNonInteractiveRequiresYes(t *testing.T) {
607607
}
608608
}
609609

610+
// TestDoCmdResourceDeleteDryRun asserts that delete --dry-run prints what would be deleted and never
611+
// calls the provider. Delete has no provider-side preview mode, so the summary is the whole dry run;
612+
// crucially --dry-run must not act like --yes (the confirmation prompt is skipped on dry runs).
613+
func TestDoCmdResourceDeleteDryRun(t *testing.T) {
614+
t.Parallel()
615+
616+
cmd, stdout, stderr := newDoResourceCommand(t, &testProvider{
617+
spec: doResourceSpec(false),
618+
MockProvider: plugin.MockProvider{
619+
DeleteF: func(ctx context.Context, req plugin.DeleteRequest) (plugin.DeleteResponse, error) {
620+
require.Fail(t, "Delete should not be called with --dry-run")
621+
return plugin.DeleteResponse{}, nil
622+
},
623+
},
624+
})
625+
cmd.SetArgs([]string{"--dry-run", "--stateless", "azure:index:myResource", "delete", "res-1"})
626+
require.NoError(t, cmd.Execute())
627+
assert.Contains(t, stderr.String(), `This would delete azure:index:myResource "res-1"`)
628+
assert.Empty(t, stdout.String())
629+
}
630+
631+
// TestDoCmdResourceDryRunIgnoredForReadOnlyOps asserts that read and list ignore --dry-run: they never
632+
// mutate anything, so there is nothing to preview and they behave as if the flag wasn't passed.
633+
func TestDoCmdResourceDryRunIgnoredForReadOnlyOps(t *testing.T) {
634+
t.Parallel()
635+
636+
t.Run("read", func(t *testing.T) {
637+
t.Parallel()
638+
cmd, stdout, _ := newDoResourceCommand(t, &testProvider{
639+
spec: doResourceSpec(false),
640+
MockProvider: plugin.MockProvider{
641+
ReadF: func(ctx context.Context, req plugin.ReadRequest) (plugin.ReadResponse, error) {
642+
return plugin.ReadResponse{
643+
ReadResult: plugin.ReadResult{
644+
ID: req.ID,
645+
Outputs: resource.PropertyMap{"name": resource.NewProperty("read")},
646+
},
647+
}, nil
648+
},
649+
},
650+
})
651+
cmd.SetArgs([]string{"--dry-run", "azure:index:myResource", "read", "res-1"})
652+
require.NoError(t, cmd.Execute())
653+
assert.JSONEq(t, `{"id":"res-1","name":"read"}`, stdout.String())
654+
})
655+
656+
t.Run("list", func(t *testing.T) {
657+
t.Parallel()
658+
cmd, stdout, _ := newDoResourceCommand(t, &testProvider{
659+
spec: doResourceSpec(true),
660+
MockProvider: plugin.MockProvider{
661+
ListF: func(ctx context.Context, req plugin.ListRequest) (*plugin.ListStream, error) {
662+
return plugin.NewListStream([]plugin.ListResult{{ID: "1", Name: "one"}}, ""), nil
663+
},
664+
},
665+
})
666+
cmd.SetArgs([]string{"--dry-run", "azure:index:myResource", "list"})
667+
require.NoError(t, cmd.Execute())
668+
assert.JSONEq(t, `[{"id":"1","name":"one"}]`, stdout.String())
669+
})
670+
}
671+
610672
// TestDoCmdResourceConfirmationSummary asserts the operation summary lands on stderr (so stdout stays a clean
611673
// JSON channel for piping) and that the patch summary surfaces the Diff response.
612674
func TestDoCmdResourceConfirmationSummary(t *testing.T) {

0 commit comments

Comments
 (0)