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

     1  package statemgr
     2  
     3  import (
     4  	version "github.com/hashicorp/go-version"
     5  )
     6  
     7  // Persistent is a union of the Refresher and Persistent interfaces, for types
     8  // that deal with persistent snapshots.
     9  //
    10  // Persistent snapshots are ones that are retained in storage that will
    11  // outlive a particular Terraform process, and are shared with other Terraform
    12  // processes that have a similarly-configured state manager.
    13  //
    14  // A manager may also choose to retain historical persistent snapshots, but
    15  // that is an implementation detail and not visible via this API.
    16  type Persistent interface {
    17  	Refresher
    18  	Persister
    19  }
    20  
    21  // Refresher is the interface for managers that can read snapshots from
    22  // persistent storage.
    23  //
    24  // Refresher is usually implemented in conjunction with Reader, with
    25  // RefreshState copying the latest persistent snapshot into the latest
    26  // transient snapshot.
    27  //
    28  // For a type that implements both Refresher and Persister, RefreshState must
    29  // return the result of the most recently completed successful call to
    30  // PersistState, unless another concurrently-running process has persisted
    31  // another snapshot in the mean time.
    32  //
    33  // The Refresher implementation must guarantee that the snapshot is read
    34  // from persistent storage in a way that is safe under concurrent calls to
    35  // PersistState that may be happening in other processes.
    36  type Refresher interface {
    37  	// RefreshState retrieves a snapshot of state from persistent storage,
    38  	// returning an error if this is not possible. If a snapshot is retrieved
    39  	// but is from an incompatible Terraform version, this will also result
    40  	// in an error.
    41  	//
    42  	// Types that implement RefreshState generally also implement a State
    43  	// method that returns the result of the latest successful refresh.
    44  	//
    45  	// Since only a subset of the data in a state is included when persisting,
    46  	// a round-trip through PersistState and then RefreshState will often
    47  	// return only a subset of what was written. Callers must assume that
    48  	// ephemeral portions of the state may be unpopulated after calling
    49  	// RefreshState.
    50  	RefreshState() error
    51  
    52  	// RefreshStateWithoutCheckVersion is similar to RefreshState, with the
    53  	// difference that it does not perform a Terraform version check of the
    54  	// state snapshot. Use with caution, as there is no guarantee that the
    55  	// state version retrieved is fully compatible.
    56  	RefreshStateWithoutCheckVersion() error
    57  }
    58  
    59  // Persister is the interface for managers that can write snapshots to
    60  // persistent storage.
    61  //
    62  // Persister is usually implemented in conjunction with Writer, with
    63  // PersistState copying the latest transient snapshot to be the new latest
    64  // persistent snapshot.
    65  //
    66  // A Persister implementation must detect updates made by other processes
    67  // that may be running concurrently and avoid destroying those changes. This
    68  // is most commonly achieved by making use of atomic write capabilities on
    69  // the remote storage backend in conjunction with book-keeping with the
    70  // Serial and Lineage fields in the standard state file formats.
    71  type Persister interface {
    72  	PersistState() error
    73  }
    74  
    75  // PersistentMeta is an optional extension to Persistent that allows inspecting
    76  // the metadata associated with the snapshot that was most recently either
    77  // read by RefreshState or written by PersistState.
    78  type PersistentMeta interface {
    79  	// StateSnapshotMeta returns metadata about the state snapshot most
    80  	// recently created either by a call to PersistState or read by a call
    81  	// to RefreshState.
    82  	//
    83  	// If no persistent snapshot is yet available in the manager then
    84  	// the return value is meaningless. This method is primarily available
    85  	// for testing and logging purposes, and is of little use otherwise.
    86  	StateSnapshotMeta() SnapshotMeta
    87  }
    88  
    89  // SnapshotMeta contains metadata about a persisted state snapshot.
    90  //
    91  // This metadata is usually (but not necessarily) included as part of the
    92  // "header" of a state file, which is then written to a raw blob storage medium
    93  // by a persistent state manager.
    94  //
    95  // Not all state managers will have useful values for all fields in this
    96  // struct, so SnapshotMeta values are of little use beyond testing and logging
    97  // use-cases.
    98  type SnapshotMeta struct {
    99  	// Lineage and Serial can be used to understand the relationships between
   100  	// snapshots.
   101  	//
   102  	// If two snapshots both have an identical, non-empty Lineage
   103  	// then the one with the higher Serial is newer than the other.
   104  	// If the Lineage values are different or empty then the two snapshots
   105  	// are unrelated and cannot be compared for relative age.
   106  	Lineage string
   107  	Serial  uint64
   108  
   109  	// TerraformVersion is the number of the version of Terraform that created
   110  	// the snapshot.
   111  	TerraformVersion *version.Version
   112  }