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