2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-08-19 18:26:48 +02:00
|
|
|
"fmt"
|
2015-03-06 02:04:04 +01:00
|
|
|
"strconv"
|
2014-08-16 01:32:43 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
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.
|
|
|
|
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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
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
|
|
|
|
|
|
|
// 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
|
2014-08-13 23:23:22 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-16 01:32:43 +02:00
|
|
|
// Diff returns a diff of this resource and is API compatible with the
|
|
|
|
// ResourceProvider interface.
|
|
|
|
func (r *Resource) Diff(
|
2014-09-17 02:07:13 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
2014-08-16 01:32:43 +02:00
|
|
|
return schemaMap(r.Schema).Diff(s, c)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return schemaMap(r.Schema).Validate(c)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-08-18 04:45:26 +02:00
|
|
|
// Refresh refreshes the state of the resource.
|
|
|
|
func (r *Resource) Refresh(
|
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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exists, err := r.Exists(data, meta)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 02:04:04 +01:00
|
|
|
needsMigration, stateSchemaVersion := r.checkSchemaVersion(s)
|
|
|
|
if needsMigration && r.MigrateState != nil {
|
|
|
|
s, err := r.MigrateState(stateSchemaVersion, s, meta)
|
|
|
|
if err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-18 04:45:26 +02:00
|
|
|
data, err := schemaMap(r.Schema).Data(s, nil)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
2015-05-12 16:45:15 +02:00
|
|
|
func (r *Resource) InternalValidate(topSchemaMap schemaMap) error {
|
2014-08-13 23:23:22 +02:00
|
|
|
if r == nil {
|
|
|
|
return errors.New("resource is nil")
|
|
|
|
}
|
2015-05-12 16:45:15 +02:00
|
|
|
tsm := topSchemaMap
|
2014-08-13 23:23:22 +02:00
|
|
|
|
2015-04-03 16:57:30 +02:00
|
|
|
if r.isTopLevel() {
|
|
|
|
// 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
|
|
|
|
|
|
|
// If we have an importer, we need to verify the importer.
|
|
|
|
if r.Importer != nil {
|
|
|
|
if err := r.Importer.InternalValidate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 16:57:30 +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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-05-04 22:37:35 +02:00
|
|
|
// Set the schema version to latest by default
|
|
|
|
result.meta = map[string]string{
|
|
|
|
"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?
|
|
|
|
return r.Create != nil
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
stateSchemaVersion, _ := strconv.Atoi(is.Meta["schema_version"])
|
|
|
|
return stateSchemaVersion < r.SchemaVersion, stateSchemaVersion
|
|
|
|
}
|
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 {
|
|
|
|
state.Meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
state.Meta["schema_version"] = strconv.Itoa(r.SchemaVersion)
|
|
|
|
}
|
|
|
|
return state
|
|
|
|
}
|