Renumber original binary state as V0

This commit rectifies the fact that the original binary state is
referred to as V1 in the source code, but the first version of the JSON
state uses StateVersion: 1. We instead make the code refer to V0 as the
binary state, and V1 as the first version of JSON state.
This commit is contained in:
James Nugent 2016-03-24 16:11:32 -07:00
parent 14cf31cf43
commit 3393492033
4 changed files with 41 additions and 41 deletions

View File

@ -1324,18 +1324,18 @@ func (e *EphemeralState) deepcopy() *EphemeralState {
func ReadState(src io.Reader) (*State, error) {
buf := bufio.NewReader(src)
// Check if this is a V1 format
// Check if this is a V0 format
start, err := buf.Peek(len(stateFormatMagic))
if err != nil {
return nil, fmt.Errorf("Failed to check for magic bytes: %v", err)
}
if string(start) == stateFormatMagic {
// Read the old state
old, err := ReadStateV1(buf)
old, err := ReadStateV0(buf)
if err != nil {
return nil, err
}
return upgradeV1State(old)
return upgradeV0State(old)
}
// Otherwise, must be V2
@ -1409,9 +1409,9 @@ func WriteState(d *State, dst io.Writer) error {
return nil
}
// upgradeV1State is used to upgrade a V1 state representation
// upgradeV0State is used to upgrade a V0 state representation
// into a proper State representation.
func upgradeV1State(old *StateV1) (*State, error) {
func upgradeV0State(old *StateV0) (*State, error) {
s := &State{}
s.init()

View File

@ -1163,15 +1163,15 @@ func TestInstanceState_MergeDiff_nilDiff(t *testing.T) {
}
func TestReadUpgradeState(t *testing.T) {
state := &StateV1{
Resources: map[string]*ResourceStateV1{
"foo": &ResourceStateV1{
state := &StateV0{
Resources: map[string]*ResourceStateV0{
"foo": &ResourceStateV0{
ID: "bar",
},
},
}
buf := new(bytes.Buffer)
if err := testWriteStateV1(state, buf); err != nil {
if err := testWriteStateV0(state, buf); err != nil {
t.Fatalf("err: %s", err)
}
@ -1182,7 +1182,7 @@ func TestReadUpgradeState(t *testing.T) {
t.Fatalf("err: %s", err)
}
upgraded, err := upgradeV1State(state)
upgraded, err := upgradeV0State(state)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -1359,20 +1359,20 @@ func TestWriteStateTFVersion(t *testing.T) {
}
}
func TestUpgradeV1State(t *testing.T) {
old := &StateV1{
func TestUpgradeV0State(t *testing.T) {
old := &StateV0{
Outputs: map[string]string{
"ip": "127.0.0.1",
},
Resources: map[string]*ResourceStateV1{
"foo": &ResourceStateV1{
Resources: map[string]*ResourceStateV0{
"foo": &ResourceStateV0{
Type: "test_resource",
ID: "bar",
Attributes: map[string]string{
"key": "val",
},
},
"bar": &ResourceStateV1{
"bar": &ResourceStateV0{
Type: "test_resource",
ID: "1234",
Attributes: map[string]string{
@ -1384,7 +1384,7 @@ func TestUpgradeV1State(t *testing.T) {
"bar": struct{}{},
},
}
state, err := upgradeV1State(old)
state, err := upgradeV0State(old)
if err != nil {
t.Fatalf("err: %v", err)
}

View File

@ -21,21 +21,21 @@ const (
stateFormatVersion byte = 1
)
// StateV1 is used to represent the state of Terraform files before
// StateV0 is used to represent the state of Terraform files before
// 0.3. It is automatically upgraded to a modern State representation
// on start.
type StateV1 struct {
type StateV0 struct {
Outputs map[string]string
Resources map[string]*ResourceStateV1
Resources map[string]*ResourceStateV0
Tainted map[string]struct{}
once sync.Once
}
func (s *StateV1) init() {
func (s *StateV0) init() {
s.once.Do(func() {
if s.Resources == nil {
s.Resources = make(map[string]*ResourceStateV1)
s.Resources = make(map[string]*ResourceStateV0)
}
if s.Tainted == nil {
@ -44,8 +44,8 @@ func (s *StateV1) init() {
})
}
func (s *StateV1) deepcopy() *StateV1 {
result := new(StateV1)
func (s *StateV0) deepcopy() *StateV0 {
result := new(StateV0)
result.init()
if s != nil {
for k, v := range s.Resources {
@ -61,7 +61,7 @@ func (s *StateV1) deepcopy() *StateV1 {
// prune is a helper that removes any empty IDs from the state
// and cleans it up in general.
func (s *StateV1) prune() {
func (s *StateV0) prune() {
for k, v := range s.Resources {
if v.ID == "" {
delete(s.Resources, k)
@ -72,7 +72,7 @@ func (s *StateV1) prune() {
// Orphans returns a list of keys of resources that are in the State
// but aren't present in the configuration itself. Hence, these keys
// represent the state of resources that are orphans.
func (s *StateV1) Orphans(c *config.Config) []string {
func (s *StateV0) Orphans(c *config.Config) []string {
keys := make(map[string]struct{})
for k, _ := range s.Resources {
keys[k] = struct{}{}
@ -96,7 +96,7 @@ func (s *StateV1) Orphans(c *config.Config) []string {
return result
}
func (s *StateV1) String() string {
func (s *StateV0) String() string {
if len(s.Resources) == 0 {
return "<no state>"
}
@ -175,7 +175,7 @@ func (s *StateV1) String() string {
//
// Extra is just extra data that a provider can return that we store
// for later, but is not exposed in any way to the user.
type ResourceStateV1 struct {
type ResourceStateV0 struct {
// This is filled in and managed by Terraform, and is the resource
// type itself such as "mycloud_instance". If a resource provider sets
// this value, it won't be persisted.
@ -228,8 +228,8 @@ type ResourceStateV1 struct {
// If the diff attribute requires computing the value, and hence
// won't be available until apply, the value is replaced with the
// computeID.
func (s *ResourceStateV1) MergeDiff(d *InstanceDiff) *ResourceStateV1 {
var result ResourceStateV1
func (s *ResourceStateV0) MergeDiff(d *InstanceDiff) *ResourceStateV0 {
var result ResourceStateV0
if s != nil {
result = *s
}
@ -258,7 +258,7 @@ func (s *ResourceStateV1) MergeDiff(d *InstanceDiff) *ResourceStateV1 {
return &result
}
func (s *ResourceStateV1) GoString() string {
func (s *ResourceStateV0) GoString() string {
return fmt.Sprintf("*%#v", *s)
}
@ -270,10 +270,10 @@ type ResourceDependency struct {
ID string
}
// ReadStateV1 reads a state structure out of a reader in the format that
// ReadStateV0 reads a state structure out of a reader in the format that
// was written by WriteState.
func ReadStateV1(src io.Reader) (*StateV1, error) {
var result *StateV1
func ReadStateV0(src io.Reader) (*StateV0, error) {
var result *StateV0
var err error
n := 0

View File

@ -12,10 +12,10 @@ import (
"github.com/mitchellh/hashstructure"
)
func TestReadWriteStateV1(t *testing.T) {
state := &StateV1{
Resources: map[string]*ResourceStateV1{
"foo": &ResourceStateV1{
func TestReadWriteStateV0(t *testing.T) {
state := &StateV0{
Resources: map[string]*ResourceStateV0{
"foo": &ResourceStateV0{
ID: "bar",
ConnInfo: map[string]string{
"type": "ssh",
@ -33,7 +33,7 @@ func TestReadWriteStateV1(t *testing.T) {
}
buf := new(bytes.Buffer)
if err := testWriteStateV1(state, buf); err != nil {
if err := testWriteStateV0(state, buf); err != nil {
t.Fatalf("err: %s", err)
}
@ -47,7 +47,7 @@ func TestReadWriteStateV1(t *testing.T) {
t.Fatalf("structure changed during serialization!")
}
actual, err := ReadStateV1(buf)
actual, err := ReadStateV0(buf)
if err != nil {
t.Fatalf("err: %s", err)
}
@ -75,9 +75,9 @@ func (s *sensitiveState) init() {
})
}
// testWriteStateV1 writes a state somewhere in a binary format.
// testWriteStateV0 writes a state somewhere in a binary format.
// Only for testing now
func testWriteStateV1(d *StateV1, dst io.Writer) error {
func testWriteStateV0(d *StateV0, dst io.Writer) error {
// Write the magic bytes so we can determine the file format later
n, err := dst.Write([]byte(stateFormatMagic))
if err != nil {