helper/schema: can specify Importer on Resource, InternalValidate

This commit is contained in:
Mitchell Hashimoto 2016-04-26 09:36:05 -07:00
parent b8121ea63e
commit 19609bde0e
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 28 additions and 0 deletions

View File

@ -78,6 +78,13 @@ type Resource struct {
Update UpdateFunc
Delete DeleteFunc
Exists ExistsFunc
// Importer is the ResourceImporter implementation for this resource.
// If this is nil, then this resource does not support importing. If
// this is non-nil, then it supports importing and ResourceImporter
// must be validated. The validity of ResourceImporter is verified
// by InternalValidate on Resource.
Importer *ResourceImporter
}
// See Resource documentation.
@ -260,6 +267,13 @@ func (r *Resource) InternalValidate(topSchemaMap schemaMap) error {
}
tsm = schemaMap(r.Schema)
// If we have an importer, we need to verify the importer.
if r.Importer != nil {
if err := r.Importer.InternalValidate(); err != nil {
return err
}
}
}
return schemaMap(r.Schema).InternalValidate(tsm)

View File

@ -27,4 +27,18 @@ type ResourceImporter struct {
// that was given to the function. In other cases (such as AWS security groups),
// an import may fan out to multiple resources and this will have to return
// multiple.
//
// To create the ResourceData structures for other resource types (if
// you have to), instantiate your resource and call the Data function.
type StateFunc func(*ResourceData, interface{}) ([]*ResourceData, error)
// InternalValidate should be called to validate the structure of this
// importer. This should be called in a unit test.
//
// Resource.InternalValidate() will automatically call this, so this doesn't
// need to be called manually. Further, Resource.InternalValidate() is
// automatically called by Provider.InternalValidate(), so you only need
// to internal validate the provider.
func (r *ResourceImporter) InternalValidate() error {
return nil
}