From 19609bde0e5a25edd5587412780b764d744951a7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 26 Apr 2016 09:36:05 -0700 Subject: [PATCH] helper/schema: can specify Importer on Resource, InternalValidate --- helper/schema/resource.go | 14 ++++++++++++++ helper/schema/resource_importer.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/helper/schema/resource.go b/helper/schema/resource.go index 631c9c2f6..76d08301d 100644 --- a/helper/schema/resource.go +++ b/helper/schema/resource.go @@ -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) diff --git a/helper/schema/resource_importer.go b/helper/schema/resource_importer.go index 24a442eec..b086adf9b 100644 --- a/helper/schema/resource_importer.go +++ b/helper/schema/resource_importer.go @@ -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 +}