opentofu/internal/terraform/hook_mock.go

334 lines
10 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2014-06-26 18:52:15 -05:00
package terraform
import (
"sync"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/plans"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/states"
)
2014-06-26 18:52:15 -05:00
// MockHook is an implementation of Hook that can be used for tests.
// It records all of its function calls.
type MockHook struct {
sync.Mutex
PreApplyCalled bool
PreApplyAddr addrs.AbsResourceInstance
PreApplyGen states.Generation
PreApplyAction plans.Action
PreApplyPriorState cty.Value
PreApplyPlannedState cty.Value
PreApplyReturn HookAction
PreApplyError error
2014-06-27 00:09:16 -05:00
PostApplyCalled bool
PostApplyAddr addrs.AbsResourceInstance
PostApplyGen states.Generation
PostApplyNewState cty.Value
PostApplyError error
PostApplyReturn HookAction
PostApplyReturnError error
PostApplyFn func(addrs.AbsResourceInstance, states.Generation, cty.Value, error) (HookAction, error)
PreDiffCalled bool
PreDiffAddr addrs.AbsResourceInstance
PreDiffGen states.Generation
PreDiffPriorState cty.Value
PreDiffProposedState cty.Value
PreDiffReturn HookAction
PreDiffError error
PostDiffCalled bool
PostDiffAddr addrs.AbsResourceInstance
PostDiffGen states.Generation
PostDiffAction plans.Action
PostDiffPriorState cty.Value
PostDiffPlannedState cty.Value
PostDiffReturn HookAction
PostDiffError error
PreProvisionInstanceCalled bool
PreProvisionInstanceAddr addrs.AbsResourceInstance
PreProvisionInstanceState cty.Value
PreProvisionInstanceReturn HookAction
PreProvisionInstanceError error
PostProvisionInstanceCalled bool
PostProvisionInstanceAddr addrs.AbsResourceInstance
PostProvisionInstanceState cty.Value
PostProvisionInstanceReturn HookAction
PostProvisionInstanceError error
PreProvisionInstanceStepCalled bool
PreProvisionInstanceStepAddr addrs.AbsResourceInstance
PreProvisionInstanceStepProvisionerType string
PreProvisionInstanceStepReturn HookAction
PreProvisionInstanceStepError error
PostProvisionInstanceStepCalled bool
PostProvisionInstanceStepAddr addrs.AbsResourceInstance
PostProvisionInstanceStepProvisionerType string
PostProvisionInstanceStepErrorArg error
PostProvisionInstanceStepReturn HookAction
PostProvisionInstanceStepError error
ProvisionOutputCalled bool
ProvisionOutputAddr addrs.AbsResourceInstance
ProvisionOutputProvisionerType string
ProvisionOutputMessage string
PreRefreshCalled bool
PreRefreshAddr addrs.AbsResourceInstance
PreRefreshGen states.Generation
PreRefreshPriorState cty.Value
PreRefreshReturn HookAction
PreRefreshError error
PostRefreshCalled bool
PostRefreshAddr addrs.AbsResourceInstance
PostRefreshGen states.Generation
PostRefreshPriorState cty.Value
PostRefreshNewState cty.Value
PostRefreshReturn HookAction
PostRefreshError error
PreImportStateCalled bool
PreImportStateAddr addrs.AbsResourceInstance
PreImportStateID string
PreImportStateReturn HookAction
PreImportStateError error
PostImportStateCalled bool
PostImportStateAddr addrs.AbsResourceInstance
PostImportStateNewStates []providers.ImportedResource
PostImportStateReturn HookAction
PostImportStateError error
PrePlanImportCalled bool
PrePlanImportAddr addrs.AbsResourceInstance
PrePlanImportReturn HookAction
PrePlanImportError error
PostPlanImportAddr addrs.AbsResourceInstance
PostPlanImportCalled bool
PostPlanImportReturn HookAction
PostPlanImportError error
PreApplyImportCalled bool
PreApplyImportAddr addrs.AbsResourceInstance
PreApplyImportReturn HookAction
PreApplyImportError error
PostApplyImportCalled bool
PostApplyImportAddr addrs.AbsResourceInstance
PostApplyImportReturn HookAction
PostApplyImportError error
backend/local: Periodically persist intermediate state snapshots Terraform Core emits a hook event every time it writes a change into the in-memory state. Previously the local backend would just copy that into the transient storage of the state manager, but for most state storage implementations that doesn't really do anything useful because it just makes another copy of the state in memory. We originally added this hook mechanism with the intent of making Terraform _persist_ the state each time, but we backed that out after finding that it was a bit too aggressive and was making the state snapshot history much harder to use in storage systems that can preserve historical snapshots. However, sometimes Terraform gets killed mid-apply for whatever reason and in our previous implementation that meant always losing that transient state, forcing the user to edit the state manually (or use "import") to recover a useful state. In an attempt at finding a sweet spot between these extremes, here we change the rule so that if an apply runs for longer than 20 seconds then we'll try to persist the state to the backend in an update that arrives at least 20 seconds after the first update, and then again for each additional 20 second period as long as Terraform keeps announcing new state snapshots. This also introduces a special interruption mode where if the apply phase gets interrupted by SIGINT (or equivalent) then the local backend will try to persist the state immediately in anticipation of a possibly-imminent SIGKILL, and will then immediately persist any subsequent state update that arrives until the apply phase is complete. After interruption Terraform will not start any new operations and will instead just let any already-running operations run to completion, and so this will persist the state once per resource instance that is able to complete before being killed. This does mean that now long-running applies will generate intermediate state snapshots where they wouldn't before, but there should still be considerably fewer snapshots than were created when we were persisting for each individual state change. We can adjust the 20 second interval in future commits if we find that this spot isn't as sweet as first assumed.
2023-02-13 19:38:24 -06:00
StoppingCalled bool
PostStateUpdateCalled bool
PostStateUpdateState *states.State
PostStateUpdateReturn HookAction
PostStateUpdateError error
2014-06-26 18:52:15 -05:00
}
var _ Hook = (*MockHook)(nil)
func (h *MockHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
h.Lock()
defer h.Unlock()
2014-06-27 00:09:16 -05:00
h.PreApplyCalled = true
h.PreApplyAddr = addr
h.PreApplyGen = gen
h.PreApplyAction = action
h.PreApplyPriorState = priorState
h.PreApplyPlannedState = plannedNewState
2014-06-27 00:09:16 -05:00
return h.PreApplyReturn, h.PreApplyError
}
func (h *MockHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (HookAction, error) {
h.Lock()
defer h.Unlock()
2014-06-27 00:09:16 -05:00
h.PostApplyCalled = true
h.PostApplyAddr = addr
h.PostApplyGen = gen
h.PostApplyNewState = newState
h.PostApplyError = err
if h.PostApplyFn != nil {
return h.PostApplyFn(addr, gen, newState, err)
}
return h.PostApplyReturn, h.PostApplyReturnError
2014-06-27 00:09:16 -05:00
}
func (h *MockHook) PreDiff(addr addrs.AbsResourceInstance, gen states.Generation, priorState, proposedNewState cty.Value) (HookAction, error) {
h.Lock()
defer h.Unlock()
2014-06-26 19:17:10 -05:00
h.PreDiffCalled = true
h.PreDiffAddr = addr
h.PreDiffGen = gen
h.PreDiffPriorState = priorState
h.PreDiffProposedState = proposedNewState
2014-06-26 19:17:10 -05:00
return h.PreDiffReturn, h.PreDiffError
}
func (h *MockHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (HookAction, error) {
h.Lock()
defer h.Unlock()
2014-06-26 19:17:10 -05:00
h.PostDiffCalled = true
h.PostDiffAddr = addr
h.PostDiffGen = gen
h.PostDiffAction = action
h.PostDiffPriorState = priorState
h.PostDiffPlannedState = plannedNewState
2014-06-26 19:17:10 -05:00
return h.PostDiffReturn, h.PostDiffError
}
func (h *MockHook) PreProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PreProvisionInstanceCalled = true
h.PreProvisionInstanceAddr = addr
h.PreProvisionInstanceState = state
return h.PreProvisionInstanceReturn, h.PreProvisionInstanceError
2014-07-27 11:00:34 -05:00
}
func (h *MockHook) PostProvisionInstance(addr addrs.AbsResourceInstance, state cty.Value) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PostProvisionInstanceCalled = true
h.PostProvisionInstanceAddr = addr
h.PostProvisionInstanceState = state
return h.PostProvisionInstanceReturn, h.PostProvisionInstanceError
2014-07-27 11:00:34 -05:00
}
func (h *MockHook) PreProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PreProvisionInstanceStepCalled = true
h.PreProvisionInstanceStepAddr = addr
h.PreProvisionInstanceStepProvisionerType = typeName
return h.PreProvisionInstanceStepReturn, h.PreProvisionInstanceStepError
2014-07-27 11:00:34 -05:00
}
func (h *MockHook) PostProvisionInstanceStep(addr addrs.AbsResourceInstance, typeName string, err error) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PostProvisionInstanceStepCalled = true
h.PostProvisionInstanceStepAddr = addr
h.PostProvisionInstanceStepProvisionerType = typeName
h.PostProvisionInstanceStepErrorArg = err
return h.PostProvisionInstanceStepReturn, h.PostProvisionInstanceStepError
2014-07-27 11:00:34 -05:00
}
func (h *MockHook) ProvisionOutput(addr addrs.AbsResourceInstance, typeName string, line string) {
h.Lock()
defer h.Unlock()
h.ProvisionOutputCalled = true
h.ProvisionOutputAddr = addr
h.ProvisionOutputProvisionerType = typeName
h.ProvisionOutputMessage = line
}
func (h *MockHook) PreRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value) (HookAction, error) {
h.Lock()
defer h.Unlock()
2014-06-26 18:52:15 -05:00
h.PreRefreshCalled = true
h.PreRefreshAddr = addr
h.PreRefreshGen = gen
h.PreRefreshPriorState = priorState
2014-06-26 18:52:15 -05:00
return h.PreRefreshReturn, h.PreRefreshError
}
func (h *MockHook) PostRefresh(addr addrs.AbsResourceInstance, gen states.Generation, priorState cty.Value, newState cty.Value) (HookAction, error) {
h.Lock()
defer h.Unlock()
2014-06-26 18:52:15 -05:00
h.PostRefreshCalled = true
h.PostRefreshAddr = addr
h.PostRefreshPriorState = priorState
h.PostRefreshNewState = newState
2014-06-26 18:52:15 -05:00
return h.PostRefreshReturn, h.PostRefreshError
}
func (h *MockHook) PreImportState(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PreImportStateCalled = true
h.PreImportStateAddr = addr
h.PreImportStateID = importID
return h.PreImportStateReturn, h.PreImportStateError
}
func (h *MockHook) PostImportState(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PostImportStateCalled = true
h.PostImportStateAddr = addr
h.PostImportStateNewStates = imported
return h.PostImportStateReturn, h.PostImportStateError
}
func (h *MockHook) PrePlanImport(addr addrs.AbsResourceInstance, importID string) (HookAction, error) {
h.PrePlanImportCalled = true
h.PrePlanImportAddr = addr
return h.PrePlanImportReturn, h.PrePlanImportError
}
func (h *MockHook) PostPlanImport(addr addrs.AbsResourceInstance, imported []providers.ImportedResource) (HookAction, error) {
h.PostPlanImportCalled = true
h.PostPlanImportAddr = addr
return h.PostPlanImportReturn, h.PostPlanImportError
}
func (h *MockHook) PreApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) {
h.PreApplyImportCalled = true
h.PreApplyImportAddr = addr
return h.PreApplyImportReturn, h.PreApplyImportError
}
func (h *MockHook) PostApplyImport(addr addrs.AbsResourceInstance, importing plans.ImportingSrc) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PostApplyImportCalled = true
h.PostApplyImportAddr = addr
return h.PostApplyImportReturn, h.PostApplyImportError
}
backend/local: Periodically persist intermediate state snapshots Terraform Core emits a hook event every time it writes a change into the in-memory state. Previously the local backend would just copy that into the transient storage of the state manager, but for most state storage implementations that doesn't really do anything useful because it just makes another copy of the state in memory. We originally added this hook mechanism with the intent of making Terraform _persist_ the state each time, but we backed that out after finding that it was a bit too aggressive and was making the state snapshot history much harder to use in storage systems that can preserve historical snapshots. However, sometimes Terraform gets killed mid-apply for whatever reason and in our previous implementation that meant always losing that transient state, forcing the user to edit the state manually (or use "import") to recover a useful state. In an attempt at finding a sweet spot between these extremes, here we change the rule so that if an apply runs for longer than 20 seconds then we'll try to persist the state to the backend in an update that arrives at least 20 seconds after the first update, and then again for each additional 20 second period as long as Terraform keeps announcing new state snapshots. This also introduces a special interruption mode where if the apply phase gets interrupted by SIGINT (or equivalent) then the local backend will try to persist the state immediately in anticipation of a possibly-imminent SIGKILL, and will then immediately persist any subsequent state update that arrives until the apply phase is complete. After interruption Terraform will not start any new operations and will instead just let any already-running operations run to completion, and so this will persist the state once per resource instance that is able to complete before being killed. This does mean that now long-running applies will generate intermediate state snapshots where they wouldn't before, but there should still be considerably fewer snapshots than were created when we were persisting for each individual state change. We can adjust the 20 second interval in future commits if we find that this spot isn't as sweet as first assumed.
2023-02-13 19:38:24 -06:00
func (h *MockHook) Stopping() {
h.Lock()
defer h.Unlock()
h.StoppingCalled = true
}
func (h *MockHook) PostStateUpdate(new *states.State) (HookAction, error) {
h.Lock()
defer h.Unlock()
h.PostStateUpdateCalled = true
h.PostStateUpdateState = new
return h.PostStateUpdateReturn, h.PostStateUpdateError
}