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.
This commit is contained in:
Chris Marchesi 2017-07-08 22:16:51 -07:00 committed by Martin Atkins
parent 2c541e8c97
commit 0c0ae3ca7c
2 changed files with 22 additions and 0 deletions

View File

@ -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

View File

@ -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 {