opentofu/terraform/hook_stop.go

98 lines
2.8 KiB
Go
Raw Normal View History

2014-07-02 18:16:38 -05:00
package terraform
import (
"errors"
2014-07-03 13:27:30 -05:00
"sync/atomic"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/providers"
"github.com/hashicorp/terraform/states"
2014-07-02 18:16:38 -05:00
)
// stopHook is a private Hook implementation that Terraform uses to
// signal when to stop or cancel actions.
type stopHook struct {
2014-07-03 13:27:30 -05:00
stop uint32
2014-07-02 18:16:38 -05:00
}
var _ Hook = (*stopHook)(nil)
func (h *stopHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
2014-07-02 18:16:38 -05:00
return h.hook()
}
func (h *stopHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) {
2014-07-02 18:16:38 -05:00
return h.hook()
}
func (h *stopHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) {
2014-07-02 18:16:38 -05:00
return h.hook()
}
func (h *stopHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
2014-07-02 18:16:38 -05:00
return h.hook()
}
func (h *stopHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return h.hook()
}
func (h *stopHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return h.hook()
}
func (h *stopHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return h.hook()
}
func (h *stopHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) {
2014-07-27 11:00:34 -05:00
return h.hook()
}
func (h *stopHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) {
}
func (h *stopHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) {
2014-07-02 18:16:38 -05:00
return h.hook()
}
func (h *stopHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) {
2014-07-02 18:16:38 -05:00
return h.hook()
}
func (h *stopHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
return h.hook()
}
func (h *stopHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
return h.hook()
}
func (h *stopHook) PostStateUpdate(new *states.State) (HookAction, error) {
return h.hook()
}
2014-07-02 18:16:38 -05:00
func (h *stopHook) hook() (HookAction, error) {
2014-07-03 13:27:30 -05:00
if h.Stopped() {
return HookActionHalt, errors.New("execution halted")
2014-07-02 18:16:38 -05:00
}
2014-07-03 13:27:30 -05:00
return HookActionContinue, nil
2014-07-02 18:16:38 -05:00
}
// reset should be called within the lock context
2014-07-03 13:27:30 -05:00
func (h *stopHook) Reset() {
atomic.StoreUint32(&h.stop, 0)
2014-07-02 18:16:38 -05:00
}
2014-07-03 13:27:30 -05:00
func (h *stopHook) Stop() {
atomic.StoreUint32(&h.stop, 1)
2014-07-02 18:16:38 -05:00
}
2014-07-03 13:27:30 -05:00
func (h *stopHook) Stopped() bool {
return atomic.LoadUint32(&h.stop) == 1
2014-07-02 18:16:38 -05:00
}