terraform/states/statefile/diagnostics.go

63 lines
1.9 KiB
Go
Raw Normal View History

statefile: New package for loading and saving state files Whereas the parent directory "states" contains the models that represent state in memory, this package's responsibility is in serializing a subset of that data to a JSON-based file format and then reloading that data back into memory later. For reading, this package supports state file formats going back to version 1, using lightly-adapted versions of the migration code previously used in the "terraform" package. State data is upgraded to the latest version step by step and then transformed into the in-memory state representation, which is distinct from any of the file format structs in this package to enable these to evolve separately. For writing, only the latest version (4) is supported, which is a new format that is a slightly-flattened version of the new in-memory state models introduced in the prior commit. This format retains the outputs from only the root module and it flattens out the module and instance parts of the hierarchy by including the identifiers for these inside the child object. The loader then reconstructs the multi-layer structure we use for more convenient access in memory. For now, the only testing in this package is of round-tripping different versions of state through a read and a write, ensuring the output is as desired. This exercises all of the reading, upgrading, and writing functions but should be augmented in later commits to improve coverage and introduce more focused tests for specific parts of the functionality.
2018-06-08 02:35:55 +02:00
package statefile
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/internal/tfdiags"
statefile: New package for loading and saving state files Whereas the parent directory "states" contains the models that represent state in memory, this package's responsibility is in serializing a subset of that data to a JSON-based file format and then reloading that data back into memory later. For reading, this package supports state file formats going back to version 1, using lightly-adapted versions of the migration code previously used in the "terraform" package. State data is upgraded to the latest version step by step and then transformed into the in-memory state representation, which is distinct from any of the file format structs in this package to enable these to evolve separately. For writing, only the latest version (4) is supported, which is a new format that is a slightly-flattened version of the new in-memory state models introduced in the prior commit. This format retains the outputs from only the root module and it flattens out the module and instance parts of the hierarchy by including the identifiers for these inside the child object. The loader then reconstructs the multi-layer structure we use for more convenient access in memory. For now, the only testing in this package is of round-tripping different versions of state through a read and a write, ensuring the output is as desired. This exercises all of the reading, upgrading, and writing functions but should be augmented in later commits to improve coverage and introduce more focused tests for specific parts of the functionality.
2018-06-08 02:35:55 +02:00
)
const invalidFormat = "Invalid state file format"
// jsonUnmarshalDiags is a helper that translates errors returned from
// json.Unmarshal into hopefully-more-helpful diagnostics messages.
func jsonUnmarshalDiags(err error) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
if err == nil {
return diags
}
switch tErr := err.(type) {
case *json.SyntaxError:
// We've usually already successfully parsed a source file as JSON at
// least once before we'd use jsonUnmarshalDiags with it (to sniff
// the version number) so this particular error should not appear much
// in practice.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
fmt.Sprintf("The state file could not be parsed as JSON: syntax error at byte offset %d.", tErr.Offset),
))
case *json.UnmarshalTypeError:
// This is likely to be the most common area, describing a
// non-conformance between the file and the expected file format
// at a semantic level.
if tErr.Field != "" {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
fmt.Sprintf("The state file field %q has invalid value %s", tErr.Field, tErr.Value),
))
break
} else {
// Without a field name, we can't really say anything helpful.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
"The state file does not conform to the expected JSON data structure.",
))
}
default:
// Fallback for all other types of errors. This can happen only for
// custom UnmarshalJSON implementations, so should be encountered
// only rarely.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
fmt.Sprintf("The state file does not conform to the expected JSON data structure: %s.", err.Error()),
))
}
return diags
}