From 954ffed693e44f413428a8b2c46188c802c5893a Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sun, 8 May 2016 01:27:46 -0700 Subject: [PATCH] core: Destroy data resources with "terraform destroy" Previously they would get left behind in the state because we had no support for planning their destruction. Now we'll create a "destroy" plan and act on it by just producing an empty state on apply, thus ensuring that the data resources don't get left behind in the state after everything else is gone. --- terraform/eval_read_data.go | 10 ++++++++++ terraform/transform_resource.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/terraform/eval_read_data.go b/terraform/eval_read_data.go index 62baa3c03671..9625bcffae71 100644 --- a/terraform/eval_read_data.go +++ b/terraform/eval_read_data.go @@ -80,6 +80,16 @@ func (n *EvalReadDataApply) Eval(ctx EvalContext) (interface{}, error) { provider := *n.Provider diff := *n.Diff + // If the diff is for *destroying* this resource then we'll + // just drop its state and move on, since data resources don't + // support an actual "destroy" action. + if diff.Destroy { + if n.Output != nil { + *n.Output = nil + } + return nil, nil + } + // For the purpose of external hooks we present a data apply as a // "Refresh" rather than an "Apply" because creating a data source // is presented to users/callers as a "read" operation. diff --git a/terraform/transform_resource.go b/terraform/transform_resource.go index 3150e4f2a2c8..da1471f874f4 100644 --- a/terraform/transform_resource.go +++ b/terraform/transform_resource.go @@ -735,6 +735,34 @@ func (n *graphNodeExpandedResource) dataResourceEvalNodes(resource *Resource, in }, }) + // Diff the resource for destruction + nodes = append(nodes, &EvalOpFilter{ + Ops: []walkOperation{walkPlanDestroy}, + Node: &EvalSequence{ + Nodes: []EvalNode{ + + &EvalReadState{ + Name: n.stateId(), + Output: &state, + }, + + // Since EvalDiffDestroy doesn't interact with the + // provider at all, we can safely share the same + // implementation for data vs. managed resources. + &EvalDiffDestroy{ + Info: info, + State: &state, + Output: &diff, + }, + + &EvalWriteDiff{ + Name: n.stateId(), + Diff: &diff, + }, + }, + }, + }) + // Apply nodes = append(nodes, &EvalOpFilter{ Ops: []walkOperation{walkApply, walkDestroy},