Skip to content

Commit

Permalink
helper/schema: blank ID refresh doesn't exist [GH-1905]
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed May 14, 2015
1 parent 442376c commit dd24ed4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions helper/schema/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions helper/schema/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit dd24ed4

Please sign in to comment.