From d3fcfcc027bd050469e95cf7f19e7d4c2dbba0ad Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 11 Apr 2016 18:41:48 -0700 Subject: [PATCH] terraform: moving resource to resource --- terraform/state_add.go | 38 ++++++++++++++++++++++--------------- terraform/state_add_test.go | 37 ++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/terraform/state_add.go b/terraform/state_add.go index 4c6e0992f..9adf0df0c 100644 --- a/terraform/state_add.go +++ b/terraform/state_add.go @@ -117,8 +117,15 @@ func stateAddFunc_Resource_Resource(s *State, addr *ResourceAddress, raw interfa } } - // TODO: Move all tainted - // TODO: Move all deposed + // Move all tainted + if len(src.Tainted) > 0 { + resource.Tainted = src.Tainted + } + + // Move all deposed + if len(src.Deposed) > 0 { + resource.Deposed = src.Deposed + } return nil } @@ -130,13 +137,8 @@ func stateAddFunc_Instance_Instance(s *State, addr *ResourceAddress, raw interfa instanceRaw, _ := stateAddInitAddr(s, addr) instance := instanceRaw.(*InstanceState) - // Depending on the instance type, set it - switch addr.InstanceType { - case TypePrimary: - *instance = *src - default: - return fmt.Errorf("can't move instance state to %s", addr.InstanceType) - } + // Set it + *instance = *src return nil } @@ -240,10 +242,14 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) { // Get the instance exists = true - var instance *InstanceState + instance := &InstanceState{} switch addr.InstanceType { case TypePrimary: - instance = resource.Primary + if v := resource.Primary; v != nil { + instance = resource.Primary + } else { + exists = false + } case TypeTainted: idx := addr.Index if addr.Index < 0 { @@ -251,6 +257,9 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) { } if len(resource.Tainted) > idx { instance = resource.Tainted[idx] + } else { + resource.Tainted = append(resource.Tainted, instance) + exists = false } case TypeDeposed: idx := addr.Index @@ -259,12 +268,11 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) { } if len(resource.Deposed) > idx { instance = resource.Deposed[idx] + } else { + resource.Deposed = append(resource.Deposed, instance) + exists = false } } - if instance == nil { - instance = &InstanceState{} - exists = false - } return instance, exists } diff --git a/terraform/state_add_test.go b/terraform/state_add_test.go index f192c1ace..8b9a9678b 100644 --- a/terraform/state_add_test.go +++ b/terraform/state_add_test.go @@ -195,6 +195,39 @@ func TestStateAdd(t *testing.T) { }, }, + "ResourceState w/ tainted => Resource Addr (new)": { + false, + "aws_instance.foo", + &ResourceState{ + Type: "test_instance", + Tainted: []*InstanceState{ + &InstanceState{ + ID: "foo", + }, + }, + }, + + &State{}, + &State{ + Modules: []*ModuleState{ + &ModuleState{ + Path: []string{"root"}, + Resources: map[string]*ResourceState{ + "aws_instance.foo": &ResourceState{ + Type: "test_instance", + Primary: &InstanceState{}, + Tainted: []*InstanceState{ + &InstanceState{ + ID: "foo", + }, + }, + }, + }, + }, + }, + }, + }, + "ResourceState => Resource Addr (existing)": { true, "aws_instance.foo", @@ -246,8 +279,8 @@ func TestStateAdd(t *testing.T) { // Verify equality if !tc.One.Equal(tc.Two) { - // t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two) - t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String()) + t.Fatalf("Bad: %s\n\n%#v\n\n%#v", k, tc.One, tc.Two) + //t.Fatalf("Bad: %s\n\n%s\n\n%s", k, tc.One.String(), tc.Two.String()) } } }