terraform: introduce EvalApplyPre so that PreApply is called even for

destroy provisioners.
This commit is contained in:
Mitchell Hashimoto 2017-01-20 20:36:53 -08:00
parent f40fdde708
commit d3df7874d5
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
7 changed files with 64 additions and 10 deletions

View File

@ -52,16 +52,6 @@ func (n *EvalApply) Eval(ctx EvalContext) (interface{}, error) {
*n.CreateNew = state.ID == "" && !diff.GetDestroy() || diff.RequiresNew()
}
{
// Call pre-apply hook
err := ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreApply(n.Info, state, diff)
})
if err != nil {
return nil, err
}
}
// With the completed diff, apply!
log.Printf("[DEBUG] apply: %s: executing Apply", n.Info.Id)
state, err := provider.Apply(n.Info, state, diff)
@ -104,6 +94,37 @@ func (n *EvalApply) Eval(ctx EvalContext) (interface{}, error) {
return nil, nil
}
// EvalApplyPre is an EvalNode implementation that does the pre-Apply work
type EvalApplyPre struct {
Info *InstanceInfo
State **InstanceState
Diff **InstanceDiff
}
// TODO: test
func (n *EvalApplyPre) Eval(ctx EvalContext) (interface{}, error) {
state := *n.State
diff := *n.Diff
// If the state is nil, make it non-nil
if state == nil {
state = new(InstanceState)
}
state.init()
{
// Call post-apply hook
err := ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreApply(n.Info, state, diff)
})
if err != nil {
return nil, err
}
}
return nil, nil
}
// EvalApplyPost is an EvalNode implementation that does the post-Apply work
type EvalApplyPost struct {
Info *InstanceInfo

View File

@ -298,6 +298,12 @@ func (n *NodeApplyableResource) evalTreeManagedResource(
Name: stateId,
Output: &state,
},
// Call pre-apply hook
&EvalApplyPre{
Info: info,
State: &state,
Diff: &diffApply,
},
&EvalApply{
Info: info,
State: &state,

View File

@ -172,6 +172,13 @@ func (n *NodeDestroyResource) EvalTree() EvalNode {
State: &state,
},
// Call pre-apply hook
&EvalApplyPre{
Info: info,
State: &state,
Diff: &diffApply,
},
// Run destroy provisioners if not tainted
&EvalIf{
If: func(ctx EvalContext) (bool, error) {

View File

@ -179,6 +179,10 @@ func (h *HookRecordApplyOrder) PreApply(
info *InstanceInfo,
s *InstanceState,
d *InstanceDiff) (HookAction, error) {
if d.Empty() {
return HookActionContinue, nil
}
if h.Active {
h.l.Lock()
defer h.l.Unlock()

View File

@ -127,6 +127,12 @@ func (n *graphNodeDeposedResource) EvalTree() EvalNode {
State: &state,
Output: &diff,
},
// Call pre-apply hook
&EvalApplyPre{
Info: info,
State: &state,
Diff: &diff,
},
&EvalApply{
Info: info,
State: &state,

View File

@ -321,6 +321,11 @@ func (n *graphNodeOrphanResource) managedResourceEvalNodes(info *InstanceInfo) [
Name: n.ResourceKey.String(),
Output: &state,
},
&EvalApplyPre{
Info: info,
State: &state,
Diff: &diff,
},
&EvalApply{
Info: info,
State: &state,

View File

@ -500,6 +500,11 @@ func (n *graphNodeExpandedResource) managedResourceEvalNodes(resource *Resource,
Name: n.stateId(),
Output: &state,
},
&EvalApplyPre{
Info: info,
State: &state,
Diff: &diffApply,
},
&EvalApply{
Info: info,
State: &state,