diff --git a/helper/schema/resource.go b/helper/schema/resource.go index dec94fc7b..cc759e0d6 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -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. diff --git a/helper/schema/resource_test.go b/helper/schema/resource_test.go index 8027f5fb1..b27bd193a 100644 --- a/helper/schema/resource_test.go +++ b/helper/schema/resource_test.go @@ -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{