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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package statefile
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  
    10  	"github.com/terramate-io/tf/tfdiags"
    11  )
    12  
    13  func readStateV3(src []byte) (*File, tfdiags.Diagnostics) {
    14  	var diags tfdiags.Diagnostics
    15  	sV3 := &stateV3{}
    16  	err := json.Unmarshal(src, sV3)
    17  	if err != nil {
    18  		diags = diags.Append(jsonUnmarshalDiags(err))
    19  		return nil, diags
    20  	}
    21  
    22  	file, prepDiags := prepareStateV3(sV3)
    23  	diags = diags.Append(prepDiags)
    24  	return file, diags
    25  }
    26  
    27  func prepareStateV3(sV3 *stateV3) (*File, tfdiags.Diagnostics) {
    28  	var diags tfdiags.Diagnostics
    29  	sV4, err := upgradeStateV3ToV4(sV3)
    30  	if err != nil {
    31  		diags = diags.Append(tfdiags.Sourceless(
    32  			tfdiags.Error,
    33  			upgradeFailed,
    34  			fmt.Sprintf("Error upgrading state file format from version 3 to version 4: %s.", err),
    35  		))
    36  		return nil, diags
    37  	}
    38  
    39  	file, prepDiags := prepareStateV4(sV4)
    40  	diags = diags.Append(prepDiags)
    41  	return file, diags
    42  }
    43  
    44  // stateV2 is a representation of the legacy JSON state format version 3.
    45  //
    46  // It is only used to read version 3 JSON files prior to upgrading them to
    47  // the current format.
    48  //
    49  // The differences between version 2 and version 3 are only in the data and
    50  // not in the structure, so stateV3 actually shares the same structs as
    51  // stateV2. Type stateV3 represents that the data within is formatted as
    52  // expected by the V3 format, rather than the V2 format.
    53  type stateV3 stateV2