terraform: refresh returning nil should delete from state

This commit is contained in:
Mitchell Hashimoto 2014-07-24 14:40:20 -07:00
parent bcf92e8d39
commit 615cf04715
2 changed files with 36 additions and 1 deletions

View File

@ -820,7 +820,11 @@ func (c *Context) refreshWalkFn() depgraph.WalkFunc {
rs.Type = r.State.Type
c.sl.Lock()
c.state.Resources[r.Id] = rs
if rs.ID == "" {
delete(c.state.Resources, r.Id)
} else {
c.state.Resources[r.Id] = rs
}
c.sl.Unlock()
for _, h := range c.hooks {

View File

@ -1639,6 +1639,37 @@ func TestContextRefresh(t *testing.T) {
}
}
func TestContextRefresh_delete(t *testing.T) {
p := testProvider("aws")
c := testConfig(t, "refresh-basic")
ctx := testContext(t, &ContextOpts{
Config: c,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
State: &State{
Resources: map[string]*ResourceState{
"aws_instance.web": &ResourceState{
ID: "foo",
Type: "aws_instance",
},
},
},
})
p.RefreshFn = nil
p.RefreshReturn = nil
s, err := ctx.Refresh()
if err != nil {
t.Fatalf("err: %s", err)
}
if len(s.Resources) > 0 {
t.Fatal("resources should be empty")
}
}
func TestContextRefresh_ignoreUncreated(t *testing.T) {
p := testProvider("aws")
c := testConfig(t, "refresh-basic")