From 2444986fe831121ddd2e32432e549bda9c27b15f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 19 Aug 2014 09:26:48 -0700 Subject: [PATCH] helper/schema: nice error if update isn't supported --- helper/schema/resource.go | 5 ++++ helper/schema/resource_test.go | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/helper/schema/resource.go b/helper/schema/resource.go index 051bf248f..3c8a12e2b 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -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) } diff --git a/helper/schema/resource_test.go b/helper/schema/resource_test.go index 6cd71cb34..eaad10cb9 100644 --- a/helper/schema/resource_test.go +++ b/helper/schema/resource_test.go @@ -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