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 // Move all tainted
// TODO: Move all deposed if len(src.Tainted) > 0 {
resource.Tainted = src.Tainted
}
// Move all deposed
if len(src.Deposed) > 0 {
resource.Deposed = src.Deposed
}
return nil return nil
} }
@ -130,13 +137,8 @@ func stateAddFunc_Instance_Instance(s *State, addr *ResourceAddress, raw interfa
instanceRaw, _ := stateAddInitAddr(s, addr) instanceRaw, _ := stateAddInitAddr(s, addr)
instance := instanceRaw.(*InstanceState) instance := instanceRaw.(*InstanceState)
// Depending on the instance type, set it // Set it
switch addr.InstanceType {
case TypePrimary:
*instance = *src *instance = *src
default:
return fmt.Errorf("can't move instance state to %s", addr.InstanceType)
}
return nil return nil
} }
@ -240,10 +242,14 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) {
// Get the instance // Get the instance
exists = true exists = true
var instance *InstanceState instance := &InstanceState{}
switch addr.InstanceType { switch addr.InstanceType {
case TypePrimary: case TypePrimary:
if v := resource.Primary; v != nil {
instance = resource.Primary instance = resource.Primary
} else {
exists = false
}
case TypeTainted: case TypeTainted:
idx := addr.Index idx := addr.Index
if addr.Index < 0 { if addr.Index < 0 {
@ -251,6 +257,9 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) {
} }
if len(resource.Tainted) > idx { if len(resource.Tainted) > idx {
instance = resource.Tainted[idx] instance = resource.Tainted[idx]
} else {
resource.Tainted = append(resource.Tainted, instance)
exists = false
} }
case TypeDeposed: case TypeDeposed:
idx := addr.Index idx := addr.Index
@ -259,12 +268,11 @@ func stateAddInitAddr(s *State, addr *ResourceAddress) (interface{}, bool) {
} }
if len(resource.Deposed) > idx { if len(resource.Deposed) > idx {
instance = resource.Deposed[idx] instance = resource.Deposed[idx]
} } else {
} resource.Deposed = append(resource.Deposed, instance)
if instance == nil {
instance = &InstanceState{}
exists = false exists = false
} }
}
return instance, exists 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)": { "ResourceState => Resource Addr (existing)": {
true, true,
"aws_instance.foo", "aws_instance.foo",
@ -246,8 +279,8 @@ func TestStateAdd(t *testing.T) {
// Verify equality // Verify equality
if !tc.One.Equal(tc.Two) { 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%#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%s\n\n%s", k, tc.One.String(), tc.Two.String())
} }
} }
} }