2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-08-19 18:26:48 +02:00
|
|
|
"fmt"
|
2017-03-02 18:07:49 +01:00
|
|
|
"log"
|
2015-03-06 02:04:04 +01:00
|
|
|
"strconv"
|
2014-08-16 01:32:43 +02:00
|
|
|
|
2017-07-11 06:51:55 +02:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-08-16 01:32:43 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-08-01 00:42:44 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-08-13 23:23:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Resource represents a thing in Terraform that has a set of configurable
|
2014-08-27 06:52:09 +02:00
|
|
|
// attributes and a lifecycle (create, read, update, delete).
|
2014-08-13 23:23:22 +02:00
|
|
|
//
|
|
|
|
// The Resource schema is an abstraction that allows provider writers to
|
|
|
|
// worry only about CRUD operations while off-loading validation, diff
|
|
|
|
// generation, etc. to this higher level library.
|
2016-04-17 02:27:00 +02:00
|
|
|
//
|
|
|
|
// In spite of the name, this struct is not used only for terraform resources,
|
|
|
|
// but also for data sources. In the case of data sources, the Create,
|
|
|
|
// Update and Delete functions must not be provided.
|
2014-08-13 23:23:22 +02:00
|
|
|
type Resource struct {
|
2014-08-27 06:52:09 +02:00
|
|
|
// Schema is the schema for the configuration of this resource.
|
|
|
|
//
|
|
|
|
// The keys of this map are the configuration keys, and the values
|
|
|
|
// describe the schema of the configuration value.
|
|
|
|
//
|
|
|
|
// The schema is used to represent both configurable data as well
|
|
|
|
// as data that might be computed in the process of creating this
|
|
|
|
// resource.
|
2014-08-13 23:23:22 +02:00
|
|
|
Schema map[string]*Schema
|
|
|
|
|
2015-03-06 02:04:04 +01:00
|
|
|
// SchemaVersion is the version number for this resource's Schema
|
|
|
|
// definition. The current SchemaVersion stored in the state for each
|
|
|
|
// resource. Provider authors can increment this version number
|
|
|
|
// when Schema semantics change. If the State's SchemaVersion is less than
|
|
|
|
// the current SchemaVersion, the InstanceState is yielded to the
|
|
|
|
// MigrateState callback, where the provider can make whatever changes it
|
|
|
|
// needs to update the state to be compatible to the latest version of the
|
|
|
|
// Schema.
|
|
|
|
//
|
|
|
|
// When unset, SchemaVersion defaults to 0, so provider authors can start
|
|
|
|
// their Versioning at any integer >= 1
|
|
|
|
SchemaVersion int
|
|
|
|
|
2018-08-01 00:42:44 +02:00
|
|
|
// MigrateState is deprecated and any new changes to a resource's schema
|
2018-08-02 03:27:36 +02:00
|
|
|
// should be handled by StateUpgraders. Existing MigrateState implementations
|
2018-08-01 00:42:44 +02:00
|
|
|
// should remain for compatibility with existing state. MigrateState will
|
2018-08-02 14:34:19 +02:00
|
|
|
// still be called if the stored SchemaVersion is less than the
|
|
|
|
// first version of the StateUpgraders.
|
2018-08-01 00:42:44 +02:00
|
|
|
//
|
2015-03-06 02:04:04 +01:00
|
|
|
// MigrateState is responsible for updating an InstanceState with an old
|
|
|
|
// version to the format expected by the current version of the Schema.
|
|
|
|
//
|
|
|
|
// It is called during Refresh if the State's stored SchemaVersion is less
|
|
|
|
// than the current SchemaVersion of the Resource.
|
|
|
|
//
|
|
|
|
// The function is yielded the state's stored SchemaVersion and a pointer to
|
|
|
|
// the InstanceState that needs updating, as well as the configured
|
|
|
|
// provider's configured meta interface{}, in case the migration process
|
|
|
|
// needs to make any remote API calls.
|
|
|
|
MigrateState StateMigrateFunc
|
|
|
|
|
2018-08-02 03:27:36 +02:00
|
|
|
// StateUpgraders contains the functions responsible for upgrading an
|
|
|
|
// existing state with an old schema version to a newer schema. It is
|
|
|
|
// called specifically by Terraform when the stored schema version is less
|
|
|
|
// than the current SchemaVersion of the Resource.
|
2018-08-01 00:42:44 +02:00
|
|
|
//
|
2018-08-02 14:34:19 +02:00
|
|
|
// StateUpgraders map specific schema versions to a StateUpgrader
|
|
|
|
// function. The registered versions are expected to be ordered,
|
|
|
|
// consecutive values. The initial value may be greater than 0 to account
|
|
|
|
// for legacy schemas that weren't recorded and can be handled by
|
|
|
|
// MigrateState.
|
2018-08-02 03:27:36 +02:00
|
|
|
StateUpgraders []StateUpgrader
|
2018-08-01 00:42:44 +02:00
|
|
|
|
2014-08-27 06:52:09 +02:00
|
|
|
// The functions below are the CRUD operations for this resource.
|
|
|
|
//
|
|
|
|
// The only optional operation is Update. If Update is not implemented,
|
|
|
|
// then updates will not be supported for this resource.
|
|
|
|
//
|
|
|
|
// The ResourceData parameter in the functions below are used to
|
|
|
|
// query configuration and changes for the resource as well as to set
|
|
|
|
// the ID, computed data, etc.
|
|
|
|
//
|
|
|
|
// The interface{} parameter is the result of the ConfigureFunc in
|
|
|
|
// the provider for this resource. If the provider does not define
|
|
|
|
// a ConfigureFunc, this will be nil. This parameter should be used
|
|
|
|
// to store API clients, configuration structures, etc.
|
|
|
|
//
|
|
|
|
// If any errors occur during each of the operation, an error should be
|
|
|
|
// returned. If a resource was partially updated, be careful to enable
|
|
|
|
// partial state mode for ResourceData and use it accordingly.
|
2015-01-11 00:39:29 +01:00
|
|
|
//
|
|
|
|
// Exists is a function that is called to check if a resource still
|
|
|
|
// exists. If this returns false, then this will affect the diff
|
|
|
|
// accordingly. If this function isn't set, it will not be called. It
|
|
|
|
// is highly recommended to set it. The *ResourceData passed to Exists
|
|
|
|
// should _not_ be modified.
|
2014-08-13 23:23:22 +02:00
|
|
|
Create CreateFunc
|
|
|
|
Read ReadFunc
|
|
|
|
Update UpdateFunc
|
|
|
|
Delete DeleteFunc
|
2015-01-11 00:39:29 +01:00
|
|
|
Exists ExistsFunc
|
2016-04-26 18:36:05 +02:00
|
|
|
|
2017-05-31 19:42:41 +02:00
|
|
|
// CustomizeDiff is a custom function for working with the diff that
|
|
|
|
// Terraform has created for this resource - it can be used to customize the
|
|
|
|
// diff that has been created, diff values not controlled by configuration,
|
|
|
|
// or even veto the diff altogether and abort the plan. It is passed a
|
|
|
|
// *ResourceDiff, a structure similar to ResourceData but lacking most write
|
2017-07-05 16:05:37 +02:00
|
|
|
// functions like Set, while introducing new functions that work with the
|
|
|
|
// diff such as SetNew, SetNewComputed, and ForceNew.
|
|
|
|
//
|
|
|
|
// The phases Terraform runs this in, and the state available via functions
|
|
|
|
// like Get and GetChange, are as follows:
|
|
|
|
//
|
|
|
|
// * New resource: One run with no state
|
|
|
|
// * Existing resource: One run with state
|
|
|
|
// * Existing resource, forced new: One run with state (before ForceNew),
|
|
|
|
// then one run without state (as if new resource)
|
|
|
|
// * Tainted resource: No runs (custom diff logic is skipped)
|
|
|
|
// * Destroy: No runs (standard diff logic is skipped on destroy diffs)
|
|
|
|
//
|
|
|
|
// This function needs to be resilient to support all scenarios.
|
|
|
|
//
|
|
|
|
// If this function needs to access external API resources, remember to flag
|
|
|
|
// the RequiresRefresh attribute mentioned below to ensure that
|
|
|
|
// -refresh=false is blocked when running plan or apply, as this means that
|
|
|
|
// this resource requires refresh-like behaviour to work effectively.
|
helper/schema: New ResourceDiff object
This adds a new object, ResourceDiff, to the schema package. This
object, in conjunction with a function defined in CustomizeDiff in the
resource schema, allows for the in-flight customization of a Terraform
diff. This helps support use cases such as when there are necessary
changes to a resource that cannot be detected in config, such as via
computed fields (most of the utility in this object works on computed
fields only). It also allows for a wholesale wipe of the diff to allow
for diff logic completely offloaded to an external API, if it is a
better use case for a specific provider.
As part of this work, many internal diff functions have been moved to
use a special resourceDiffer interface, to allow for shared
functionality between ResourceDiff and ResourceData. This may be
extended to the DiffSuppressFunc as well which would restrict use of
ResourceData in DiffSuppressFunc to generally read-only fields.
This work is not yet in its final state - CustomizeDiff is not yet
implemented in the general diff workflow, new functions may be added
(notably Clear() for a single key), and functionality may be altered.
Tests will follow as well.
2017-05-23 06:32:59 +02:00
|
|
|
//
|
2017-05-31 05:42:37 +02:00
|
|
|
// For the most part, only computed fields can be customized by this
|
|
|
|
// function.
|
2017-07-09 07:16:51 +02:00
|
|
|
//
|
|
|
|
// This function is only allowed on regular resources (not data sources).
|
2017-05-31 19:42:41 +02:00
|
|
|
CustomizeDiff CustomizeDiffFunc
|
helper/schema: New ResourceDiff object
This adds a new object, ResourceDiff, to the schema package. This
object, in conjunction with a function defined in CustomizeDiff in the
resource schema, allows for the in-flight customization of a Terraform
diff. This helps support use cases such as when there are necessary
changes to a resource that cannot be detected in config, such as via
computed fields (most of the utility in this object works on computed
fields only). It also allows for a wholesale wipe of the diff to allow
for diff logic completely offloaded to an external API, if it is a
better use case for a specific provider.
As part of this work, many internal diff functions have been moved to
use a special resourceDiffer interface, to allow for shared
functionality between ResourceDiff and ResourceData. This may be
extended to the DiffSuppressFunc as well which would restrict use of
ResourceData in DiffSuppressFunc to generally read-only fields.
This work is not yet in its final state - CustomizeDiff is not yet
implemented in the general diff workflow, new functions may be added
(notably Clear() for a single key), and functionality may be altered.
Tests will follow as well.
2017-05-23 06:32:59 +02:00
|
|
|
|
2016-04-26 18:36:05 +02:00
|
|
|
// 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
|
2016-05-08 03:06:38 +02:00
|
|
|
|
|
|
|
// If non-empty, this string is emitted as a warning during Validate.
|
2018-06-20 20:21:46 +02:00
|
|
|
DeprecationMessage string
|
2017-03-02 18:07:49 +01:00
|
|
|
|
|
|
|
// Timeouts allow users to specify specific time durations in which an
|
|
|
|
// operation should time out, to allow them to extend an action to suit their
|
|
|
|
// usage. For example, a user may specify a large Creation timeout for their
|
|
|
|
// AWS RDS Instance due to it's size, or restoring from a snapshot.
|
|
|
|
// Resource implementors must enable Timeout support by adding the allowed
|
|
|
|
// actions (Create, Read, Update, Delete, Default) to the Resource struct, and
|
|
|
|
// accessing them in the matching methods.
|
|
|
|
Timeouts *ResourceTimeout
|
2014-08-13 23:23:22 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 04:57:55 +01:00
|
|
|
// ShimInstanceStateFromValue converts a cty.Value to a
|
|
|
|
// terraform.InstanceState.
|
|
|
|
func (r *Resource) ShimInstanceStateFromValue(state cty.Value) (*terraform.InstanceState, error) {
|
|
|
|
// Get the raw shimmed value. While this is correct, the set hashes don't
|
|
|
|
// match those from the Schema.
|
|
|
|
s := terraform.NewInstanceStateShimmedFromValue(state, r.SchemaVersion)
|
|
|
|
|
|
|
|
// We now rebuild the state through the ResourceData, so that the set indexes
|
|
|
|
// match what helper/schema expects.
|
|
|
|
data, err := schemaMap(r.Schema).Data(s, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s = data.State()
|
|
|
|
if s == nil {
|
|
|
|
s = &terraform.InstanceState{}
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2014-08-27 06:52:09 +02:00
|
|
|
// See Resource documentation.
|
|
|
|
type CreateFunc func(*ResourceData, interface{}) error
|
|
|
|
|
|
|
|
// See Resource documentation.
|
|
|
|
type ReadFunc func(*ResourceData, interface{}) error
|
|
|
|
|
|
|
|
// See Resource documentation.
|
|
|
|
type UpdateFunc func(*ResourceData, interface{}) error
|
|
|
|
|
|
|
|
// See Resource documentation.
|
|
|
|
type DeleteFunc func(*ResourceData, interface{}) error
|
|
|
|
|
2015-01-11 00:39:29 +01:00
|
|
|
// See Resource documentation.
|
|
|
|
type ExistsFunc func(*ResourceData, interface{}) (bool, error)
|
|
|
|
|
2015-03-06 02:04:04 +01:00
|
|
|
// See Resource documentation.
|
|
|
|
type StateMigrateFunc func(
|
|
|
|
int, *terraform.InstanceState, interface{}) (*terraform.InstanceState, error)
|
|
|
|
|
2018-08-02 03:27:36 +02:00
|
|
|
type StateUpgrader struct {
|
|
|
|
// Version is the version schema that this Upgrader will handle, converting
|
|
|
|
// it to Version+1.
|
|
|
|
Version int
|
|
|
|
|
|
|
|
// Type describes the schema that this function can upgrade. Type is
|
|
|
|
// required to decode the schema if the state was stored in a legacy
|
|
|
|
// flatmap format.
|
|
|
|
Type cty.Type
|
|
|
|
|
|
|
|
// Upgrade takes the JSON encoded state and the provider meta value, and
|
2018-08-02 14:34:19 +02:00
|
|
|
// upgrades the state one single schema version. The provided state is
|
|
|
|
// deocded into the default json types using a map[string]interface{}. It
|
|
|
|
// is up to the StateUpgradeFunc to ensure that the returned value can be
|
|
|
|
// encoded using the new schema.
|
2018-08-02 03:27:36 +02:00
|
|
|
Upgrade StateUpgradeFunc
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:34:19 +02:00
|
|
|
// See StateUpgrader
|
|
|
|
type StateUpgradeFunc func(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error)
|
2018-08-01 00:42:44 +02:00
|
|
|
|
helper/schema: New ResourceDiff object
This adds a new object, ResourceDiff, to the schema package. This
object, in conjunction with a function defined in CustomizeDiff in the
resource schema, allows for the in-flight customization of a Terraform
diff. This helps support use cases such as when there are necessary
changes to a resource that cannot be detected in config, such as via
computed fields (most of the utility in this object works on computed
fields only). It also allows for a wholesale wipe of the diff to allow
for diff logic completely offloaded to an external API, if it is a
better use case for a specific provider.
As part of this work, many internal diff functions have been moved to
use a special resourceDiffer interface, to allow for shared
functionality between ResourceDiff and ResourceData. This may be
extended to the DiffSuppressFunc as well which would restrict use of
ResourceData in DiffSuppressFunc to generally read-only fields.
This work is not yet in its final state - CustomizeDiff is not yet
implemented in the general diff workflow, new functions may be added
(notably Clear() for a single key), and functionality may be altered.
Tests will follow as well.
2017-05-23 06:32:59 +02:00
|
|
|
// See Resource documentation.
|
2017-05-31 19:42:41 +02:00
|
|
|
type CustomizeDiffFunc func(*ResourceDiff, interface{}) error
|
helper/schema: New ResourceDiff object
This adds a new object, ResourceDiff, to the schema package. This
object, in conjunction with a function defined in CustomizeDiff in the
resource schema, allows for the in-flight customization of a Terraform
diff. This helps support use cases such as when there are necessary
changes to a resource that cannot be detected in config, such as via
computed fields (most of the utility in this object works on computed
fields only). It also allows for a wholesale wipe of the diff to allow
for diff logic completely offloaded to an external API, if it is a
better use case for a specific provider.
As part of this work, many internal diff functions have been moved to
use a special resourceDiffer interface, to allow for shared
functionality between ResourceDiff and ResourceData. This may be
extended to the DiffSuppressFunc as well which would restrict use of
ResourceData in DiffSuppressFunc to generally read-only fields.
This work is not yet in its final state - CustomizeDiff is not yet
implemented in the general diff workflow, new functions may be added
(notably Clear() for a single key), and functionality may be altered.
Tests will follow as well.
2017-05-23 06:32:59 +02:00
|
|
|
|
2014-08-18 05:20:11 +02:00
|
|
|
// Apply creates, updates, and/or deletes a resource.
|
|
|
|
func (r *Resource) Apply(
|
2014-09-17 02:07:13 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff,
|
2014-09-17 02:07:13 +02:00
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2014-08-18 05:20:11 +02:00
|
|
|
data, err := schemaMap(r.Schema).Data(s, d)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
2017-03-02 18:07:49 +01:00
|
|
|
// Instance Diff shoould have the timeout info, need to copy it over to the
|
|
|
|
// ResourceData meta
|
|
|
|
rt := ResourceTimeout{}
|
|
|
|
if _, ok := d.Meta[TimeoutKey]; ok {
|
|
|
|
if err := rt.DiffDecode(d); err != nil {
|
|
|
|
log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
|
|
|
|
}
|
2017-06-22 23:24:24 +02:00
|
|
|
} else if s != nil {
|
|
|
|
if _, ok := s.Meta[TimeoutKey]; ok {
|
|
|
|
if err := rt.StateDecode(s); err != nil {
|
|
|
|
log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 18:07:49 +01:00
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] No meta timeoutkey found in Apply()")
|
|
|
|
}
|
|
|
|
data.timeouts = &rt
|
|
|
|
|
2014-08-18 05:20:11 +02:00
|
|
|
if s == nil {
|
|
|
|
// The Terraform API dictates that this should never happen, but
|
|
|
|
// it doesn't hurt to be safe in this case.
|
2014-09-17 02:07:13 +02:00
|
|
|
s = new(terraform.InstanceState)
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.Destroy || d.RequiresNew() {
|
|
|
|
if s.ID != "" {
|
|
|
|
// Destroy the resource since it is created
|
|
|
|
if err := r.Delete(data, meta); err != nil {
|
2015-08-03 22:52:04 +02:00
|
|
|
return r.recordCurrentSchemaVersion(data.State()), err
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 00:26:15 +02:00
|
|
|
// Make sure the ID is gone.
|
|
|
|
data.SetId("")
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're only destroying, and not creating, then return
|
|
|
|
// now since we're done!
|
|
|
|
if !d.RequiresNew() {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2014-10-21 09:28:41 +02:00
|
|
|
|
|
|
|
// Reset the data to be stateless since we just destroyed
|
|
|
|
data, err = schemaMap(r.Schema).Data(nil, d)
|
2017-03-02 18:07:49 +01:00
|
|
|
// data was reset, need to re-apply the parsed timeouts
|
|
|
|
data.timeouts = &rt
|
2014-10-21 09:28:41 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = nil
|
2014-08-28 00:26:15 +02:00
|
|
|
if data.Id() == "" {
|
2014-08-18 05:20:11 +02:00
|
|
|
// We're creating, it is a new resource.
|
2015-12-27 10:41:08 +01:00
|
|
|
data.MarkNewResource()
|
2014-08-18 05:20:11 +02:00
|
|
|
err = r.Create(data, meta)
|
|
|
|
} else {
|
2014-08-19 18:26:48 +02:00
|
|
|
if r.Update == nil {
|
2014-09-17 02:07:13 +02:00
|
|
|
return s, fmt.Errorf("doesn't support update")
|
2014-08-19 18:26:48 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 05:20:11 +02:00
|
|
|
err = r.Update(data, meta)
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:07:29 +01:00
|
|
|
return r.recordCurrentSchemaVersion(data.State()), err
|
2014-08-18 05:20:11 +02:00
|
|
|
}
|
|
|
|
|
2017-05-31 19:04:25 +02:00
|
|
|
// Diff returns a diff of this resource.
|
2014-08-16 01:32:43 +02:00
|
|
|
func (r *Resource) Diff(
|
2014-09-17 02:07:13 +02:00
|
|
|
s *terraform.InstanceState,
|
2017-05-28 07:58:44 +02:00
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.InstanceDiff, error) {
|
2017-03-02 18:07:49 +01:00
|
|
|
|
|
|
|
t := &ResourceTimeout{}
|
|
|
|
err := t.ConfigDecode(r, c)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("[ERR] Error decoding timeout: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-16 01:29:37 +02:00
|
|
|
instanceDiff, err := schemaMap(r.Schema).Diff(s, c, r.CustomizeDiff, meta, true)
|
|
|
|
if err != nil {
|
|
|
|
return instanceDiff, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if instanceDiff != nil {
|
|
|
|
if err := t.DiffEncode(instanceDiff); err != nil {
|
|
|
|
log.Printf("[ERR] Error encoding timeout to instance diff: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] Instance Diff is nil in Diff()")
|
|
|
|
}
|
|
|
|
|
|
|
|
return instanceDiff, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Resource) simpleDiff(
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
c *terraform.ResourceConfig,
|
|
|
|
meta interface{}) (*terraform.InstanceDiff, error) {
|
|
|
|
|
|
|
|
t := &ResourceTimeout{}
|
|
|
|
err := t.ConfigDecode(r, c)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("[ERR] Error decoding timeout: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
instanceDiff, err := schemaMap(r.Schema).Diff(s, c, r.CustomizeDiff, meta, false)
|
2017-03-02 18:07:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return instanceDiff, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:25:20 +02:00
|
|
|
if instanceDiff == nil {
|
|
|
|
log.Printf("[DEBUG] Instance Diff is nil in SimpleDiff()")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-19 02:05:33 +02:00
|
|
|
// Make sure the old value is set in each of the instance diffs.
|
|
|
|
// This was done by the RequiresNew logic in the full legacy Diff.
|
|
|
|
for k, attr := range instanceDiff.Attributes {
|
|
|
|
if attr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s != nil {
|
|
|
|
attr.Old = s.Attributes[k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:25:20 +02:00
|
|
|
if err := t.DiffEncode(instanceDiff); err != nil {
|
|
|
|
log.Printf("[ERR] Error encoding timeout to instance diff: %s", err)
|
2017-03-02 18:07:49 +01:00
|
|
|
}
|
|
|
|
return instanceDiff, err
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
// Validate validates the resource configuration against the schema.
|
|
|
|
func (r *Resource) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
2016-05-08 03:06:38 +02:00
|
|
|
warns, errs := schemaMap(r.Schema).Validate(c)
|
|
|
|
|
2018-06-20 20:21:46 +02:00
|
|
|
if r.DeprecationMessage != "" {
|
|
|
|
warns = append(warns, r.DeprecationMessage)
|
2016-05-08 03:06:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return warns, errs
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
core: New ResourceProvider methods for data resources
This is a breaking change to the ResourceProvider interface that adds the
new operations relating to data sources.
DataSources, ValidateDataSource, ReadDataDiff and ReadDataApply are the
data source equivalents of Resources, Validate, Diff and Apply (respectively)
for managed resources.
The diff/apply model seems at first glance a rather strange workflow for
read-only resources, but implementing data resources in this way allows them
to fit cleanly into the standard plan/apply lifecycle in cases where the
configuration contains computed arguments and thus the read must be deferred
until apply time.
Along with breaking the interface, we also fix up the plugin client/server
and helper/schema implementations of it, which are all of the callers
used when provider plugins use helper/schema. This would be a breaking
change for any provider plugin that directly implements the provider
interface, but no known plugins do this and it is not recommended.
At the helper/schema layer the implementer sees ReadDataApply as a "Read",
as opposed to "Create" or "Update" as in the managed resource Apply
implementation. The planning mechanics are handled entirely within
helper/schema, so that complexity is hidden from the provider implementation
itself.
2016-05-08 06:55:32 +02:00
|
|
|
// ReadDataApply loads the data for a data source, given a diff that
|
|
|
|
// describes the configuration arguments and desired computed attributes.
|
|
|
|
func (r *Resource) ReadDataApply(
|
|
|
|
d *terraform.InstanceDiff,
|
|
|
|
meta interface{},
|
|
|
|
) (*terraform.InstanceState, error) {
|
|
|
|
// Data sources are always built completely from scratch
|
|
|
|
// on each read, so the source state is always nil.
|
|
|
|
data, err := schemaMap(r.Schema).Data(nil, d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Read(data, meta)
|
|
|
|
state := data.State()
|
|
|
|
if state != nil && state.ID == "" {
|
|
|
|
// Data sources can set an ID if they want, but they aren't
|
|
|
|
// required to; we'll provide a placeholder if they don't,
|
|
|
|
// to preserve the invariant that all resources have non-empty
|
|
|
|
// ids.
|
|
|
|
state.ID = "-"
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.recordCurrentSchemaVersion(state), err
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:17:42 +02:00
|
|
|
// RefreshWithoutUpgrade reads the instance state, but does not call
|
|
|
|
// MigrateState or the StateUpgraders, since those are now invoked in a
|
|
|
|
// separate API call.
|
|
|
|
// RefreshWithoutUpgrade is part of the new plugin shims.
|
|
|
|
func (r *Resource) RefreshWithoutUpgrade(
|
2014-09-17 02:07:13 +02:00
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
2015-05-14 05:15:13 +02:00
|
|
|
// If the ID is already somehow blank, it doesn't exist
|
|
|
|
if s.ID == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-03-02 18:07:49 +01:00
|
|
|
rt := ResourceTimeout{}
|
|
|
|
if _, ok := s.Meta[TimeoutKey]; ok {
|
|
|
|
if err := rt.StateDecode(s); err != nil {
|
|
|
|
log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 00:39:29 +01:00
|
|
|
if r.Exists != nil {
|
|
|
|
// Make a copy of data so that if it is modified it doesn't
|
|
|
|
// affect our Read later.
|
|
|
|
data, err := schemaMap(r.Schema).Data(s, nil)
|
2017-03-02 18:07:49 +01:00
|
|
|
data.timeouts = &rt
|
|
|
|
|
2015-01-11 00:39:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := r.Exists(data, meta)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 18:17:42 +02:00
|
|
|
data, err := schemaMap(r.Schema).Data(s, nil)
|
|
|
|
data.timeouts = &rt
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Read(data, meta)
|
|
|
|
state := data.State()
|
|
|
|
if state != nil && state.ID == "" {
|
|
|
|
state = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.recordCurrentSchemaVersion(state), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh refreshes the state of the resource.
|
|
|
|
func (r *Resource) Refresh(
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
meta interface{}) (*terraform.InstanceState, error) {
|
|
|
|
// If the ID is already somehow blank, it doesn't exist
|
|
|
|
if s.ID == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rt := ResourceTimeout{}
|
|
|
|
if _, ok := s.Meta[TimeoutKey]; ok {
|
|
|
|
if err := rt.StateDecode(s); err != nil {
|
|
|
|
log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Exists != nil {
|
|
|
|
// Make a copy of data so that if it is modified it doesn't
|
|
|
|
// affect our Read later.
|
|
|
|
data, err := schemaMap(r.Schema).Data(s, nil)
|
|
|
|
data.timeouts = &rt
|
|
|
|
|
2015-03-06 02:04:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
2018-08-03 18:17:42 +02:00
|
|
|
|
|
|
|
exists, err := r.Exists(data, meta)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// there may be new StateUpgraders that need to be run
|
|
|
|
s, err := r.upgradeState(s, meta)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
2015-03-06 02:04:04 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 04:45:26 +02:00
|
|
|
data, err := schemaMap(r.Schema).Data(s, nil)
|
2017-03-02 18:07:49 +01:00
|
|
|
data.timeouts = &rt
|
2014-08-18 04:45:26 +02:00
|
|
|
if err != nil {
|
2014-08-27 00:50:31 +02:00
|
|
|
return s, err
|
2014-08-18 04:45:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = r.Read(data, meta)
|
2014-08-20 03:19:02 +02:00
|
|
|
state := data.State()
|
2014-08-22 07:19:33 +02:00
|
|
|
if state != nil && state.ID == "" {
|
2014-08-20 03:19:02 +02:00
|
|
|
state = nil
|
|
|
|
}
|
|
|
|
|
2015-03-19 01:07:29 +01:00
|
|
|
return r.recordCurrentSchemaVersion(state), err
|
2014-08-18 04:45:26 +02:00
|
|
|
}
|
|
|
|
|
2018-08-03 18:17:42 +02:00
|
|
|
func (r *Resource) upgradeState(s *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
needsMigration, stateSchemaVersion := r.checkSchemaVersion(s)
|
|
|
|
migrate := needsMigration && r.MigrateState != nil
|
|
|
|
|
|
|
|
if migrate {
|
|
|
|
s, err = r.MigrateState(stateSchemaVersion, s, meta)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.StateUpgraders) == 0 {
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we ran MigrateState, then the stateSchemaVersion value is no longer
|
|
|
|
// correct. We can expect the first upgrade function to be the correct
|
|
|
|
// schema type version.
|
|
|
|
if migrate {
|
|
|
|
stateSchemaVersion = r.StateUpgraders[0].Version
|
|
|
|
}
|
|
|
|
|
|
|
|
schemaType := r.CoreConfigSchema().ImpliedType()
|
|
|
|
// find the expected type to convert the state
|
|
|
|
for _, upgrader := range r.StateUpgraders {
|
|
|
|
if stateSchemaVersion == upgrader.Version {
|
|
|
|
schemaType = upgrader.Type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateUpgraders only operate on the new JSON format state, so the state
|
|
|
|
// need to be converted.
|
|
|
|
stateVal, err := StateValueFromInstanceState(s, schemaType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonState, err := StateValueToJSONMap(stateVal, schemaType)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, upgrader := range r.StateUpgraders {
|
|
|
|
if stateSchemaVersion != upgrader.Version {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonState, err = upgrader.Upgrade(jsonState, meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stateSchemaVersion++
|
|
|
|
}
|
|
|
|
|
|
|
|
// now we need to re-flatmap the new state
|
|
|
|
stateVal, err = JSONMapToStateValue(jsonState, r.CoreConfigSchema())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-14 04:57:55 +01:00
|
|
|
return r.ShimInstanceStateFromValue(stateVal)
|
2018-08-03 18:17:42 +02:00
|
|
|
}
|
|
|
|
|
2014-08-13 23:23:22 +02:00
|
|
|
// InternalValidate should be called to validate the structure
|
|
|
|
// of the resource.
|
|
|
|
//
|
|
|
|
// This should be called in a unit test for any resource to verify
|
|
|
|
// before release that a resource is properly configured for use with
|
|
|
|
// this library.
|
2014-08-27 06:52:09 +02:00
|
|
|
//
|
|
|
|
// Provider.InternalValidate() will automatically call this for all of
|
|
|
|
// the resources it manages, so you don't need to call this manually if it
|
|
|
|
// is part of a Provider.
|
2016-04-17 02:27:00 +02:00
|
|
|
func (r *Resource) InternalValidate(topSchemaMap schemaMap, writable bool) error {
|
2014-08-13 23:23:22 +02:00
|
|
|
if r == nil {
|
|
|
|
return errors.New("resource is nil")
|
|
|
|
}
|
2016-04-17 02:27:00 +02:00
|
|
|
|
|
|
|
if !writable {
|
|
|
|
if r.Create != nil || r.Update != nil || r.Delete != nil {
|
|
|
|
return fmt.Errorf("must not implement Create, Update or Delete")
|
|
|
|
}
|
2017-07-09 07:16:51 +02:00
|
|
|
|
|
|
|
// CustomizeDiff cannot be defined for read-only resources
|
|
|
|
if r.CustomizeDiff != nil {
|
|
|
|
return fmt.Errorf("cannot implement CustomizeDiff")
|
|
|
|
}
|
2016-04-17 02:27:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-12 16:45:15 +02:00
|
|
|
tsm := topSchemaMap
|
2014-08-13 23:23:22 +02:00
|
|
|
|
2016-04-17 02:27:00 +02:00
|
|
|
if r.isTopLevel() && writable {
|
2015-04-03 16:57:30 +02:00
|
|
|
// All non-Computed attributes must be ForceNew if Update is not defined
|
|
|
|
if r.Update == nil {
|
|
|
|
nonForceNewAttrs := make([]string, 0)
|
|
|
|
for k, v := range r.Schema {
|
|
|
|
if !v.ForceNew && !v.Computed {
|
|
|
|
nonForceNewAttrs = append(nonForceNewAttrs, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(nonForceNewAttrs) > 0 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"No Update defined, must set ForceNew on: %#v", nonForceNewAttrs)
|
|
|
|
}
|
2015-09-19 20:11:49 +02:00
|
|
|
} else {
|
|
|
|
nonUpdateableAttrs := make([]string, 0)
|
|
|
|
for k, v := range r.Schema {
|
2015-10-08 14:48:04 +02:00
|
|
|
if v.ForceNew || v.Computed && !v.Optional {
|
2015-09-19 20:11:49 +02:00
|
|
|
nonUpdateableAttrs = append(nonUpdateableAttrs, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateableAttrs := len(r.Schema) - len(nonUpdateableAttrs)
|
|
|
|
if updateableAttrs == 0 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"All fields are ForceNew or Computed w/out Optional, Update is superfluous")
|
|
|
|
}
|
2015-04-03 16:57:30 +02:00
|
|
|
}
|
2015-09-19 20:11:49 +02:00
|
|
|
|
2015-05-12 16:45:15 +02:00
|
|
|
tsm = schemaMap(r.Schema)
|
2016-04-26 18:36:05 +02:00
|
|
|
|
2016-10-30 23:04:32 +01:00
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:36:05 +02:00
|
|
|
// If we have an importer, we need to verify the importer.
|
|
|
|
if r.Importer != nil {
|
|
|
|
if err := r.Importer.InternalValidate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 08:44:36 +02:00
|
|
|
|
|
|
|
for k, f := range tsm {
|
|
|
|
if isReservedResourceFieldName(k, f) {
|
|
|
|
return fmt.Errorf("%s is a reserved field name", k)
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 16:57:30 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 03:27:36 +02:00
|
|
|
lastVersion := -1
|
|
|
|
for _, u := range r.StateUpgraders {
|
|
|
|
if lastVersion >= 0 && u.Version-lastVersion > 1 {
|
|
|
|
return fmt.Errorf("missing schema version between %d and %d", lastVersion, u.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Version >= r.SchemaVersion {
|
|
|
|
return fmt.Errorf("StateUpgrader version %d is >= current version %d", u.Version, r.SchemaVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !u.Type.IsObjectType() {
|
|
|
|
return fmt.Errorf("StateUpgrader %d type is not cty.Object", u.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Upgrade == nil {
|
|
|
|
return fmt.Errorf("StateUpgrader %d missing StateUpgradeFunc", u.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastVersion = u.Version
|
2018-08-01 00:42:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 03:27:36 +02:00
|
|
|
if lastVersion >= 0 && lastVersion != r.SchemaVersion-1 {
|
|
|
|
return fmt.Errorf("missing StateUpgrader between %d and %d", lastVersion, r.SchemaVersion)
|
2018-08-01 00:42:44 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 08:44:36 +02:00
|
|
|
// Data source
|
|
|
|
if r.isTopLevel() && !writable {
|
|
|
|
tsm = schemaMap(r.Schema)
|
|
|
|
for k, _ := range tsm {
|
|
|
|
if isReservedDataSourceFieldName(k) {
|
|
|
|
return fmt.Errorf("%s is a reserved field name", k)
|
|
|
|
}
|
2017-07-11 06:51:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 16:45:15 +02:00
|
|
|
return schemaMap(r.Schema).InternalValidate(tsm)
|
2014-08-13 23:23:22 +02:00
|
|
|
}
|
2015-03-06 02:04:04 +01:00
|
|
|
|
2017-10-26 08:44:36 +02:00
|
|
|
func isReservedDataSourceFieldName(name string) bool {
|
|
|
|
for _, reservedName := range config.ReservedDataSourceFields {
|
|
|
|
if name == reservedName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isReservedResourceFieldName(name string, s *Schema) bool {
|
|
|
|
// Allow phasing out "id"
|
|
|
|
// See https://github.com/terraform-providers/terraform-provider-aws/pull/1626#issuecomment-328881415
|
|
|
|
if name == "id" && (s.Deprecated != "" || s.Removed != "") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-11 06:51:55 +02:00
|
|
|
for _, reservedName := range config.ReservedResourceFields {
|
|
|
|
if name == reservedName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:17:26 +02:00
|
|
|
// Data returns a ResourceData struct for this Resource. Each return value
|
|
|
|
// is a separate copy and can be safely modified differently.
|
|
|
|
//
|
|
|
|
// The data returned from this function has no actual affect on the Resource
|
|
|
|
// itself (including the state given to this function).
|
|
|
|
//
|
|
|
|
// This function is useful for unit tests and ResourceImporter functions.
|
|
|
|
func (r *Resource) Data(s *terraform.InstanceState) *ResourceData {
|
|
|
|
result, err := schemaMap(r.Schema).Data(s, nil)
|
|
|
|
if err != nil {
|
|
|
|
// At the time of writing, this isn't possible (Data never returns
|
|
|
|
// non-nil errors). We panic to find this in the future if we have to.
|
|
|
|
// I don't see a reason for Data to ever return an error.
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:10:43 +01:00
|
|
|
// load the Resource timeouts
|
|
|
|
result.timeouts = r.Timeouts
|
|
|
|
if result.timeouts == nil {
|
|
|
|
result.timeouts = &ResourceTimeout{}
|
|
|
|
}
|
|
|
|
|
2016-05-04 22:37:35 +02:00
|
|
|
// Set the schema version to latest by default
|
2017-02-23 19:44:05 +01:00
|
|
|
result.meta = map[string]interface{}{
|
2016-05-04 22:37:35 +02:00
|
|
|
"schema_version": strconv.Itoa(r.SchemaVersion),
|
|
|
|
}
|
|
|
|
|
2016-04-26 18:17:26 +02:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:55:10 +01:00
|
|
|
// TestResourceData Yields a ResourceData filled with this resource's schema for use in unit testing
|
2016-04-26 18:17:26 +02:00
|
|
|
//
|
|
|
|
// TODO: May be able to be removed with the above ResourceData function.
|
2016-01-27 16:55:10 +01:00
|
|
|
func (r *Resource) TestResourceData() *ResourceData {
|
|
|
|
return &ResourceData{
|
|
|
|
schema: r.Schema,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:57:30 +02:00
|
|
|
// Returns true if the resource is "top level" i.e. not a sub-resource.
|
|
|
|
func (r *Resource) isTopLevel() bool {
|
|
|
|
// TODO: This is a heuristic; replace with a definitive attribute?
|
2017-10-26 08:44:36 +02:00
|
|
|
return (r.Create != nil || r.Read != nil)
|
2015-04-03 16:57:30 +02:00
|
|
|
}
|
|
|
|
|
2015-03-06 02:04:04 +01:00
|
|
|
// Determines if a given InstanceState needs to be migrated by checking the
|
|
|
|
// stored version number with the current SchemaVersion
|
|
|
|
func (r *Resource) checkSchemaVersion(is *terraform.InstanceState) (bool, int) {
|
2017-02-23 19:44:05 +01:00
|
|
|
// Get the raw interface{} value for the schema version. If it doesn't
|
|
|
|
// exist or is nil then set it to zero.
|
|
|
|
raw := is.Meta["schema_version"]
|
|
|
|
if raw == nil {
|
|
|
|
raw = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to convert it to a string. If it isn't a string then we pretend
|
|
|
|
// that it isn't set at all. It should never not be a string unless it
|
|
|
|
// was manually tampered with.
|
|
|
|
rawString, ok := raw.(string)
|
|
|
|
if !ok {
|
|
|
|
rawString = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
stateSchemaVersion, _ := strconv.Atoi(rawString)
|
2018-08-03 18:17:42 +02:00
|
|
|
|
|
|
|
// Don't run MigrateState if the version is handled by a StateUpgrader,
|
|
|
|
// since StateMigrateFuncs are not required to handle unknown versions
|
|
|
|
maxVersion := r.SchemaVersion
|
|
|
|
if len(r.StateUpgraders) > 0 {
|
|
|
|
maxVersion = r.StateUpgraders[0].Version
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateSchemaVersion < maxVersion, stateSchemaVersion
|
2015-03-06 02:04:04 +01:00
|
|
|
}
|
2015-03-19 01:07:29 +01:00
|
|
|
|
|
|
|
func (r *Resource) recordCurrentSchemaVersion(
|
|
|
|
state *terraform.InstanceState) *terraform.InstanceState {
|
|
|
|
if state != nil && r.SchemaVersion > 0 {
|
|
|
|
if state.Meta == nil {
|
2017-02-23 19:44:05 +01:00
|
|
|
state.Meta = make(map[string]interface{})
|
2015-03-19 01:07:29 +01:00
|
|
|
}
|
|
|
|
state.Meta["schema_version"] = strconv.Itoa(r.SchemaVersion)
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|
2016-10-25 18:45:39 +02:00
|
|
|
|
|
|
|
// Noop is a convenience implementation of resource function which takes
|
|
|
|
// no action and returns no error.
|
|
|
|
func Noop(*ResourceData, interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFromState is a convenience implementation of a resource function
|
|
|
|
// which sets the resource ID to empty string (to remove it from state)
|
|
|
|
// and returns no error.
|
|
|
|
func RemoveFromState(d *ResourceData, _ interface{}) error {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|