From 0c0ae3ca7c73bda71db828d0efdeab02b18083a4 Mon Sep 17 00:00:00 2001 From: Chris Marchesi Date: Sat, 8 Jul 2017 22:16:51 -0700 Subject: [PATCH] helper/schema: CustomizeDiff allowed on writable resources only This keeps CustomizeDiff from being defined on data sources, where it would be useless. We just catch this in InternalValidate like the rest of the CRUD functions that are not used in data sources. --- helper/schema/resource.go | 7 +++++++ helper/schema/resource_test.go | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/helper/schema/resource.go b/helper/schema/resource.go index fbc75bc1c2..8290ba5035 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -112,6 +112,8 @@ type Resource struct { // // For the most part, only computed fields can be customized by this // function. + // + // This function is only allowed on regular resources (not data sources). CustomizeDiff CustomizeDiffFunc // Importer is the ResourceImporter implementation for this resource. @@ -378,6 +380,11 @@ func (r *Resource) InternalValidate(topSchemaMap schemaMap, writable bool) error if r.Create != nil || r.Update != nil || r.Delete != nil { return fmt.Errorf("must not implement Create, Update or Delete") } + + // CustomizeDiff cannot be defined for read-only resources + if r.CustomizeDiff != nil { + return fmt.Errorf("cannot implement CustomizeDiff") + } } tsm := topSchemaMap diff --git a/helper/schema/resource_test.go b/helper/schema/resource_test.go index 22bfd1b8cf..60b49619bf 100644 --- a/helper/schema/resource_test.go +++ b/helper/schema/resource_test.go @@ -835,6 +835,21 @@ func TestResourceInternalValidate(t *testing.T) { true, false, }, + + 13: { // non-writable must not define CustomizeDiff + &Resource{ + Read: func(d *ResourceData, meta interface{}) error { return nil }, + Schema: map[string]*Schema{ + "goo": &Schema{ + Type: TypeInt, + Optional: true, + }, + }, + CustomizeDiff: func(*ResourceDiff, interface{}) error { return nil }, + }, + false, + true, + }, } for i, tc := range cases {