Skip to content

Commit

Permalink
core: Destroy data resources with "terraform destroy"
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apparentlymart committed May 8, 2016
1 parent 22d114f commit 954ffed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions terraform/eval_read_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions terraform/transform_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit 954ffed

Please sign in to comment.