terraform: destroy should remove from state

This commit is contained in:
Mitchell Hashimoto 2014-07-02 19:56:29 -07:00
parent 5608175c13
commit 770d4e1e71
2 changed files with 55 additions and 11 deletions

View File

@ -252,12 +252,6 @@ func (t *Terraform) applyWalkFn(
// Force the resource state type to be our type // Force the resource state type to be our type
rs.Type = r.State.Type rs.Type = r.State.Type
// If no state was returned, then no variables were updated so
// just return.
if rs == nil {
return nil, nil
}
var errs []error var errs []error
for ak, av := range rs.Attributes { for ak, av := range rs.Attributes {
// If the value is the unknown variable value, then it is an error. // If the value is the unknown variable value, then it is an error.
@ -271,7 +265,11 @@ func (t *Terraform) applyWalkFn(
// Update the resulting diff // Update the resulting diff
l.Lock() l.Lock()
if rs.ID == "" {
delete(result.Resources, r.Id)
} else {
result.Resources[r.Id] = rs result.Resources[r.Id] = rs
}
l.Unlock() l.Unlock()
// Update the state for the resource itself // Update the state for the resource itself

View File

@ -196,6 +196,55 @@ func TestTerraformApply_destroy(t *testing.T) {
} }
} }
func TestTerraformApply_destroyOrphan(t *testing.T) {
rpAWS := new(MockResourceProvider)
rpAWS.ResourcesReturn = []ResourceType{
ResourceType{Name: "aws_instance"},
}
rpAWS.DiffFn = func(*ResourceState, *ResourceConfig) (*ResourceDiff, error) {
return &ResourceDiff{
Attributes: map[string]*ResourceAttrDiff{
"num": &ResourceAttrDiff{
New: "bar",
},
},
}, nil
}
rpAWS.ApplyFn = func(*ResourceState, *ResourceDiff) (*ResourceState, error) {
return nil, nil
}
c := testConfig(t, "apply-error")
tf := testTerraform2(t, &Config{
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(rpAWS),
},
})
s := &State{
Resources: map[string]*ResourceState{
"aws_instance.baz": &ResourceState{
ID: "bar",
Type: "aws_instance",
},
},
}
p, err := tf.Plan(&PlanOpts{Config: c, State: s})
if err != nil {
t.Fatalf("err: %s", err)
}
state, err := tf.Apply(p)
if err != nil {
t.Fatalf("err: %s", err)
}
if len(state.Resources) != 0 {
t.Fatalf("bad: %#v", state.Resources)
}
}
func TestTerraformApply_error(t *testing.T) { func TestTerraformApply_error(t *testing.T) {
errored := false errored := false
@ -908,10 +957,7 @@ aws_instance.foo:
` `
const testTerraformApplyDestroyStr = ` const testTerraformApplyDestroyStr = `
aws_instance.bar: <no state>
ID = <not created>
aws_instance.foo:
ID = <not created>
` `
const testTerraformApplyErrorStr = ` const testTerraformApplyErrorStr = `