Merge pull request #9730 from hashicorp/b-schema-validate

helper/schema: validate Read, Delete are set
This commit is contained in:
Mitchell Hashimoto 2016-10-31 13:24:02 -07:00 committed by GitHub
commit 75f18492f0
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 {
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)
}
})
}
}