github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/states/statemgr/transient.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package statemgr
     5  
     6  import "github.com/terramate-io/tf/states"
     7  
     8  // Transient is a union of the Reader and Writer interfaces, for types that
     9  // deal with transient snapshots.
    10  //
    11  // Transient snapshots are ones that are generally retained only locally and
    12  // to not create any historical version record when updated. Transient
    13  // snapshots are not expected to outlive a particular Terraform process,
    14  // and are not shared with any other process.
    15  //
    16  // A state manager type that is primarily concerned with persistent storage
    17  // may embed type Transient and then call State from its PersistState and
    18  // WriteState from its RefreshState in order to build on any existing
    19  // Transient implementation, such as the one returned by NewTransientInMemory.
    20  type Transient interface {
    21  	Reader
    22  	Writer
    23  }
    24  
    25  // Reader is the interface for managers that can return transient snapshots
    26  // of state.
    27  //
    28  // Retrieving the snapshot must not fail, so retrieving a snapshot from remote
    29  // storage (for example) should be dealt with elsewhere, often in an
    30  // implementation of Refresher. For a type that implements both Reader
    31  // and Refresher, it is okay for State to return nil if called before
    32  // a RefreshState call has completed.
    33  //
    34  // For a type that implements both Reader and Writer, State must return the
    35  // result of the most recently completed call to WriteState, and the state
    36  // manager must accept concurrent calls to both State and WriteState.
    37  //
    38  // Each caller of this function must get a distinct copy of the state, and
    39  // it must also be distinct from any instance cached inside the reader, to
    40  // ensure that mutations of the returned state will not affect the values
    41  // returned to other callers.
    42  type Reader interface {
    43  	// State returns the latest state.
    44  	//
    45  	// Each call to State returns an entirely-distinct copy of the state, with
    46  	// no storage shared with any other call, so the caller may freely mutate
    47  	// the returned object via the state APIs.
    48  	State() *states.State
    49  }
    50  
    51  // Writer is the interface for managers that can create transient snapshots
    52  // from state.
    53  //
    54  // Writer is the opposite of Reader, and so it must update whatever the State
    55  // method reads from. It does not write the state to any persistent
    56  // storage, and (for managers that support historical versions) must not
    57  // be recorded as a persistent new version of state.
    58  //
    59  // Implementations that cache the state in memory must take a deep copy of it,
    60  // since the caller may continue to modify the given state object after
    61  // WriteState returns.
    62  type Writer interface {
    63  	// WriteState saves a transient snapshot of the given state.
    64  	//
    65  	// The caller must ensure that the given state object is not concurrently
    66  	// modified while a WriteState call is in progress. WriteState itself
    67  	// will never modify the given state.
    68  	WriteState(*states.State) error
    69  }