github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/states/statefile/version3.go (about)

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