2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
2014-08-16 01:32:43 +02:00
|
|
|
import (
|
core: Allow dynamic attributes in helper/schema
The helper/schema framework for building providers previously validated
in all cases that each field being set in state was in the schema.
However, in order to support remote state in a usable fashion, the need
has arisen for the top level attributes of the resource to be created
dynamically. In order to still be able to use helper/schema, this commit
adds the capability to assign additional fields.
Though I do not forsee this being used by providers other than remote
state (and that eventually may move into Terraform Core rather than
being a provider), the usage and semantics are:
To opt into dynamic attributes, add a schema attribute named
"__has_dynamic_attributes", and make it an optional string with no
default value, in order that it does not appear in diffs:
"__has_dynamic_attributes": {
Type: schema.TypeString
Optional: true
}
In the read callback, use the d.UnsafeSetFieldRaw(key, value) function
to set the dynamic attributes.
Note that other fields in the schema _are_ copied into state, and that
the names of the schema fields cannot currently be used as dynamic
attribute names, as we check to ensure a value is not already set for a
given key.
2016-06-11 12:23:32 +02:00
|
|
|
"log"
|
2014-08-18 19:00:41 +02:00
|
|
|
"reflect"
|
2014-08-16 01:32:43 +02:00
|
|
|
"strings"
|
2014-08-19 00:41:12 +02:00
|
|
|
"sync"
|
2017-03-02 18:07:49 +01:00
|
|
|
"time"
|
2014-08-16 01:32:43 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-08-27 06:52:09 +02:00
|
|
|
// ResourceData is used to query and set the attributes of a resource.
|
|
|
|
//
|
|
|
|
// ResourceData is the primary argument received for CRUD operations on
|
|
|
|
// a resource as well as configuration of a provider. It is a powerful
|
|
|
|
// structure that can be used to not only query data, but check for changes,
|
|
|
|
// define partial state updates, etc.
|
|
|
|
//
|
|
|
|
// The most relevant methods to take a look at are Get, Set, and Partial.
|
|
|
|
type ResourceData struct {
|
|
|
|
// Settable (internally)
|
2017-03-02 18:07:49 +01:00
|
|
|
schema map[string]*Schema
|
|
|
|
config *terraform.ResourceConfig
|
|
|
|
state *terraform.InstanceState
|
|
|
|
diff *terraform.InstanceDiff
|
|
|
|
meta map[string]interface{}
|
|
|
|
timeouts *ResourceTimeout
|
2014-08-27 06:52:09 +02:00
|
|
|
|
|
|
|
// Don't set
|
2015-01-09 03:02:19 +01:00
|
|
|
multiReader *MultiLevelFieldReader
|
2015-01-10 20:44:26 +01:00
|
|
|
setWriter *MapFieldWriter
|
2015-01-09 03:02:19 +01:00
|
|
|
newState *terraform.InstanceState
|
|
|
|
partial bool
|
|
|
|
partialMap map[string]struct{}
|
|
|
|
once sync.Once
|
2015-12-27 10:41:08 +01:00
|
|
|
isNew bool
|
2017-11-07 22:32:08 +01:00
|
|
|
|
|
|
|
panicOnError bool
|
2014-08-27 06:52:09 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 08:03:04 +02:00
|
|
|
// getResult is the internal structure that is generated when a Get
|
|
|
|
// is called that contains some extra data that might be used.
|
|
|
|
type getResult struct {
|
2014-08-22 21:09:06 +02:00
|
|
|
Value interface{}
|
|
|
|
ValueProcessed interface{}
|
2014-10-10 04:09:06 +02:00
|
|
|
Computed bool
|
2014-08-22 21:09:06 +02:00
|
|
|
Exists bool
|
|
|
|
Schema *Schema
|
2014-08-22 08:03:04 +02:00
|
|
|
}
|
|
|
|
|
core: Allow dynamic attributes in helper/schema
The helper/schema framework for building providers previously validated
in all cases that each field being set in state was in the schema.
However, in order to support remote state in a usable fashion, the need
has arisen for the top level attributes of the resource to be created
dynamically. In order to still be able to use helper/schema, this commit
adds the capability to assign additional fields.
Though I do not forsee this being used by providers other than remote
state (and that eventually may move into Terraform Core rather than
being a provider), the usage and semantics are:
To opt into dynamic attributes, add a schema attribute named
"__has_dynamic_attributes", and make it an optional string with no
default value, in order that it does not appear in diffs:
"__has_dynamic_attributes": {
Type: schema.TypeString
Optional: true
}
In the read callback, use the d.UnsafeSetFieldRaw(key, value) function
to set the dynamic attributes.
Note that other fields in the schema _are_ copied into state, and that
the names of the schema fields cannot currently be used as dynamic
attribute names, as we check to ensure a value is not already set for a
given key.
2016-06-11 12:23:32 +02:00
|
|
|
// UnsafeSetFieldRaw allows setting arbitrary values in state to arbitrary
|
|
|
|
// values, bypassing schema. This MUST NOT be used in normal circumstances -
|
|
|
|
// it exists only to support the remote_state data source.
|
|
|
|
func (d *ResourceData) UnsafeSetFieldRaw(key string, value string) {
|
|
|
|
d.once.Do(d.init)
|
|
|
|
|
|
|
|
d.setWriter.unsafeWriteField(key, value)
|
|
|
|
}
|
2014-08-22 08:03:04 +02:00
|
|
|
|
|
|
|
// Get returns the data for the given key, or nil if the key doesn't exist
|
|
|
|
// in the schema.
|
2014-08-16 02:46:05 +02:00
|
|
|
//
|
2014-08-22 08:03:04 +02:00
|
|
|
// If the key does exist in the schema but doesn't exist in the configuration,
|
|
|
|
// then the default value for that type will be returned. For strings, this is
|
|
|
|
// "", for numbers it is 0, etc.
|
|
|
|
//
|
2014-08-27 06:52:09 +02:00
|
|
|
// If you want to test if something is set at all in the configuration,
|
|
|
|
// use GetOk.
|
2014-08-15 04:55:47 +02:00
|
|
|
func (d *ResourceData) Get(key string) interface{} {
|
2014-08-22 08:03:04 +02:00
|
|
|
v, _ := d.GetOk(key)
|
|
|
|
return v
|
2014-08-18 18:58:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetChange returns the old and new value for a given key.
|
|
|
|
//
|
2014-08-27 06:52:09 +02:00
|
|
|
// HasChange should be used to check if a change exists. It is possible
|
|
|
|
// that both the old and new value are the same if the old value was not
|
|
|
|
// set and the new value is. This is common, for example, for boolean
|
|
|
|
// fields which have a zero value of false.
|
2014-08-18 18:58:44 +02:00
|
|
|
func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
|
2015-02-18 00:43:19 +01:00
|
|
|
o, n := d.getChange(key, getSourceState, getSourceDiff)
|
2014-08-22 08:03:04 +02:00
|
|
|
return o.Value, n.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOk returns the data for the given key and whether or not the key
|
2015-02-18 01:12:02 +01:00
|
|
|
// has been set to a non-zero value at some point.
|
2014-08-27 06:52:09 +02:00
|
|
|
//
|
|
|
|
// The first result will not necessarilly be nil if the value doesn't exist.
|
|
|
|
// The second result should be checked to determine this information.
|
2014-08-22 08:03:04 +02:00
|
|
|
func (d *ResourceData) GetOk(key string) (interface{}, bool) {
|
2015-01-10 21:25:34 +01:00
|
|
|
r := d.getRaw(key, getSourceSet)
|
2015-02-18 01:12:02 +01:00
|
|
|
exists := r.Exists && !r.Computed
|
2015-02-18 01:55:39 +01:00
|
|
|
if exists {
|
|
|
|
// If it exists, we also want to verify it is not the zero-value.
|
|
|
|
value := r.Value
|
|
|
|
zero := r.Schema.Type.Zero()
|
|
|
|
|
|
|
|
if eq, ok := value.(Equal); ok {
|
|
|
|
exists = !eq.Equal(zero)
|
|
|
|
} else {
|
|
|
|
exists = !reflect.DeepEqual(value, zero)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 01:12:02 +01:00
|
|
|
return r.Value, exists
|
2014-08-22 21:09:06 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 18:05:19 +02:00
|
|
|
// GetOkExists returns the data for a given key and whether or not the key
|
2017-08-02 17:18:59 +02:00
|
|
|
// has been set to a non-zero value. This is only useful for determining
|
|
|
|
// if boolean attributes have been set, if they are Optional but do not
|
|
|
|
// have a Default value.
|
|
|
|
//
|
|
|
|
// This is nearly the same function as GetOk, yet it does not check
|
|
|
|
// for the zero value of the attribute's type. This allows for attributes
|
|
|
|
// without a default, to fully check for a literal assignment, regardless
|
|
|
|
// of the zero-value for that type.
|
|
|
|
// This should only be used if absolutely required/needed.
|
2017-08-03 18:05:19 +02:00
|
|
|
func (d *ResourceData) GetOkExists(key string) (interface{}, bool) {
|
2017-08-02 17:18:59 +02:00
|
|
|
r := d.getRaw(key, getSourceSet)
|
|
|
|
exists := r.Exists && !r.Computed
|
|
|
|
return r.Value, exists
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:19:44 +02:00
|
|
|
func (d *ResourceData) getRaw(key string, level getSource) getResult {
|
2014-08-22 08:03:04 +02:00
|
|
|
var parts []string
|
|
|
|
if key != "" {
|
|
|
|
parts = strings.Split(key, ".")
|
|
|
|
}
|
|
|
|
|
2015-01-10 21:22:05 +01:00
|
|
|
return d.get(parts, level)
|
2014-08-16 01:32:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:00:41 +02:00
|
|
|
// HasChange returns whether or not the given key has been changed.
|
|
|
|
func (d *ResourceData) HasChange(key string) bool {
|
|
|
|
o, n := d.GetChange(key)
|
2015-01-16 14:13:40 +01:00
|
|
|
|
2015-01-16 18:32:15 +01:00
|
|
|
// If the type implements the Equal interface, then call that
|
|
|
|
// instead of just doing a reflect.DeepEqual. An example where this is
|
|
|
|
// needed is *Set
|
|
|
|
if eq, ok := o.(Equal); ok {
|
|
|
|
return !eq.Equal(n)
|
2015-01-16 14:13:40 +01:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:00:41 +02:00
|
|
|
return !reflect.DeepEqual(o, n)
|
|
|
|
}
|
|
|
|
|
2014-08-27 05:19:44 +02:00
|
|
|
// Partial turns partial state mode on/off.
|
|
|
|
//
|
|
|
|
// When partial state mode is enabled, then only key prefixes specified
|
|
|
|
// by SetPartial will be in the final state. This allows providers to return
|
|
|
|
// partial states for partially applied resources (when errors occur).
|
|
|
|
func (d *ResourceData) Partial(on bool) {
|
|
|
|
d.partial = on
|
|
|
|
if on {
|
2014-10-09 01:35:14 +02:00
|
|
|
if d.partialMap == nil {
|
|
|
|
d.partialMap = make(map[string]struct{})
|
|
|
|
}
|
2014-08-27 05:19:44 +02:00
|
|
|
} else {
|
|
|
|
d.partialMap = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-17 00:02:51 +02:00
|
|
|
// Set sets the value for the given key.
|
|
|
|
//
|
|
|
|
// If the key is invalid or the value is not a correct type, an error
|
|
|
|
// will be returned.
|
|
|
|
func (d *ResourceData) Set(key string, value interface{}) error {
|
2015-01-09 03:02:19 +01:00
|
|
|
d.once.Do(d.init)
|
2015-03-03 06:06:14 +01:00
|
|
|
|
|
|
|
// If the value is a pointer to a non-struct, get its value and
|
|
|
|
// use that. This allows Set to take a pointer to primitives to
|
|
|
|
// simplify the interface.
|
|
|
|
reflectVal := reflect.ValueOf(value)
|
|
|
|
if reflectVal.Kind() == reflect.Ptr {
|
2015-03-03 08:37:43 +01:00
|
|
|
if reflectVal.IsNil() {
|
|
|
|
// If the pointer is nil, then the value is just nil
|
|
|
|
value = nil
|
|
|
|
} else {
|
|
|
|
// Otherwise, we dereference the pointer as long as its not
|
|
|
|
// a pointer to a struct, since struct pointers are allowed.
|
|
|
|
reflectVal = reflect.Indirect(reflectVal)
|
|
|
|
if reflectVal.Kind() != reflect.Struct {
|
|
|
|
value = reflectVal.Interface()
|
|
|
|
}
|
2015-03-03 06:06:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 22:32:08 +01:00
|
|
|
err := d.setWriter.WriteField(strings.Split(key, "."), value)
|
|
|
|
if err != nil && d.panicOnError {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return err
|
2014-08-17 00:02:51 +02:00
|
|
|
}
|
|
|
|
|
2015-01-10 21:18:32 +01:00
|
|
|
// SetPartial adds the key to the final state output while
|
|
|
|
// in partial state mode. The key must be a root key in the schema (i.e.
|
|
|
|
// it cannot be "list.0").
|
2014-08-27 05:19:44 +02:00
|
|
|
//
|
|
|
|
// If partial state mode is disabled, then this has no effect. Additionally,
|
|
|
|
// whenever partial state mode is toggled, the partial data is cleared.
|
|
|
|
func (d *ResourceData) SetPartial(k string) {
|
|
|
|
if d.partial {
|
|
|
|
d.partialMap[k] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 10:41:08 +01:00
|
|
|
func (d *ResourceData) MarkNewResource() {
|
|
|
|
d.isNew = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) IsNewResource() bool {
|
|
|
|
return d.isNew
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:32:40 +02:00
|
|
|
// Id returns the ID of the resource.
|
|
|
|
func (d *ResourceData) Id() string {
|
|
|
|
var result string
|
|
|
|
|
|
|
|
if d.state != nil {
|
|
|
|
result = d.state.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.newState != nil {
|
|
|
|
result = d.newState.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:15:47 +02:00
|
|
|
// ConnInfo returns the connection info for this resource.
|
|
|
|
func (d *ResourceData) ConnInfo() map[string]string {
|
|
|
|
if d.newState != nil {
|
2014-09-17 02:07:13 +02:00
|
|
|
return d.newState.Ephemeral.ConnInfo
|
2014-08-22 07:15:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.state != nil {
|
2014-09-17 02:07:13 +02:00
|
|
|
return d.state.Ephemeral.ConnInfo
|
2014-08-19 00:41:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-18 05:20:11 +02:00
|
|
|
// SetId sets the ID of the resource. If the value is blank, then the
|
|
|
|
// resource is destroyed.
|
|
|
|
func (d *ResourceData) SetId(v string) {
|
2014-08-19 00:41:12 +02:00
|
|
|
d.once.Do(d.init)
|
2014-08-18 05:20:11 +02:00
|
|
|
d.newState.ID = v
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:15:47 +02:00
|
|
|
// SetConnInfo sets the connection info for a resource.
|
|
|
|
func (d *ResourceData) SetConnInfo(v map[string]string) {
|
|
|
|
d.once.Do(d.init)
|
2014-09-17 02:07:13 +02:00
|
|
|
d.newState.Ephemeral.ConnInfo = v
|
2014-08-19 00:41:12 +02:00
|
|
|
}
|
|
|
|
|
2016-04-26 21:25:50 +02:00
|
|
|
// SetType sets the ephemeral type for the data. This is only required
|
|
|
|
// for importing.
|
|
|
|
func (d *ResourceData) SetType(t string) {
|
|
|
|
d.once.Do(d.init)
|
|
|
|
d.newState.Ephemeral.Type = t
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:07:13 +02:00
|
|
|
// State returns the new InstanceState after the diff and any Set
|
2014-08-17 23:12:54 +02:00
|
|
|
// calls.
|
2014-09-17 02:07:13 +02:00
|
|
|
func (d *ResourceData) State() *terraform.InstanceState {
|
|
|
|
var result terraform.InstanceState
|
2014-08-18 18:32:40 +02:00
|
|
|
result.ID = d.Id()
|
2016-05-04 22:37:35 +02:00
|
|
|
result.Meta = d.meta
|
2014-08-22 07:19:33 +02:00
|
|
|
|
|
|
|
// If we have no ID, then this resource doesn't exist and we just
|
|
|
|
// return nil.
|
|
|
|
if result.ID == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-02 18:07:49 +01:00
|
|
|
if d.timeouts != nil {
|
|
|
|
if err := d.timeouts.StateEncode(&result); err != nil {
|
|
|
|
log.Printf("[ERR] Error encoding Timeout meta to Instance State: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
core: Allow dynamic attributes in helper/schema
The helper/schema framework for building providers previously validated
in all cases that each field being set in state was in the schema.
However, in order to support remote state in a usable fashion, the need
has arisen for the top level attributes of the resource to be created
dynamically. In order to still be able to use helper/schema, this commit
adds the capability to assign additional fields.
Though I do not forsee this being used by providers other than remote
state (and that eventually may move into Terraform Core rather than
being a provider), the usage and semantics are:
To opt into dynamic attributes, add a schema attribute named
"__has_dynamic_attributes", and make it an optional string with no
default value, in order that it does not appear in diffs:
"__has_dynamic_attributes": {
Type: schema.TypeString
Optional: true
}
In the read callback, use the d.UnsafeSetFieldRaw(key, value) function
to set the dynamic attributes.
Note that other fields in the schema _are_ copied into state, and that
the names of the schema fields cannot currently be used as dynamic
attribute names, as we check to ensure a value is not already set for a
given key.
2016-06-11 12:23:32 +02:00
|
|
|
// Look for a magic key in the schema that determines we skip the
|
|
|
|
// integrity check of fields existing in the schema, allowing dynamic
|
|
|
|
// keys to be created.
|
|
|
|
hasDynamicAttributes := false
|
|
|
|
for k, _ := range d.schema {
|
|
|
|
if k == "__has_dynamic_attributes" {
|
|
|
|
hasDynamicAttributes = true
|
|
|
|
log.Printf("[INFO] Resource %s has dynamic attributes", result.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 21:18:32 +01:00
|
|
|
// In order to build the final state attributes, we read the full
|
|
|
|
// attribute set as a map[string]interface{}, write it to a MapFieldWriter,
|
|
|
|
// and then use that map.
|
|
|
|
rawMap := make(map[string]interface{})
|
2015-11-18 11:24:04 +01:00
|
|
|
for k := range d.schema {
|
2015-01-10 21:18:32 +01:00
|
|
|
source := getSourceSet
|
|
|
|
if d.partial {
|
|
|
|
source = getSourceState
|
|
|
|
if _, ok := d.partialMap[k]; ok {
|
|
|
|
source = getSourceSet
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 21:22:05 +01:00
|
|
|
raw := d.get([]string{k}, source)
|
2015-01-15 19:35:44 +01:00
|
|
|
if raw.Exists && !raw.Computed {
|
|
|
|
rawMap[k] = raw.Value
|
|
|
|
if raw.ValueProcessed != nil {
|
|
|
|
rawMap[k] = raw.ValueProcessed
|
|
|
|
}
|
2015-01-10 21:18:32 +01:00
|
|
|
}
|
|
|
|
}
|
core: Allow dynamic attributes in helper/schema
The helper/schema framework for building providers previously validated
in all cases that each field being set in state was in the schema.
However, in order to support remote state in a usable fashion, the need
has arisen for the top level attributes of the resource to be created
dynamically. In order to still be able to use helper/schema, this commit
adds the capability to assign additional fields.
Though I do not forsee this being used by providers other than remote
state (and that eventually may move into Terraform Core rather than
being a provider), the usage and semantics are:
To opt into dynamic attributes, add a schema attribute named
"__has_dynamic_attributes", and make it an optional string with no
default value, in order that it does not appear in diffs:
"__has_dynamic_attributes": {
Type: schema.TypeString
Optional: true
}
In the read callback, use the d.UnsafeSetFieldRaw(key, value) function
to set the dynamic attributes.
Note that other fields in the schema _are_ copied into state, and that
the names of the schema fields cannot currently be used as dynamic
attribute names, as we check to ensure a value is not already set for a
given key.
2016-06-11 12:23:32 +02:00
|
|
|
|
2015-01-10 21:18:32 +01:00
|
|
|
mapW := &MapFieldWriter{Schema: d.schema}
|
|
|
|
if err := mapW.WriteField(nil, rawMap); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Attributes = mapW.Map()
|
core: Allow dynamic attributes in helper/schema
The helper/schema framework for building providers previously validated
in all cases that each field being set in state was in the schema.
However, in order to support remote state in a usable fashion, the need
has arisen for the top level attributes of the resource to be created
dynamically. In order to still be able to use helper/schema, this commit
adds the capability to assign additional fields.
Though I do not forsee this being used by providers other than remote
state (and that eventually may move into Terraform Core rather than
being a provider), the usage and semantics are:
To opt into dynamic attributes, add a schema attribute named
"__has_dynamic_attributes", and make it an optional string with no
default value, in order that it does not appear in diffs:
"__has_dynamic_attributes": {
Type: schema.TypeString
Optional: true
}
In the read callback, use the d.UnsafeSetFieldRaw(key, value) function
to set the dynamic attributes.
Note that other fields in the schema _are_ copied into state, and that
the names of the schema fields cannot currently be used as dynamic
attribute names, as we check to ensure a value is not already set for a
given key.
2016-06-11 12:23:32 +02:00
|
|
|
|
|
|
|
if hasDynamicAttributes {
|
|
|
|
// If we have dynamic attributes, just copy the attributes map
|
|
|
|
// one for one into the result attributes.
|
|
|
|
for k, v := range d.setWriter.Map() {
|
|
|
|
// Don't clobber schema values. This limits usage of dynamic
|
|
|
|
// attributes to names which _do not_ conflict with schema
|
|
|
|
// keys!
|
|
|
|
if _, ok := result.Attributes[k]; !ok {
|
|
|
|
result.Attributes[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 21:25:50 +02:00
|
|
|
if d.newState != nil {
|
|
|
|
result.Ephemeral = d.newState.Ephemeral
|
|
|
|
}
|
2014-08-18 04:45:26 +02:00
|
|
|
|
2015-01-10 21:18:32 +01:00
|
|
|
// TODO: This is hacky and we can remove this when we have a proper
|
|
|
|
// state writer. We should instead have a proper StateFieldWriter
|
|
|
|
// and use that.
|
|
|
|
for k, schema := range d.schema {
|
|
|
|
if schema.Type != TypeMap {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Attributes[k] == "" {
|
|
|
|
delete(result.Attributes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:37:59 +02:00
|
|
|
if v := d.Id(); v != "" {
|
|
|
|
result.Attributes["id"] = d.Id()
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:55:29 +02:00
|
|
|
if d.state != nil {
|
|
|
|
result.Tainted = d.state.Tainted
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:12:54 +02:00
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
2017-03-02 18:07:49 +01:00
|
|
|
// Timeout returns the data for the given timeout key
|
|
|
|
// Returns a duration of 20 minutes for any key not found, or not found and no default.
|
|
|
|
func (d *ResourceData) Timeout(key string) time.Duration {
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
|
2018-03-22 20:10:43 +01:00
|
|
|
// System default of 20 minutes
|
|
|
|
defaultTimeout := 20 * time.Minute
|
|
|
|
|
|
|
|
if d.timeouts == nil {
|
|
|
|
return defaultTimeout
|
|
|
|
}
|
|
|
|
|
2017-03-02 18:07:49 +01:00
|
|
|
var timeout *time.Duration
|
|
|
|
switch key {
|
|
|
|
case TimeoutCreate:
|
|
|
|
timeout = d.timeouts.Create
|
|
|
|
case TimeoutRead:
|
|
|
|
timeout = d.timeouts.Read
|
|
|
|
case TimeoutUpdate:
|
|
|
|
timeout = d.timeouts.Update
|
|
|
|
case TimeoutDelete:
|
|
|
|
timeout = d.timeouts.Delete
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout != nil {
|
|
|
|
return *timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.timeouts.Default != nil {
|
|
|
|
return *d.timeouts.Default
|
|
|
|
}
|
|
|
|
|
2018-03-22 20:10:43 +01:00
|
|
|
return defaultTimeout
|
2017-03-02 18:07:49 +01:00
|
|
|
}
|
|
|
|
|
2014-08-19 00:41:12 +02:00
|
|
|
func (d *ResourceData) init() {
|
2015-01-09 03:02:19 +01:00
|
|
|
// Initialize the field that will store our new state
|
2014-09-17 02:07:13 +02:00
|
|
|
var copyState terraform.InstanceState
|
2014-08-19 00:41:12 +02:00
|
|
|
if d.state != nil {
|
2016-04-26 21:25:50 +02:00
|
|
|
copyState = *d.state.DeepCopy()
|
2014-08-19 00:41:12 +02:00
|
|
|
}
|
|
|
|
d.newState = ©State
|
2015-01-09 03:02:19 +01:00
|
|
|
|
|
|
|
// Initialize the map for storing set data
|
2015-01-10 20:44:26 +01:00
|
|
|
d.setWriter = &MapFieldWriter{Schema: d.schema}
|
2015-01-09 03:02:19 +01:00
|
|
|
|
|
|
|
// Initialize the reader for getting data from the
|
|
|
|
// underlying sources (config, diff, etc.)
|
|
|
|
readers := make(map[string]FieldReader)
|
|
|
|
var stateAttributes map[string]string
|
|
|
|
if d.state != nil {
|
|
|
|
stateAttributes = d.state.Attributes
|
|
|
|
readers["state"] = &MapFieldReader{
|
|
|
|
Schema: d.schema,
|
|
|
|
Map: BasicMapReader(stateAttributes),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.config != nil {
|
|
|
|
readers["config"] = &ConfigFieldReader{
|
|
|
|
Schema: d.schema,
|
|
|
|
Config: d.config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if d.diff != nil {
|
|
|
|
readers["diff"] = &DiffFieldReader{
|
|
|
|
Schema: d.schema,
|
|
|
|
Diff: d.diff,
|
|
|
|
Source: &MultiLevelFieldReader{
|
|
|
|
Levels: []string{"state", "config"},
|
|
|
|
Readers: readers,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
readers["set"] = &MapFieldReader{
|
|
|
|
Schema: d.schema,
|
2015-01-10 20:44:26 +01:00
|
|
|
Map: BasicMapReader(d.setWriter.Map()),
|
2015-01-09 03:02:19 +01:00
|
|
|
}
|
|
|
|
d.multiReader = &MultiLevelFieldReader{
|
|
|
|
Levels: []string{
|
|
|
|
"state",
|
|
|
|
"config",
|
|
|
|
"diff",
|
|
|
|
"set",
|
|
|
|
},
|
|
|
|
|
|
|
|
Readers: readers,
|
|
|
|
}
|
2014-08-19 00:41:12 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 04:09:06 +02:00
|
|
|
func (d *ResourceData) diffChange(
|
2017-12-20 00:46:58 +01:00
|
|
|
k string) (interface{}, interface{}, bool, bool, bool) {
|
2014-08-21 00:45:34 +02:00
|
|
|
// Get the change between the state and the config.
|
2014-10-11 19:40:54 +02:00
|
|
|
o, n := d.getChange(k, getSourceState, getSourceConfig|getSourceExact)
|
2014-08-22 08:03:04 +02:00
|
|
|
if !o.Exists {
|
|
|
|
o.Value = nil
|
|
|
|
}
|
|
|
|
if !n.Exists {
|
|
|
|
n.Value = nil
|
|
|
|
}
|
2014-08-21 00:45:34 +02:00
|
|
|
|
|
|
|
// Return the old, new, and whether there is a change
|
2017-12-20 00:46:58 +01:00
|
|
|
return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value), n.Computed, false
|
2014-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *ResourceData) getChange(
|
2015-11-18 11:24:04 +01:00
|
|
|
k string,
|
2014-08-21 00:45:34 +02:00
|
|
|
oldLevel getSource,
|
2014-08-22 08:03:04 +02:00
|
|
|
newLevel getSource) (getResult, getResult) {
|
2014-08-21 06:02:42 +02:00
|
|
|
var parts, parts2 []string
|
2015-11-18 11:24:04 +01:00
|
|
|
if k != "" {
|
|
|
|
parts = strings.Split(k, ".")
|
|
|
|
parts2 = strings.Split(k, ".")
|
2014-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
2015-01-10 21:22:05 +01:00
|
|
|
o := d.get(parts, oldLevel)
|
|
|
|
n := d.get(parts2, newLevel)
|
2014-08-21 00:45:34 +02:00
|
|
|
return o, n
|
|
|
|
}
|
|
|
|
|
2015-01-10 21:22:05 +01:00
|
|
|
func (d *ResourceData) get(addr []string, source getSource) getResult {
|
2015-01-09 03:02:19 +01:00
|
|
|
d.once.Do(d.init)
|
|
|
|
|
|
|
|
level := "set"
|
|
|
|
flags := source & ^getSourceLevelMask
|
|
|
|
exact := flags&getSourceExact != 0
|
|
|
|
source = source & getSourceLevelMask
|
|
|
|
if source >= getSourceSet {
|
|
|
|
level = "set"
|
2015-01-10 21:25:34 +01:00
|
|
|
} else if source >= getSourceDiff {
|
2015-01-09 03:02:19 +01:00
|
|
|
level = "diff"
|
|
|
|
} else if source >= getSourceConfig {
|
|
|
|
level = "config"
|
|
|
|
} else {
|
|
|
|
level = "state"
|
|
|
|
}
|
|
|
|
|
|
|
|
var result FieldReadResult
|
|
|
|
var err error
|
|
|
|
if exact {
|
|
|
|
result, err = d.multiReader.ReadFieldExact(addr, level)
|
|
|
|
} else {
|
|
|
|
result, err = d.multiReader.ReadFieldMerge(addr, level)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the result doesn't exist, then we set the value to the zero value
|
2015-02-18 01:12:02 +01:00
|
|
|
var schema *Schema
|
|
|
|
if schemaL := addrToSchema(addr, d.schema); len(schemaL) > 0 {
|
|
|
|
schema = schemaL[len(schemaL)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Value == nil && schema != nil {
|
|
|
|
result.Value = result.ValueOrZero(schema)
|
2015-01-09 03:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transform the FieldReadResult into a getResult. It might be worth
|
|
|
|
// merging these two structures one day.
|
|
|
|
return getResult{
|
|
|
|
Value: result.Value,
|
|
|
|
ValueProcessed: result.ValueProcessed,
|
|
|
|
Computed: result.Computed,
|
|
|
|
Exists: result.Exists,
|
2015-02-18 01:12:02 +01:00
|
|
|
Schema: schema,
|
2014-08-21 02:51:27 +02:00
|
|
|
}
|
|
|
|
}
|