opentofu/states/statemgr/transient_inmem.go
Martin Atkins 53cafc542b statemgr: New package for state managers
This idea of a "state manager" was previously modelled via the
confusingly-named state.State interface, which we've been calling a "state
manager" only in some local variable names in situations where there were
also *terraform.State variables.

As part of reworking our state models to make room for the new type
system, we also need to change what was previously the state.StateReader
interface. Since we've found the previous organization confusing anyway,
here we just copy all of those interfaces over into statemgr where we can
make the relationship to states.State hopefully a little clearer.

This is not yet a complete move of the functionality from "state", since
we're not yet ready to break existing callers. In a future commit we'll
turn the interfaces in the old "state" package into aliases of the
interfaces in this package, and update all the implementers of what will
by then be statemgr.Reader to use *states.State instead of
*terraform.State.

This also includes an adaptation of what was previously state.LocalState
into statemgr.FileSystem, using the new state serialization functionality
from package statefile instead of the old terraform.ReadState and
terraform.WriteState.
2018-10-16 18:49:20 -07:00

42 lines
951 B
Go

package statemgr
import (
"sync"
"github.com/hashicorp/terraform/states"
)
// NewTransientInMemory returns a Transient implementation that retains
// transient snapshots only in memory, as part of the object.
//
// The given initial state, if any, must not be modified concurrently while
// this function is running, but may be freely modified once this function
// returns without affecting the stored transient snapshot.
func NewTransientInMemory(initial *states.State) Transient {
return &transientInMemory{
current: initial.DeepCopy(),
}
}
type transientInMemory struct {
lock sync.RWMutex
current *states.State
}
var _ Transient = (*transientInMemory)(nil)
func (m *transientInMemory) State() *states.State {
m.lock.RLock()
defer m.lock.RUnlock()
return m.current.DeepCopy()
}
func (m *transientInMemory) WriteState(new *states.State) error {
m.lock.Lock()
defer m.lock.Unlock()
m.current = new.DeepCopy()
return nil
}