Skip to content

Commit

Permalink
Merge pull request #28272 from hashicorp/jbardin/plan-destroy-skip-re…
Browse files Browse the repository at this point in the history
…fesh

ensure plans always have a stored state
  • Loading branch information
jbardin committed Apr 1, 2021
2 parents 1e1bb00 + b6b7f78 commit 973ed6e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
4 changes: 3 additions & 1 deletion terraform/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,9 @@ func (c *Context) plan() (*plans.Plan, tfdiags.Diagnostics) {

func (c *Context) destroyPlan() (*plans.Plan, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
destroyPlan := &plans.Plan{}
destroyPlan := &plans.Plan{
State: c.state.DeepCopy(),
}
c.changes = plans.NewChanges()

// A destroy plan starts by running Refresh to read any pending data
Expand Down
44 changes: 44 additions & 0 deletions terraform/context_plan2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,47 @@ resource "test_resource" "b" {
}
}
}

func TestContext2Plan_destroySkipRefresh(t *testing.T) {
m := testModuleInline(t, map[string]string{
"main.tf": `
resource "test_object" "a" {
}
`,
})

p := simpleMockProvider()

addr := mustResourceInstanceAddr("test_object.a")
state := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(addr, &states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"test_string":"foo"}`),
Status: states.ObjectReady,
}, mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`))
})

ctx := testContext2(t, &ContextOpts{
Config: m,
State: state,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
Destroy: true,
SkipRefresh: true,
})

plan, diags := ctx.Plan()
if diags.HasErrors() {
t.Fatal(diags.Err())
}

if plan.State == nil {
t.Fatal("missing plan state")
}

for _, c := range plan.Changes.Resources {
if c.Action != plans.Delete {
t.Errorf("unexpected %s change for %s", c.Action, c.Addr)
}
}
}

0 comments on commit 973ed6e

Please sign in to comment.