terraform: moving resource to resource

This commit is contained in:
Mitchell Hashimoto 2016-04-11 18:41:48 -07:00
parent e65a726936
commit d3fcfcc027
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 58 additions and 17 deletions

View File

@ -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
}

View File

@ -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())
}
}
}