helper/schema: emit warning when using data source resource shim
For backward compatibility we will continue to support using the data sources that were formerly logical resources as resources for the moment, but we want to warn the user about it since this support is likely to be removed in future. This is done by adding a new "deprecation message" feature to schema.Resource, but for the moment this is done as an internal feature (not usable directly by plugins) so that we can collect additional use-cases and design a more general interface before creating a compatibility constraint.
This commit is contained in:
parent
3eb4a89104
commit
6cd22a4c9a
|
@ -10,6 +10,7 @@ func Provider() terraform.ResourceProvider {
|
|||
return &schema.Provider{
|
||||
ResourcesMap: map[string]*schema.Resource{
|
||||
"terraform_remote_state": schema.DataSourceResourceShim(
|
||||
"terraform_remote_state",
|
||||
dataSourceRemoteState(),
|
||||
),
|
||||
},
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package schema
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DataSourceResourceShim takes a Resource instance describing a data source
|
||||
// (with a Read implementation and a Schema, at least) and returns a new
|
||||
// Resource instance with additional Create and Delete implementations that
|
||||
|
@ -14,11 +18,7 @@ package schema
|
|||
//
|
||||
// The provided Resource instance, and its schema, will be modified in-place
|
||||
// to make it suitable for use as a full resource.
|
||||
func DataSourceResourceShim(dataSource *Resource) *Resource {
|
||||
// TODO: Somehow emit a warning when a shim resource is used,
|
||||
// recommending that the user switch to using the data source
|
||||
// instead.
|
||||
|
||||
func DataSourceResourceShim(name string, dataSource *Resource) *Resource {
|
||||
// Recursively, in-place adjust the schema so that it has ForceNew
|
||||
// on any user-settable resource.
|
||||
dataSourceResourceShimAdjustSchema(dataSource.Schema)
|
||||
|
@ -30,6 +30,13 @@ func DataSourceResourceShim(dataSource *Resource) *Resource {
|
|||
}
|
||||
dataSource.Update = nil // should already be nil, but let's make sure
|
||||
|
||||
// FIXME: Link to some further docs either on the website or in the
|
||||
// changelog, once such a thing exists.
|
||||
dataSource.deprecationMessage = fmt.Sprintf(
|
||||
"using %s as a resource is deprecated; consider using the data source instead",
|
||||
name,
|
||||
)
|
||||
|
||||
return dataSource
|
||||
}
|
||||
|
||||
|
|
|
@ -89,6 +89,11 @@ type Resource struct {
|
|||
// must be validated. The validity of ResourceImporter is verified
|
||||
// by InternalValidate on Resource.
|
||||
Importer *ResourceImporter
|
||||
|
||||
// If non-empty, this string is emitted as a warning during Validate.
|
||||
// This is a private interface for now, for use by DataSourceResourceShim,
|
||||
// and not for general use. (But maybe later...)
|
||||
deprecationMessage string
|
||||
}
|
||||
|
||||
// See Resource documentation.
|
||||
|
@ -176,7 +181,13 @@ func (r *Resource) Diff(
|
|||
|
||||
// Validate validates the resource configuration against the schema.
|
||||
func (r *Resource) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
||||
return schemaMap(r.Schema).Validate(c)
|
||||
warns, errs := schemaMap(r.Schema).Validate(c)
|
||||
|
||||
if r.deprecationMessage != "" {
|
||||
warns = append(warns, r.deprecationMessage)
|
||||
}
|
||||
|
||||
return warns, errs
|
||||
}
|
||||
|
||||
// ReadDataApply loads the data for a data source, given a diff that
|
||||
|
|
Loading…
Reference in New Issue