github.com/opentofu/opentofu@v1.7.1/internal/tofu/hook_stop.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package tofu 7 8 import ( 9 "errors" 10 "sync/atomic" 11 12 "github.com/zclconf/go-cty/cty" 13 14 "github.com/opentofu/opentofu/internal/addrs" 15 "github.com/opentofu/opentofu/internal/plans" 16 "github.com/opentofu/opentofu/internal/providers" 17 "github.com/opentofu/opentofu/internal/states" 18 ) 19 20 // stopHook is a private Hook implementation that OpenTofu uses to 21 // signal when to stop or cancel actions. 22 type stopHook struct { 23 stop uint32 24 } 25 26 var _ Hook = (*stopHook)(nil) 27 28 func (h *stopHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { 29 return h.hook() 30 } 31 32 func (h *stopHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) { 33 return h.hook() 34 } 35 36 func (h *stopHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) { 37 return h.hook() 38 } 39 40 func (h *stopHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) { 41 return h.hook() 42 } 43 44 func (h *stopHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { 45 return h.hook() 46 } 47 48 func (h *stopHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) { 49 return h.hook() 50 } 51 52 func (h *stopHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) { 53 return h.hook() 54 } 55 56 func (h *stopHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) { 57 return h.hook() 58 } 59 60 func (h *stopHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) { 61 } 62 63 func (h *stopHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) { 64 return h.hook() 65 } 66 67 func (h *stopHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) { 68 return h.hook() 69 } 70 71 func (h *stopHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) { 72 return h.hook() 73 } 74 75 func (h *stopHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) { 76 return h.hook() 77 } 78 79 func (h *stopHook) PrePlanImport(addr addrs.AbsResourceInstance, importID string) (HookAction, error) { 80 return h.hook() 81 } 82 83 func (h *stopHook) PostPlanImport(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) { 84 return h.hook() 85 } 86 87 func (h *stopHook) PreApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) { 88 return h.hook() 89 } 90 91 func (h *stopHook) PostApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) { 92 return h.hook() 93 } 94 95 func (h *stopHook) Stopping() {} 96 97 func (h *stopHook) PostStateUpdate(new *states.State) (HookAction, error) { 98 return h.hook() 99 } 100 101 func (h *stopHook) hook() (HookAction, error) { 102 if h.Stopped() { 103 return HookActionHalt, errors.New("execution halted") 104 } 105 106 return HookActionContinue, nil 107 } 108 109 // reset should be called within the lock context 110 func (h *stopHook) Reset() { 111 atomic.StoreUint32(&h.stop, 0) 112 } 113 114 func (h *stopHook) Stop() { 115 atomic.StoreUint32(&h.stop, 1) 116 } 117 118 func (h *stopHook) Stopped() bool { 119 return atomic.LoadUint32(&h.stop) == 1 120 }