helper/schema: validate Read, Delete are set

This commit is contained in:
Mitchell Hashimoto 2016-10-30 15:04:32 -07:00
parent 6e55f5683c
commit f7a234bc71
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 48 additions and 4 deletions

View File

@ -317,6 +317,14 @@ func (r *Resource) InternalValidate(topSchemaMap schemaMap, writable bool) error
tsm = schemaMap(r.Schema)
// Destroy, and Read are required
if r.Read == nil {
return fmt.Errorf("Read must be implemented")
}
if r.Delete == nil {
return fmt.Errorf("Delete must be implemented")
}
// If we have an importer, we need to verify the importer.
if r.Importer != nil {
if err := r.Importer.InternalValidate(); err != nil {

View File

@ -480,13 +480,49 @@ func TestResourceInternalValidate(t *testing.T) {
false,
true,
},
// writable must have Read
{
&Resource{
Create: func(d *ResourceData, meta interface{}) error { return nil },
Update: func(d *ResourceData, meta interface{}) error { return nil },
Delete: func(d *ResourceData, meta interface{}) error { return nil },
Schema: map[string]*Schema{
"goo": &Schema{
Type: TypeInt,
Optional: true,
},
},
},
true,
true,
},
// writable must have Delete
{
&Resource{
Create: func(d *ResourceData, meta interface{}) error { return nil },
Read: func(d *ResourceData, meta interface{}) error { return nil },
Update: func(d *ResourceData, meta interface{}) error { return nil },
Schema: map[string]*Schema{
"goo": &Schema{
Type: TypeInt,
Optional: true,
},
},
},
true,
true,
},
}
for i, tc := range cases {
err := tc.In.InternalValidate(schemaMap{}, tc.Writable)
if err != nil != tc.Err {
t.Fatalf("%d: bad: %s", i, err)
}
t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) {
err := tc.In.InternalValidate(schemaMap{}, tc.Writable)
if err != nil != tc.Err {
t.Fatalf("%d: bad: %s", i, err)
}
})
}
}