From 8ebdc1e786c3f3ef5f5120457a231998c4ebe91c Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 25 Jun 2015 20:11:29 -0700 Subject: [PATCH] terraform: orphans should call post-apply hook [GH-1938] --- command/counthookaction_string.go | 16 +++++++ command/hook_count.go | 8 ---- command/hook_count_action.go | 11 +++++ terraform/context_test.go | 51 +++++++++++++++++++++ terraform/test-fixtures/apply-blank/main.tf | 1 + terraform/transform_orphan.go | 7 +++ 6 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 command/counthookaction_string.go create mode 100644 command/hook_count_action.go create mode 100644 terraform/test-fixtures/apply-blank/main.tf diff --git a/command/counthookaction_string.go b/command/counthookaction_string.go new file mode 100644 index 000000000..8b90dc50b --- /dev/null +++ b/command/counthookaction_string.go @@ -0,0 +1,16 @@ +// generated by stringer -type=countHookAction hook_count_action.go; DO NOT EDIT + +package command + +import "fmt" + +const _countHookAction_name = "countHookActionAddcountHookActionChangecountHookActionRemove" + +var _countHookAction_index = [...]uint8{0, 18, 39, 60} + +func (i countHookAction) String() string { + if i >= countHookAction(len(_countHookAction_index)-1) { + return fmt.Sprintf("countHookAction(%d)", i) + } + return _countHookAction_name[_countHookAction_index[i]:_countHookAction_index[i+1]] +} diff --git a/command/hook_count.go b/command/hook_count.go index 3d3d534dd..12fb229ab 100644 --- a/command/hook_count.go +++ b/command/hook_count.go @@ -24,14 +24,6 @@ type CountHook struct { terraform.NilHook } -type countHookAction byte - -const ( - countHookActionAdd countHookAction = iota - countHookActionChange - countHookActionRemove -) - func (h *CountHook) Reset() { h.Lock() defer h.Unlock() diff --git a/command/hook_count_action.go b/command/hook_count_action.go new file mode 100644 index 000000000..344431dea --- /dev/null +++ b/command/hook_count_action.go @@ -0,0 +1,11 @@ +package command + +//go:generate stringer -type=countHookAction hook_count_action.go + +type countHookAction byte + +const ( + countHookActionAdd countHookAction = iota + countHookActionChange + countHookActionRemove +) diff --git a/terraform/context_test.go b/terraform/context_test.go index b77035e1e..8232d52c7 100644 --- a/terraform/context_test.go +++ b/terraform/context_test.go @@ -5912,6 +5912,57 @@ func TestContext2Apply_hook(t *testing.T) { } } +func TestContext2Apply_hookOrphan(t *testing.T) { + m := testModule(t, "apply-blank") + h := new(MockHook) + p := testProvider("aws") + p.ApplyFn = testApplyFn + p.DiffFn = testDiffFn + + state := &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: rootModulePath, + Resources: map[string]*ResourceState{ + "aws_instance.bar": &ResourceState{ + Type: "aws_instance", + Primary: &InstanceState{ + ID: "bar", + }, + }, + }, + }, + }, + } + + ctx := testContext2(t, &ContextOpts{ + Module: m, + State: state, + Hooks: []Hook{h}, + Providers: map[string]ResourceProviderFactory{ + "aws": testProviderFuncFixed(p), + }, + }) + + if _, err := ctx.Plan(); err != nil { + t.Fatalf("err: %s", err) + } + + if _, err := ctx.Apply(); err != nil { + t.Fatalf("err: %s", err) + } + + if !h.PreApplyCalled { + t.Fatal("should be called") + } + if !h.PostApplyCalled { + t.Fatal("should be called") + } + if !h.PostStateUpdateCalled { + t.Fatalf("should call post state update") + } +} + func TestContext2Apply_idAttr(t *testing.T) { m := testModule(t, "apply-idattr") p := testProvider("aws") diff --git a/terraform/test-fixtures/apply-blank/main.tf b/terraform/test-fixtures/apply-blank/main.tf new file mode 100644 index 000000000..0081db186 --- /dev/null +++ b/terraform/test-fixtures/apply-blank/main.tf @@ -0,0 +1 @@ +// Nothing! diff --git a/terraform/transform_orphan.go b/terraform/transform_orphan.go index f0aceb3b9..8e609f609 100644 --- a/terraform/transform_orphan.go +++ b/terraform/transform_orphan.go @@ -256,6 +256,7 @@ func (n *graphNodeOrphanResource) EvalTree() EvalNode { }) // Apply + var err error seq.Nodes = append(seq.Nodes, &EvalOpFilter{ Ops: []walkOperation{walkApply}, Node: &EvalSequence{ @@ -278,6 +279,7 @@ func (n *graphNodeOrphanResource) EvalTree() EvalNode { Diff: &diff, Provider: &provider, Output: &state, + Error: &err, }, &EvalWriteState{ Name: n.ResourceName, @@ -286,6 +288,11 @@ func (n *graphNodeOrphanResource) EvalTree() EvalNode { Dependencies: n.DependentOn(), State: &state, }, + &EvalApplyPost{ + Info: info, + State: &state, + Error: &err, + }, &EvalUpdateStateHook{}, }, },