From dd24ed4b76126a763b0504fd5605ce3b52685245 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 13 May 2015 20:15:13 -0700 Subject: [PATCH] helper/schema: blank ID refresh doesn't exist [GH-1905] --- helper/schema/resource.go | 5 +++++ helper/schema/resource_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) 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{