helper/schema: if update sets the ID to blank, it deletes the resource

This commit is contained in:
Mitchell Hashimoto 2014-08-19 18:19:02 -07:00
parent 1de0c5e2c9
commit 021a23fe99
2 changed files with 38 additions and 1 deletions

View File

@ -117,7 +117,12 @@ func (r *Resource) Refresh(
}
err = r.Read(data, meta)
return data.State(), err
state := data.State()
if state.ID == "" {
state = nil
}
return state, err
}
// InternalValidate should be called to validate the structure

View File

@ -304,3 +304,35 @@ func TestResourceRefresh(t *testing.T) {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceRefresh_delete(t *testing.T) {
r := &Resource{
Schema: map[string]*Schema{
"foo": &Schema{
Type: TypeInt,
Optional: true,
},
},
}
r.Read = func(d *ResourceData, m interface{}) error {
d.SetId("")
return nil
}
s := &terraform.ResourceState{
ID: "bar",
Attributes: map[string]string{
"foo": "12",
},
}
actual, err := r.Refresh(s, 42)
if err != nil {
t.Fatalf("err: %s", err)
}
if actual != nil {
t.Fatalf("bad: %#v", actual)
}
}