Merge pull request #1959 from hashicorp/b-refresh-blank-id

helper/schema: blank ID refresh doesn't exist [GH-1905]
This commit is contained in:
Mitchell Hashimoto 2015-05-14 10:00:23 -07:00
commit b6906f9158
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{