helper/schema: if update sets the ID to blank, it deletes the resource
This commit is contained in:
parent
1de0c5e2c9
commit
021a23fe99
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue