github.com/opentofu/opentofu@v1.7.1/internal/states/statefile/file.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package statefile
     7  
     8  import (
     9  	version "github.com/hashicorp/go-version"
    10  
    11  	"github.com/opentofu/opentofu/internal/states"
    12  	tfversion "github.com/opentofu/opentofu/version"
    13  )
    14  
    15  // File is the in-memory representation of a state file. It includes the state
    16  // itself along with various metadata used to track changing state files for
    17  // the same configuration over time.
    18  type File struct {
    19  	// TerraformVersion is the version of OpenTofu that wrote this state file.
    20  	TerraformVersion *version.Version
    21  
    22  	// Serial is incremented on any operation that modifies
    23  	// the State file. It is used to detect potentially conflicting
    24  	// updates.
    25  	Serial uint64
    26  
    27  	// Lineage is set when a new, blank state file is created and then
    28  	// never updated. This allows us to determine whether the serials
    29  	// of two states can be meaningfully compared.
    30  	// Apart from the guarantee that collisions between two lineages
    31  	// are very unlikely, this value is opaque and external callers
    32  	// should only compare lineage strings byte-for-byte for equality.
    33  	Lineage string
    34  
    35  	// State is the actual state represented by this file.
    36  	State *states.State
    37  }
    38  
    39  func New(state *states.State, lineage string, serial uint64) *File {
    40  	// To make life easier on callers, we'll accept a nil state here and just
    41  	// allocate an empty one, which is required for this file to be successfully
    42  	// written out.
    43  	if state == nil {
    44  		state = states.NewState()
    45  	}
    46  
    47  	return &File{
    48  		TerraformVersion: tfversion.SemVer,
    49  		State:            state,
    50  		Lineage:          lineage,
    51  		Serial:           serial,
    52  	}
    53  }
    54  
    55  // DeepCopy is a convenience method to create a new File object whose state
    56  // is a deep copy of the receiver's, as implemented by states.State.DeepCopy.
    57  func (f *File) DeepCopy() *File {
    58  	if f == nil {
    59  		return nil
    60  	}
    61  	return &File{
    62  		TerraformVersion: f.TerraformVersion,
    63  		Serial:           f.Serial,
    64  		Lineage:          f.Lineage,
    65  		State:            f.State.DeepCopy(),
    66  	}
    67  }