github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/states/statemgr/transient_inmem.go (about)

     1  package statemgr
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/hashicorp/terraform/states"
     7  )
     8  
     9  // NewTransientInMemory returns a Transient implementation that retains
    10  // transient snapshots only in memory, as part of the object.
    11  //
    12  // The given initial state, if any, must not be modified concurrently while
    13  // this function is running, but may be freely modified once this function
    14  // returns without affecting the stored transient snapshot.
    15  func NewTransientInMemory(initial *states.State) Transient {
    16  	return &transientInMemory{
    17  		current: initial.DeepCopy(),
    18  	}
    19  }
    20  
    21  type transientInMemory struct {
    22  	lock    sync.RWMutex
    23  	current *states.State
    24  }
    25  
    26  var _ Transient = (*transientInMemory)(nil)
    27  
    28  func (m *transientInMemory) State() *states.State {
    29  	m.lock.RLock()
    30  	defer m.lock.RUnlock()
    31  
    32  	return m.current.DeepCopy()
    33  }
    34  
    35  func (m *transientInMemory) WriteState(new *states.State) error {
    36  	m.lock.Lock()
    37  	defer m.lock.Unlock()
    38  
    39  	m.current = new.DeepCopy()
    40  	return nil
    41  }