From 3276ae313f7035aa13abd9f091a0b9dd72716d73 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 17 Jul 2014 15:38:50 -0700 Subject: [PATCH] terraform: PostApply should get error result from Apply --- command/hook_count.go | 21 +++++++++++++-------- terraform/context.go | 2 +- terraform/hook.go | 7 ++++--- terraform/hook_mock.go | 16 +++++++++------- terraform/hook_stop.go | 2 +- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/command/hook_count.go b/command/hook_count.go index 06f02bc56..152d3170e 100644 --- a/command/hook_count.go +++ b/command/hook_count.go @@ -62,19 +62,24 @@ func (h *CountHook) PreApply( func (h *CountHook) PostApply( id string, - s *terraform.ResourceState) (terraform.HookAction, error) { + s *terraform.ResourceState, + e error) (terraform.HookAction, error) { h.Lock() defer h.Unlock() if h.pending != nil { if a, ok := h.pending[id]; ok { - switch a { - case countHookActionAdd: - h.Added += 1 - case countHookActionChange: - h.Changed += 1 - case countHookActionRemove: - h.Removed += 1 + delete(h.pending, id) + + if e == nil { + switch a { + case countHookActionAdd: + h.Added += 1 + case countHookActionChange: + h.Changed += 1 + case countHookActionRemove: + h.Removed += 1 + } } } } diff --git a/terraform/context.go b/terraform/context.go index 54d870e19..1fa95ae0d 100644 --- a/terraform/context.go +++ b/terraform/context.go @@ -547,7 +547,7 @@ func (c *Context) applyWalkFn() depgraph.WalkFunc { r.State = rs for _, h := range c.hooks { - handleHook(h.PostApply(r.Id, r.State)) + handleHook(h.PostApply(r.Id, r.State, applyerr)) } // Determine the new state and update variables diff --git a/terraform/hook.go b/terraform/hook.go index 4c4c1a776..1e7475951 100644 --- a/terraform/hook.go +++ b/terraform/hook.go @@ -22,9 +22,10 @@ const ( // nothing. Then, override only the functions you want to implement. type Hook interface { // PreApply and PostApply are called before and after a single - // resource is applied. + // resource is applied. The error argument in PostApply is the + // error, if any, that was returned from the provider Apply call itself. PreApply(string, *ResourceState, *ResourceDiff) (HookAction, error) - PostApply(string, *ResourceState) (HookAction, error) + PostApply(string, *ResourceState, error) (HookAction, error) // PreDiff and PostDiff are called before and after a single resource // resource is diffed. @@ -46,7 +47,7 @@ func (*NilHook) PreApply(string, *ResourceState, *ResourceDiff) (HookAction, err return HookActionContinue, nil } -func (*NilHook) PostApply(string, *ResourceState) (HookAction, error) { +func (*NilHook) PostApply(string, *ResourceState, error) (HookAction, error) { return HookActionContinue, nil } diff --git a/terraform/hook_mock.go b/terraform/hook_mock.go index 9baed1d34..e4ff675e4 100644 --- a/terraform/hook_mock.go +++ b/terraform/hook_mock.go @@ -10,11 +10,12 @@ type MockHook struct { PreApplyReturn HookAction PreApplyError error - PostApplyCalled bool - PostApplyId string - PostApplyState *ResourceState - PostApplyReturn HookAction - PostApplyError error + PostApplyCalled bool + PostApplyId string + PostApplyState *ResourceState + PostApplyError error + PostApplyReturn HookAction + PostApplyReturnError error PreDiffCalled bool PreDiffId string @@ -49,11 +50,12 @@ func (h *MockHook) PreApply(n string, s *ResourceState, d *ResourceDiff) (HookAc return h.PreApplyReturn, h.PreApplyError } -func (h *MockHook) PostApply(n string, s *ResourceState) (HookAction, error) { +func (h *MockHook) PostApply(n string, s *ResourceState, e error) (HookAction, error) { h.PostApplyCalled = true h.PostApplyId = n h.PostApplyState = s - return h.PostApplyReturn, h.PostApplyError + h.PostApplyError = e + return h.PostApplyReturn, h.PostApplyReturnError } func (h *MockHook) PreDiff(n string, s *ResourceState) (HookAction, error) { diff --git a/terraform/hook_stop.go b/terraform/hook_stop.go index b0c19aaaf..fc36c38f5 100644 --- a/terraform/hook_stop.go +++ b/terraform/hook_stop.go @@ -14,7 +14,7 @@ func (h *stopHook) PreApply(string, *ResourceState, *ResourceDiff) (HookAction, return h.hook() } -func (h *stopHook) PostApply(string, *ResourceState) (HookAction, error) { +func (h *stopHook) PostApply(string, *ResourceState, error) (HookAction, error) { return h.hook() }