add a way to selectively write to RefreshState

All resources use EvalWriteState to store their state, so we need a way
to switch the states when the resource is refreshing vs when it is
planning. (this will likely change once we factor out the EvalNode pattern)
This commit is contained in:
James Bardin 2020-09-04 15:59:44 -04:00
parent ad22e137e6
commit 7b178b1788
2 changed files with 31 additions and 11 deletions

View File

@ -13,6 +13,13 @@ import (
"github.com/hashicorp/terraform/tfdiags"
)
type phaseState int
const (
workingState phaseState = iota
refreshState
)
// EvalReadState is an EvalNode implementation that reads the
// current object for a specific instance in the state.
type EvalReadState struct {
@ -220,6 +227,10 @@ type EvalWriteState struct {
// Dependencies are the inter-resource dependencies to be stored in the
// state.
Dependencies *[]addrs.ConfigResource
// targetState determines which context state we're writing to during plan.
// The default is the global working state.
targetState phaseState
}
func (n *EvalWriteState) Eval(ctx EvalContext) (interface{}, error) {
@ -230,7 +241,15 @@ func (n *EvalWriteState) Eval(ctx EvalContext) (interface{}, error) {
}
absAddr := n.Addr.Absolute(ctx.Path())
state := ctx.State()
var state *states.SyncState
switch n.targetState {
case refreshState:
log.Printf("[TRACE] EvalWriteState: using RefreshState for %s", absAddr)
state = ctx.RefreshState()
default:
state = ctx.State()
}
if n.ProviderAddr.Provider.Type == "" {
return nil, fmt.Errorf("failed to write state for %s: missing provider type", absAddr)

View File

@ -107,8 +107,8 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(addr addrs.AbsRe
var provider providers.Interface
var providerSchema *ProviderSchema
var change *plans.ResourceInstanceChange
var refreshState *states.ResourceInstanceObject
var planState *states.ResourceInstanceObject
var instanceRefreshState *states.ResourceInstanceObject
var instancePlanState *states.ResourceInstanceObject
return &EvalSequence{
Nodes: []EvalNode{
@ -129,10 +129,10 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(addr addrs.AbsRe
Addr: addr.Resource,
Provider: &provider,
ProviderSchema: &providerSchema,
Output: &refreshState,
Output: &instanceRefreshState,
},
&EvalRefreshDependencies{
State: &refreshState,
State: &instanceRefreshState,
Dependencies: &n.Dependencies,
},
&EvalRefresh{
@ -141,14 +141,15 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(addr addrs.AbsRe
Provider: &provider,
ProviderMetas: n.ProviderMetas,
ProviderSchema: &providerSchema,
State: &refreshState,
Output: &refreshState,
State: &instanceRefreshState,
Output: &instanceRefreshState,
},
&EvalWriteState{
Addr: addr.Resource,
ProviderAddr: n.ResolvedProvider,
State: &refreshState,
State: &instanceRefreshState,
ProviderSchema: &providerSchema,
targetState: refreshState,
},
// Plan the instance
@ -160,9 +161,9 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(addr addrs.AbsRe
ProviderAddr: n.ResolvedProvider,
ProviderMetas: n.ProviderMetas,
ProviderSchema: &providerSchema,
State: &refreshState,
State: &instanceRefreshState,
OutputChange: &change,
OutputState: &planState,
OutputState: &instancePlanState,
},
&EvalCheckPreventDestroy{
Addr: addr.Resource,
@ -172,7 +173,7 @@ func (n *NodePlannableResourceInstance) evalTreeManagedResource(addr addrs.AbsRe
&EvalWriteState{
Addr: addr.Resource,
ProviderAddr: n.ResolvedProvider,
State: &planState,
State: &instancePlanState,
ProviderSchema: &providerSchema,
},
&EvalWriteDiff{