helper/schema: nice error if update isn't supported

This commit is contained in:
Mitchell Hashimoto 2014-08-19 09:26:48 -07:00
parent e2b8951a58
commit 2444986fe8
2 changed files with 49 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package schema
import (
"errors"
"fmt"
"github.com/hashicorp/terraform/terraform"
)
@ -73,6 +74,10 @@ func (r *Resource) Apply(
// We're creating, it is a new resource.
err = r.Create(data, meta)
} else {
if r.Update == nil {
return s, fmt.Errorf("%s doesn't support update", s.Type)
}
err = r.Update(data, meta)
}

View File

@ -186,6 +186,50 @@ func TestResourceApply_update(t *testing.T) {
}
}
func TestResourceApply_updateNoCallback(t *testing.T) {
r := &Resource{
Schema: map[string]*Schema{
"foo": &Schema{
Type: TypeInt,
Optional: true,
},
},
}
r.Update = nil
s := &terraform.ResourceState{
ID: "foo",
Attributes: map[string]string{
"foo": "12",
},
}
d := &terraform.ResourceDiff{
Attributes: map[string]*terraform.ResourceAttrDiff{
"foo": &terraform.ResourceAttrDiff{
New: "13",
},
},
}
actual, err := r.Apply(s, d, nil)
if err == nil {
t.Fatal("should error")
}
expected := &terraform.ResourceState{
ID: "foo",
Attributes: map[string]string{
"foo": "12",
},
}
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestResourceInternalValidate(t *testing.T) {
cases := []struct {
In *Resource