helper/schema: blank ID refresh doesn't exist [GH-1905]

This commit is contained in:
Mitchell Hashimoto 2015-05-13 20:15:13 -07:00
parent 442376c8e4
commit dd24ed4b76
2 changed files with 34 additions and 0 deletions

View File

@ -171,6 +171,11 @@ func (r *Resource) Validate(c *terraform.ResourceConfig) ([]string, []error) {
func (r *Resource) Refresh(
s *terraform.InstanceState,
meta interface{}) (*terraform.InstanceState, error) {
// If the ID is already somehow blank, it doesn't exist
if s.ID == "" {
return nil, nil
}
if r.Exists != nil {
// Make a copy of data so that if it is modified it doesn't
// affect our Read later.

View File

@ -388,6 +388,35 @@ func TestResourceRefresh(t *testing.T) {
}
}
func TestResourceRefresh_blankId(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("foo")
return nil
}
s := &terraform.InstanceState{
ID: "",
Attributes: map[string]string{},
}
actual, err := r.Refresh(s, 42)
if err != nil {
t.Fatalf("err: %s", err)
}
if actual != nil {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceRefresh_delete(t *testing.T) {
r := &Resource{
Schema: map[string]*Schema{