2014-08-27 06:52:09 +02:00
|
|
|
// schema is a high-level framework for easily writing new providers
|
|
|
|
// for Terraform. Usage of schema is recommended over attempting to write
|
|
|
|
// to the low-level plugin interfaces manually.
|
|
|
|
//
|
|
|
|
// schema breaks down provider creation into simple CRUD operations for
|
|
|
|
// resources. The logic of diffing, destroying before creating, updating
|
|
|
|
// or creating, etc. is all handled by the framework. The plugin author
|
|
|
|
// only needs to implement a configuration schema and the CRUD operations and
|
|
|
|
// everything else is meant to just work.
|
|
|
|
//
|
|
|
|
// A good starting point is to view the Provider structure.
|
2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
2014-08-15 04:55:47 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2015-01-16 18:22:09 +01:00
|
|
|
"os"
|
2014-08-18 23:20:57 +02:00
|
|
|
"reflect"
|
2014-09-29 23:00:35 +02:00
|
|
|
"sort"
|
2014-08-19 01:54:30 +02:00
|
|
|
"strconv"
|
2015-05-12 16:45:15 +02:00
|
|
|
"strings"
|
2014-08-15 04:55:47 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2014-08-13 23:23:22 +02:00
|
|
|
// Schema is used to describe the structure of a value.
|
2014-08-27 06:52:09 +02:00
|
|
|
//
|
|
|
|
// Read the documentation of the struct elements for important details.
|
2014-08-13 23:23:22 +02:00
|
|
|
type Schema struct {
|
|
|
|
// Type is the type of the value and must be one of the ValueType values.
|
2014-08-27 06:52:09 +02:00
|
|
|
//
|
|
|
|
// This type not only determines what type is expected/valid in configuring
|
|
|
|
// this value, but also what type is returned when ResourceData.Get is
|
|
|
|
// called. The types returned by Get are:
|
|
|
|
//
|
|
|
|
// TypeBool - bool
|
|
|
|
// TypeInt - int
|
2015-01-28 18:53:34 +01:00
|
|
|
// TypeFloat - float64
|
2014-08-27 06:52:09 +02:00
|
|
|
// TypeString - string
|
|
|
|
// TypeList - []interface{}
|
|
|
|
// TypeMap - map[string]interface{}
|
|
|
|
// TypeSet - *schema.Set
|
|
|
|
//
|
2014-08-15 04:55:47 +02:00
|
|
|
Type ValueType
|
2014-08-13 23:23:22 +02:00
|
|
|
|
|
|
|
// If one of these is set, then this item can come from the configuration.
|
|
|
|
// Both cannot be set. If Optional is set, the value is optional. If
|
|
|
|
// Required is set, the value is required.
|
2014-08-25 01:53:27 +02:00
|
|
|
//
|
|
|
|
// One of these must be set if the value is not computed. That is:
|
|
|
|
// value either comes from the config, is computed, or is both.
|
2014-08-13 23:23:22 +02:00
|
|
|
Optional bool
|
|
|
|
Required bool
|
|
|
|
|
2016-08-31 22:26:57 +02:00
|
|
|
// If this is non-nil, the provided function will be used during diff
|
|
|
|
// of this field. If this is nil, a default diff for the type of the
|
|
|
|
// schema will be used.
|
|
|
|
//
|
|
|
|
// This allows comparison based on something other than primitive, list
|
|
|
|
// or map equality - for example SSH public keys may be considered
|
|
|
|
// equivalent regardless of trailing whitespace.
|
|
|
|
DiffSuppressFunc SchemaDiffSuppressFunc
|
|
|
|
|
2014-09-10 06:17:29 +02:00
|
|
|
// If this is non-nil, then this will be a default value that is used
|
|
|
|
// when this item is not set in the configuration/state.
|
2014-09-10 06:33:08 +02:00
|
|
|
//
|
|
|
|
// DefaultFunc can be specified if you want a dynamic default value.
|
|
|
|
// Only one of Default or DefaultFunc can be set.
|
|
|
|
//
|
|
|
|
// If Required is true above, then Default cannot be set. DefaultFunc
|
|
|
|
// can be set with Required. If the DefaultFunc returns nil, then there
|
2015-02-11 23:49:50 +01:00
|
|
|
// will be no default and the user will be asked to fill it in.
|
2014-10-13 02:37:52 +02:00
|
|
|
//
|
|
|
|
// If either of these is set, then the user won't be asked for input
|
|
|
|
// for this key if the default is not nil.
|
2014-09-10 06:33:08 +02:00
|
|
|
Default interface{}
|
|
|
|
DefaultFunc SchemaDefaultFunc
|
2014-09-10 06:17:29 +02:00
|
|
|
|
2014-09-29 22:30:28 +02:00
|
|
|
// Description is used as the description for docs or asking for user
|
|
|
|
// input. It should be relatively short (a few sentences max) and should
|
|
|
|
// be formatted to fit a CLI.
|
|
|
|
Description string
|
|
|
|
|
2014-09-29 23:00:35 +02:00
|
|
|
// InputDefault is the default value to use for when inputs are requested.
|
2014-10-13 02:37:52 +02:00
|
|
|
// This differs from Default in that if Default is set, no input is
|
|
|
|
// asked for. If Input is asked, this will be the default value offered.
|
2014-09-29 23:00:35 +02:00
|
|
|
InputDefault string
|
|
|
|
|
2014-08-16 18:49:22 +02:00
|
|
|
// The fields below relate to diffs.
|
|
|
|
//
|
|
|
|
// If Computed is true, then the result of this value is computed
|
|
|
|
// (unless specified by config) on creation.
|
|
|
|
//
|
|
|
|
// If ForceNew is true, then a change in this resource necessitates
|
|
|
|
// the creation of a new resource.
|
2014-08-22 17:45:54 +02:00
|
|
|
//
|
|
|
|
// StateFunc is a function called to change the value of this before
|
|
|
|
// storing it in the state (and likewise before comparing for diffs).
|
|
|
|
// The use for this is for example with large strings, you may want
|
|
|
|
// to simply store the hash of it.
|
|
|
|
Computed bool
|
|
|
|
ForceNew bool
|
|
|
|
StateFunc SchemaStateFunc
|
2014-08-13 23:23:22 +02:00
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
// The following fields are only set for a TypeList or TypeSet Type.
|
2014-08-16 01:32:43 +02:00
|
|
|
//
|
2014-08-13 23:23:22 +02:00
|
|
|
// Elem must be either a *Schema or a *Resource only if the Type is
|
|
|
|
// TypeList, and represents what the element type is. If it is *Schema,
|
|
|
|
// the element type is just a simple value. If it is *Resource, the
|
|
|
|
// element type is a complex structure, potentially with its own lifecycle.
|
2016-02-18 02:20:36 +01:00
|
|
|
//
|
|
|
|
// MaxItems defines a maximum amount of items that can exist within a
|
|
|
|
// TypeSet or TypeList. Specific use cases would be if a TypeSet is being
|
|
|
|
// used to wrap a complex structure, however more than one instance would
|
|
|
|
// cause instability.
|
2016-10-04 19:37:23 +02:00
|
|
|
//
|
|
|
|
// MinItems defines a minimum amount of items that can exist within a
|
|
|
|
// TypeSet or TypeList. Specific use cases would be if a TypeSet is being
|
|
|
|
// used to wrap a complex structure, however less than one instance would
|
|
|
|
// cause instability.
|
2016-02-18 02:20:36 +01:00
|
|
|
Elem interface{}
|
|
|
|
MaxItems int
|
2016-10-04 19:37:23 +02:00
|
|
|
MinItems int
|
2014-08-21 02:51:27 +02:00
|
|
|
|
2015-01-12 13:57:47 +01:00
|
|
|
// The following fields are only valid for a TypeSet type.
|
2014-08-21 00:45:34 +02:00
|
|
|
//
|
2014-08-21 02:51:27 +02:00
|
|
|
// Set defines a function to determine the unique ID of an item so that
|
|
|
|
// a proper set can be built.
|
|
|
|
Set SchemaSetFunc
|
2014-08-16 18:49:22 +02:00
|
|
|
|
|
|
|
// ComputedWhen is a set of queries on the configuration. Whenever any
|
|
|
|
// of these things is changed, it will require a recompute (this requires
|
|
|
|
// that Computed is set to true).
|
2014-08-27 06:52:09 +02:00
|
|
|
//
|
|
|
|
// NOTE: This currently does not work.
|
2014-08-16 18:49:22 +02:00
|
|
|
ComputedWhen []string
|
2015-03-05 22:16:50 +01:00
|
|
|
|
2016-11-03 06:23:11 +01:00
|
|
|
// ConflictsWith is a set of schema keys that conflict with this schema.
|
|
|
|
// This will only check that they're set in the _config_. This will not
|
|
|
|
// raise an error for a malfunctioning resource that sets a conflicting
|
|
|
|
// key.
|
2015-04-19 12:54:45 +02:00
|
|
|
ConflictsWith []string
|
|
|
|
|
2015-03-05 22:33:56 +01:00
|
|
|
// When Deprecated is set, this attribute is deprecated.
|
2015-03-05 22:16:50 +01:00
|
|
|
//
|
|
|
|
// A deprecated field still works, but will probably stop working in near
|
|
|
|
// future. This string is the message shown to the user with instructions on
|
|
|
|
// how to address the deprecation.
|
|
|
|
Deprecated string
|
2015-03-05 22:33:56 +01:00
|
|
|
|
|
|
|
// When Removed is set, this attribute has been removed from the schema
|
|
|
|
//
|
|
|
|
// Removed attributes can be left in the Schema to generate informative error
|
|
|
|
// messages for the user when they show up in resource configurations.
|
|
|
|
// This string is the message shown to the user with instructions on
|
|
|
|
// what do to about the removed attribute.
|
|
|
|
Removed string
|
2015-06-08 03:49:15 +02:00
|
|
|
|
2015-06-08 15:51:04 +02:00
|
|
|
// ValidateFunc allows individual fields to define arbitrary validation
|
|
|
|
// logic. It is yielded the provided config value as an interface{} that is
|
|
|
|
// guaranteed to be of the proper Schema type, and it can yield warnings or
|
|
|
|
// errors based on inspection of that value.
|
2015-06-11 14:06:30 +02:00
|
|
|
//
|
|
|
|
// ValidateFunc currently only works for primitive types.
|
2015-06-08 03:49:15 +02:00
|
|
|
ValidateFunc SchemaValidateFunc
|
2016-05-28 02:00:59 +02:00
|
|
|
|
|
|
|
// Sensitive ensures that the attribute's value does not get displayed in
|
|
|
|
// logs or regular output. It should be used for passwords or other
|
|
|
|
// secret fields. Futrure versions of Terraform may encrypt these
|
|
|
|
// values.
|
|
|
|
Sensitive bool
|
2014-08-13 23:23:22 +02:00
|
|
|
}
|
2014-08-15 04:55:47 +02:00
|
|
|
|
2016-08-31 22:26:57 +02:00
|
|
|
// SchemaDiffSuppresFunc is a function which can be used to determine
|
|
|
|
// whether a detected diff on a schema element is "valid" or not, and
|
|
|
|
// suppress it from the plan if necessary.
|
|
|
|
//
|
|
|
|
// Return true if the diff should be suppressed, false to retain it.
|
|
|
|
type SchemaDiffSuppressFunc func(k, old, new string, d *ResourceData) bool
|
|
|
|
|
2014-09-10 06:33:08 +02:00
|
|
|
// SchemaDefaultFunc is a function called to return a default value for
|
|
|
|
// a field.
|
|
|
|
type SchemaDefaultFunc func() (interface{}, error)
|
|
|
|
|
2015-01-16 18:22:09 +01:00
|
|
|
// EnvDefaultFunc is a helper function that returns the value of the
|
|
|
|
// given environment variable, if one exists, or the default value
|
|
|
|
// otherwise.
|
|
|
|
func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
if v := os.Getenv(k); v != "" {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return dv, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 21:30:27 +01:00
|
|
|
// MultiEnvDefaultFunc is a helper function that returns the value of the first
|
|
|
|
// environment variable in the given list that returns a non-empty value. If
|
|
|
|
// none of the environment variables return a value, the default value is
|
|
|
|
// returned.
|
|
|
|
func MultiEnvDefaultFunc(ks []string, dv interface{}) SchemaDefaultFunc {
|
|
|
|
return func() (interface{}, error) {
|
|
|
|
for _, k := range ks {
|
|
|
|
if v := os.Getenv(k); v != "" {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dv, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 02:51:27 +02:00
|
|
|
// SchemaSetFunc is a function that must return a unique ID for the given
|
|
|
|
// element. This unique ID is used to store the element in a hash.
|
2014-08-22 17:45:54 +02:00
|
|
|
type SchemaSetFunc func(interface{}) int
|
|
|
|
|
|
|
|
// SchemaStateFunc is a function used to convert some type to a string
|
|
|
|
// to be stored in the state.
|
|
|
|
type SchemaStateFunc func(interface{}) string
|
2014-08-21 00:45:34 +02:00
|
|
|
|
2015-06-08 03:49:15 +02:00
|
|
|
// SchemaValidateFunc is a function used to validate a single field in the
|
|
|
|
// schema.
|
2015-06-24 18:29:24 +02:00
|
|
|
type SchemaValidateFunc func(interface{}, string) ([]string, []error)
|
2015-06-08 03:49:15 +02:00
|
|
|
|
2014-08-25 01:52:51 +02:00
|
|
|
func (s *Schema) GoString() string {
|
|
|
|
return fmt.Sprintf("*%#v", *s)
|
|
|
|
}
|
|
|
|
|
2015-01-27 21:23:49 +01:00
|
|
|
// Returns a default value for this schema by either reading Default or
|
|
|
|
// evaluating DefaultFunc. If neither of these are defined, returns nil.
|
|
|
|
func (s *Schema) DefaultValue() (interface{}, error) {
|
|
|
|
if s.Default != nil {
|
|
|
|
return s.Default, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.DefaultFunc != nil {
|
|
|
|
defaultValue, err := s.DefaultFunc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error loading default: %s", err)
|
|
|
|
}
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-08-18 04:26:58 +02:00
|
|
|
// Returns a zero value for the schema.
|
|
|
|
func (s *Schema) ZeroValue() interface{} {
|
|
|
|
// If it's a set then we'll do a bit of extra work to provide the
|
|
|
|
// right hashing function in our empty value.
|
|
|
|
if s.Type == TypeSet {
|
|
|
|
setFunc := s.Set
|
|
|
|
if setFunc == nil {
|
|
|
|
// Default set function uses the schema to hash the whole value
|
|
|
|
elem := s.Elem
|
|
|
|
switch t := elem.(type) {
|
|
|
|
case *Schema:
|
|
|
|
setFunc = HashSchema(t)
|
|
|
|
case *Resource:
|
|
|
|
setFunc = HashResource(t)
|
|
|
|
default:
|
|
|
|
panic("invalid set element type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Set{F: setFunc}
|
|
|
|
} else {
|
|
|
|
return s.Type.Zero()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
func (s *Schema) finalizeDiff(
|
|
|
|
d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff {
|
|
|
|
if d == nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
helper/schema: Normalize bools to "true"/"false" in diffs
For a long time now, the diff logic has relied on the behavior of
`mapstructure.WeakDecode` to determine how various primitives are
converted into strings. The `schema.DiffString` function is used for
all primitive field types: TypeBool, TypeInt, TypeFloat, and TypeString.
The `mapstructure` library's string representation of booleans is "0"
and "1", which differs from `strconv.FormatBool`'s "false" and "true"
(which is used in writing out boolean fields to the state).
Because of this difference, diffs have long had the potential for
cosmetically odd but semantically neutral output like:
"true" => "1"
"false" => "0"
So long as `mapstructure.Decode` or `strconv.ParseBool` are used to
interpret these strings, there's no functional problem.
We had our first clear functional problem with #6005 and friends, where
users noticed diffs like the above showing up unexpectedly and causing
troubles when `ignore_changes` was in play.
This particular bug occurs down in Terraform core's EvalIgnoreChanges.
There, the diff is modified to account for ignored attributes, and
special logic attempts to handle properly the situation where the
ignored attribute was going to trigger a resource replacement. That
logic relies on the string representations of the Old and New fields in
the diff to be the same so that it filters properly.
So therefore, we now get a bug when a diff includes `Old: "0", New:
"false"` since the strings do not match, and `ignore_changes` is not
properly handled.
Here, we introduce `TypeBool`-specific normalizing into `finalizeDiff`.
I spiked out a full `diffBool` function, but figuring out which pieces
of `diffString` to duplicate there got hairy. This seemed like a simpler
and more direct solution.
Fixes #6005 (and potentially others!)
2016-05-05 16:00:58 +02:00
|
|
|
if s.Type == TypeBool {
|
|
|
|
normalizeBoolString := func(s string) string {
|
|
|
|
switch s {
|
|
|
|
case "0":
|
|
|
|
return "false"
|
|
|
|
case "1":
|
|
|
|
return "true"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
d.Old = normalizeBoolString(d.Old)
|
|
|
|
d.New = normalizeBoolString(d.New)
|
|
|
|
}
|
|
|
|
|
2016-10-26 02:56:45 +02:00
|
|
|
if s.Computed && !d.NewRemoved && d.New == "" {
|
|
|
|
// Computed attribute without a new value set
|
|
|
|
d.NewComputed = true
|
|
|
|
}
|
|
|
|
|
2016-10-28 21:35:26 +02:00
|
|
|
if s.ForceNew {
|
2016-10-26 02:56:45 +02:00
|
|
|
// ForceNew, mark that this field is requiring new under the
|
|
|
|
// following conditions, explained below:
|
|
|
|
//
|
|
|
|
// * Old != New - There is a change in value. This field
|
|
|
|
// is therefore causing a new resource.
|
|
|
|
//
|
|
|
|
// * NewComputed - This field is being computed, hence a
|
|
|
|
// potential change in value, mark as causing a new resource.
|
|
|
|
d.RequiresNew = d.Old != d.New || d.NewComputed
|
2016-10-28 21:35:26 +02:00
|
|
|
}
|
|
|
|
|
2014-10-09 03:21:21 +02:00
|
|
|
if d.NewRemoved {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:49:22 +02:00
|
|
|
if s.Computed {
|
|
|
|
if d.Old != "" && d.New == "" {
|
|
|
|
// This is a computed value with an old value set already,
|
|
|
|
// just let it go.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.New == "" {
|
|
|
|
// Computed attribute without a new value set
|
|
|
|
d.NewComputed = true
|
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
|
|
|
|
2016-05-28 02:00:59 +02:00
|
|
|
if s.Sensitive {
|
|
|
|
// Set the Sensitive flag so output is hidden in the UI
|
|
|
|
d.Sensitive = true
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2014-08-15 04:55:47 +02:00
|
|
|
// schemaMap is a wrapper that adds nice functions on top of schemas.
|
|
|
|
type schemaMap map[string]*Schema
|
|
|
|
|
|
|
|
// Data returns a ResourceData for the given schema, state, and diff.
|
|
|
|
//
|
|
|
|
// The diff is optional.
|
|
|
|
func (m schemaMap) Data(
|
2014-09-17 02:07:13 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff) (*ResourceData, error) {
|
2014-08-16 01:32:43 +02:00
|
|
|
return &ResourceData{
|
|
|
|
schema: m,
|
|
|
|
state: s,
|
|
|
|
diff: d,
|
|
|
|
}, nil
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Diff returns the diff for a resource given the schema map,
|
|
|
|
// state, and configuration.
|
|
|
|
func (m schemaMap) 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) {
|
|
|
|
result := new(terraform.InstanceDiff)
|
2014-08-15 04:55:47 +02:00
|
|
|
result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
|
|
|
|
|
2016-04-21 21:55:29 +02:00
|
|
|
// Make sure to mark if the resource is tainted
|
|
|
|
if s != nil {
|
|
|
|
result.DestroyTainted = s.Tainted
|
|
|
|
}
|
|
|
|
|
2014-08-21 00:45:34 +02:00
|
|
|
d := &ResourceData{
|
2015-01-10 21:50:53 +01:00
|
|
|
schema: m,
|
|
|
|
state: s,
|
|
|
|
config: c,
|
2014-08-21 00:45:34 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 04:55:47 +02:00
|
|
|
for k, schema := range m {
|
2014-10-21 19:49:27 +02:00
|
|
|
err := m.diff(k, schema, result, d, false)
|
2014-08-15 04:55:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 00:26:15 +02:00
|
|
|
// If the diff requires a new resource, then we recompute the diff
|
|
|
|
// so we have the complete new resource diff, and preserve the
|
|
|
|
// RequiresNew fields where necessary so the user knows exactly what
|
|
|
|
// caused that.
|
|
|
|
if result.RequiresNew() {
|
|
|
|
// Create the new diff
|
2014-09-18 01:33:24 +02:00
|
|
|
result2 := new(terraform.InstanceDiff)
|
2014-08-28 00:26:15 +02:00
|
|
|
result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)
|
|
|
|
|
2016-04-21 21:55:29 +02:00
|
|
|
// Preserve the DestroyTainted flag
|
|
|
|
result2.DestroyTainted = result.DestroyTainted
|
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
// Reset the data to not contain state. We have to call init()
|
|
|
|
// again in order to reset the FieldReaders.
|
2014-08-28 00:26:15 +02:00
|
|
|
d.state = nil
|
2015-01-09 03:02:19 +01:00
|
|
|
d.init()
|
2014-08-28 00:26:15 +02:00
|
|
|
|
|
|
|
// Perform the diff again
|
|
|
|
for k, schema := range m {
|
2014-10-21 19:49:27 +02:00
|
|
|
err := m.diff(k, schema, result2, d, false)
|
2014-08-28 00:26:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force all the fields to not force a new since we know what we
|
|
|
|
// want to force new.
|
|
|
|
for k, attr := range result2.Attributes {
|
|
|
|
if attr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr.RequiresNew {
|
|
|
|
attr.RequiresNew = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s != nil {
|
|
|
|
attr.Old = s.Attributes[k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now copy in all the requires new diffs...
|
|
|
|
for k, attr := range result.Attributes {
|
|
|
|
if attr == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newAttr, ok := result2.Attributes[k]
|
|
|
|
if !ok {
|
|
|
|
newAttr = attr
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr.RequiresNew {
|
|
|
|
newAttr.RequiresNew = true
|
|
|
|
}
|
|
|
|
|
|
|
|
result2.Attributes[k] = newAttr
|
|
|
|
}
|
|
|
|
|
|
|
|
// And set the diff!
|
|
|
|
result = result2
|
|
|
|
}
|
|
|
|
|
2014-08-16 22:32:21 +02:00
|
|
|
// Remove any nil diffs just to keep things clean
|
2014-08-16 18:49:22 +02:00
|
|
|
for k, v := range result.Attributes {
|
|
|
|
if v == nil {
|
|
|
|
delete(result.Attributes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 22:32:21 +02:00
|
|
|
// Go through and detect all of the ComputedWhens now that we've
|
|
|
|
// finished the diff.
|
|
|
|
// TODO
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
if result.Empty() {
|
|
|
|
// If we don't have any diff elements, just return nil
|
|
|
|
return nil, nil
|
|
|
|
}
|
2014-08-15 04:55:47 +02:00
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:25:43 +02:00
|
|
|
// Input implements the terraform.ResourceProvider method by asking
|
|
|
|
// for input for required configuration keys that don't have a value.
|
|
|
|
func (m schemaMap) Input(
|
|
|
|
input terraform.UIInput,
|
|
|
|
c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
|
2014-09-29 23:00:35 +02:00
|
|
|
keys := make([]string, 0, len(m))
|
|
|
|
for k, _ := range m {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, k := range keys {
|
|
|
|
v := m[k]
|
|
|
|
|
2014-09-29 19:25:43 +02:00
|
|
|
// Skip things that don't require config, if that is even valid
|
|
|
|
// for a provider schema.
|
|
|
|
if !v.Required && !v.Optional {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-07 18:27:04 +01:00
|
|
|
// Deprecated fields should never prompt
|
|
|
|
if v.Deprecated != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-29 20:16:19 +02:00
|
|
|
// Skip things that have a value of some sort already
|
|
|
|
if _, ok := c.Raw[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-27 21:23:49 +01:00
|
|
|
// Skip if it has a default value
|
|
|
|
defaultValue, err := v.DefaultValue()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: error loading default: %s", k, err)
|
2014-10-13 02:37:52 +02:00
|
|
|
}
|
2015-01-27 21:23:49 +01:00
|
|
|
if defaultValue != nil {
|
|
|
|
continue
|
2014-10-13 02:37:52 +02:00
|
|
|
}
|
|
|
|
|
2014-09-29 19:25:43 +02:00
|
|
|
var value interface{}
|
|
|
|
switch v.Type {
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeBool, TypeInt, TypeFloat, TypeSet:
|
2015-04-20 00:54:42 +02:00
|
|
|
continue
|
2014-09-29 19:25:43 +02:00
|
|
|
case TypeString:
|
|
|
|
value, err = m.inputString(input, k, v)
|
|
|
|
default:
|
2014-11-02 13:56:44 +01:00
|
|
|
panic(fmt.Sprintf("Unknown type for input: %#v", v.Type))
|
2014-09-29 19:25:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"%s: %s", k, err)
|
|
|
|
}
|
|
|
|
|
2014-10-18 23:54:42 +02:00
|
|
|
c.Config[k] = value
|
2014-09-29 19:25:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
// Validate validates the configuration against this schema mapping.
|
|
|
|
func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
2014-08-16 07:15:10 +02:00
|
|
|
return m.validateObject("", m, c)
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 23:50:44 +02:00
|
|
|
// InternalValidate validates the format of this schema. This should be called
|
|
|
|
// from a unit test (and not in user-path code) to verify that a schema
|
|
|
|
// is properly built.
|
2015-05-12 16:45:15 +02:00
|
|
|
func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
|
|
|
|
if topSchemaMap == nil {
|
|
|
|
topSchemaMap = m
|
|
|
|
}
|
2014-08-17 23:50:44 +02:00
|
|
|
for k, v := range m {
|
|
|
|
if v.Type == TypeInvalid {
|
|
|
|
return fmt.Errorf("%s: Type must be specified", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Optional && v.Required {
|
|
|
|
return fmt.Errorf("%s: Optional or Required must be set, not both", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Required && v.Computed {
|
|
|
|
return fmt.Errorf("%s: Cannot be both Required and Computed", k)
|
|
|
|
}
|
|
|
|
|
2014-08-25 01:52:51 +02:00
|
|
|
if !v.Required && !v.Optional && !v.Computed {
|
|
|
|
return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
|
|
|
|
}
|
|
|
|
|
2014-09-10 06:17:29 +02:00
|
|
|
if v.Computed && v.Default != nil {
|
|
|
|
return fmt.Errorf("%s: Default must be nil if computed", k)
|
|
|
|
}
|
|
|
|
|
2014-09-10 06:33:08 +02:00
|
|
|
if v.Required && v.Default != nil {
|
|
|
|
return fmt.Errorf("%s: Default cannot be set with Required", k)
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:50:44 +02:00
|
|
|
if len(v.ComputedWhen) > 0 && !v.Computed {
|
|
|
|
return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
|
|
|
|
}
|
|
|
|
|
2015-04-19 12:54:45 +02:00
|
|
|
if len(v.ConflictsWith) > 0 && v.Required {
|
|
|
|
return fmt.Errorf("%s: ConflictsWith cannot be set with Required", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(v.ConflictsWith) > 0 {
|
|
|
|
for _, key := range v.ConflictsWith {
|
2015-05-12 16:45:15 +02:00
|
|
|
parts := strings.Split(key, ".")
|
|
|
|
sm := topSchemaMap
|
|
|
|
var target *Schema
|
|
|
|
for _, part := range parts {
|
|
|
|
// Skip index fields
|
|
|
|
if _, err := strconv.Atoi(part); err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
if target, ok = sm[part]; !ok {
|
|
|
|
return fmt.Errorf("%s: ConflictsWith references unknown attribute (%s)", k, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subResource, ok := target.Elem.(*Resource); ok {
|
|
|
|
sm = schemaMap(subResource.Schema)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if target == nil {
|
|
|
|
return fmt.Errorf("%s: ConflictsWith cannot find target attribute (%s), sm: %#v", k, key, sm)
|
|
|
|
}
|
|
|
|
if target.Required {
|
2015-04-19 12:54:45 +02:00
|
|
|
return fmt.Errorf("%s: ConflictsWith cannot contain Required attribute (%s)", k, key)
|
|
|
|
}
|
|
|
|
|
2016-11-03 06:23:11 +01:00
|
|
|
if len(target.ComputedWhen) > 0 {
|
2015-04-19 12:54:45 +02:00
|
|
|
return fmt.Errorf("%s: ConflictsWith cannot contain Computed(When) attribute (%s)", k, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-21 06:13:18 +02:00
|
|
|
if v.Type == TypeList || v.Type == TypeSet {
|
2014-08-17 23:50:44 +02:00
|
|
|
if v.Elem == nil {
|
|
|
|
return fmt.Errorf("%s: Elem must be set for lists", k)
|
|
|
|
}
|
|
|
|
|
2014-09-10 06:17:29 +02:00
|
|
|
if v.Default != nil {
|
|
|
|
return fmt.Errorf("%s: Default is not valid for lists or sets", k)
|
|
|
|
}
|
|
|
|
|
2015-08-18 04:26:58 +02:00
|
|
|
if v.Type != TypeSet && v.Set != nil {
|
2014-08-21 02:51:27 +02:00
|
|
|
return fmt.Errorf("%s: Set can only be set for TypeSet", k)
|
|
|
|
}
|
|
|
|
|
2014-08-17 23:50:44 +02:00
|
|
|
switch t := v.Elem.(type) {
|
|
|
|
case *Resource:
|
2016-04-17 02:27:00 +02:00
|
|
|
if err := t.InternalValidate(topSchemaMap, true); err != nil {
|
2014-08-17 23:50:44 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case *Schema:
|
|
|
|
bad := t.Computed || t.Optional || t.Required
|
|
|
|
if bad {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s: Elem must have only Type set", k)
|
|
|
|
}
|
|
|
|
}
|
2016-02-18 02:20:36 +01:00
|
|
|
} else {
|
2016-10-04 19:37:23 +02:00
|
|
|
if v.MaxItems > 0 || v.MinItems > 0 {
|
|
|
|
return fmt.Errorf("%s: MaxItems and MinItems are only supported on lists or sets", k)
|
2016-02-18 02:20:36 +01:00
|
|
|
}
|
2014-08-17 23:50:44 +02:00
|
|
|
}
|
2015-06-11 14:06:30 +02:00
|
|
|
|
|
|
|
if v.ValidateFunc != nil {
|
|
|
|
switch v.Type {
|
2015-10-14 20:44:28 +02:00
|
|
|
case TypeList, TypeSet:
|
|
|
|
return fmt.Errorf("ValidateFunc is not yet supported on lists or sets.")
|
2015-06-11 14:06:30 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-17 23:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
func (m schemaMap) diff(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *terraform.InstanceDiff,
|
2014-10-21 19:49:27 +02:00
|
|
|
d *ResourceData,
|
|
|
|
all bool) error {
|
2016-08-31 22:26:57 +02:00
|
|
|
|
|
|
|
unsupressedDiff := new(terraform.InstanceDiff)
|
|
|
|
unsupressedDiff.Attributes = make(map[string]*terraform.ResourceAttrDiff)
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
var err error
|
|
|
|
switch schema.Type {
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeBool, TypeInt, TypeFloat, TypeString:
|
2016-08-31 22:26:57 +02:00
|
|
|
err = m.diffString(k, schema, unsupressedDiff, d, all)
|
2014-08-15 08:17:53 +02:00
|
|
|
case TypeList:
|
2016-08-31 22:26:57 +02:00
|
|
|
err = m.diffList(k, schema, unsupressedDiff, d, all)
|
2014-08-19 00:07:09 +02:00
|
|
|
case TypeMap:
|
2016-08-31 22:26:57 +02:00
|
|
|
err = m.diffMap(k, schema, unsupressedDiff, d, all)
|
2014-08-21 03:30:28 +02:00
|
|
|
case TypeSet:
|
2016-08-31 22:26:57 +02:00
|
|
|
err = m.diffSet(k, schema, unsupressedDiff, d, all)
|
2014-08-15 08:17:53 +02:00
|
|
|
default:
|
2014-08-25 06:50:35 +02:00
|
|
|
err = fmt.Errorf("%s: unknown type %#v", k, schema.Type)
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:26:57 +02:00
|
|
|
for attrK, attrV := range unsupressedDiff.Attributes {
|
2016-09-14 10:48:42 +02:00
|
|
|
if schema.DiffSuppressFunc != nil &&
|
|
|
|
attrV != nil &&
|
|
|
|
schema.DiffSuppressFunc(attrK, attrV.Old, attrV.New, d) {
|
2016-08-31 22:26:57 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
diff.Attributes[attrK] = attrV
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m schemaMap) diffList(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *terraform.InstanceDiff,
|
2014-10-21 19:49:27 +02:00
|
|
|
d *ResourceData,
|
|
|
|
all bool) error {
|
2014-10-10 04:09:06 +02:00
|
|
|
o, n, _, computedList := d.diffChange(k)
|
2015-01-09 03:02:19 +01:00
|
|
|
if computedList {
|
|
|
|
n = nil
|
|
|
|
}
|
2014-10-11 19:40:54 +02:00
|
|
|
nSet := n != nil
|
2014-08-28 00:03:42 +02:00
|
|
|
|
2014-12-12 15:24:29 +01:00
|
|
|
// If we have an old value and no new value is set or will be
|
|
|
|
// computed once all variables can be interpolated and we're
|
|
|
|
// computed, then nothing has changed.
|
|
|
|
if o != nil && n == nil && !computedList && schema.Computed {
|
2014-08-28 00:03:42 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-21 07:24:13 +02:00
|
|
|
if o == nil {
|
|
|
|
o = []interface{}{}
|
|
|
|
}
|
|
|
|
if n == nil {
|
|
|
|
n = []interface{}{}
|
|
|
|
}
|
2014-08-21 06:02:42 +02:00
|
|
|
if s, ok := o.(*Set); ok {
|
|
|
|
o = s.List()
|
|
|
|
}
|
|
|
|
if s, ok := n.(*Set); ok {
|
|
|
|
n = s.List()
|
|
|
|
}
|
2014-08-21 00:45:34 +02:00
|
|
|
os := o.([]interface{})
|
|
|
|
vs := n.([]interface{})
|
2014-08-15 19:39:40 +02:00
|
|
|
|
2014-10-11 19:40:54 +02:00
|
|
|
// If the new value was set, and the two are equal, then we're done.
|
|
|
|
// We have to do this check here because sets might be NOT
|
|
|
|
// reflect.DeepEqual so we need to wait until we get the []interface{}
|
2014-10-21 19:49:27 +02:00
|
|
|
if !all && nSet && reflect.DeepEqual(os, vs) {
|
2014-10-11 19:40:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-19 01:54:30 +02:00
|
|
|
// Get the counts
|
2014-08-21 00:45:34 +02:00
|
|
|
oldLen := len(os)
|
|
|
|
newLen := len(vs)
|
2014-10-10 04:09:06 +02:00
|
|
|
oldStr := strconv.FormatInt(int64(oldLen), 10)
|
|
|
|
|
|
|
|
// If the whole list is computed, then say that the # is computed
|
|
|
|
if computedList {
|
|
|
|
diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
|
|
|
|
Old: oldStr,
|
|
|
|
NewComputed: true,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-19 01:54:30 +02:00
|
|
|
|
|
|
|
// If the counts are not the same, then record that diff
|
2014-08-19 06:22:27 +02:00
|
|
|
changed := oldLen != newLen
|
|
|
|
computed := oldLen == 0 && newLen == 0 && schema.Computed
|
2014-10-21 19:49:27 +02:00
|
|
|
if changed || computed || all {
|
2014-08-19 01:54:30 +02:00
|
|
|
countSchema := &Schema{
|
|
|
|
Type: TypeInt,
|
2014-08-19 06:22:27 +02:00
|
|
|
Computed: schema.Computed,
|
2014-08-19 01:54:30 +02:00
|
|
|
ForceNew: schema.ForceNew,
|
|
|
|
}
|
|
|
|
|
2014-08-19 06:22:27 +02:00
|
|
|
newStr := ""
|
|
|
|
if !computed {
|
|
|
|
newStr = strconv.FormatInt(int64(newLen), 10)
|
2014-10-10 04:09:06 +02:00
|
|
|
} else {
|
|
|
|
oldStr = ""
|
2014-08-19 06:22:27 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 01:54:30 +02:00
|
|
|
diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
|
2014-08-19 06:22:27 +02:00
|
|
|
Old: oldStr,
|
|
|
|
New: newStr,
|
2014-08-19 01:54:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the maximum
|
|
|
|
maxLen := oldLen
|
|
|
|
if newLen > maxLen {
|
|
|
|
maxLen = newLen
|
2014-08-15 08:32:20 +02:00
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
|
|
|
|
switch t := schema.Elem.(type) {
|
2014-12-15 23:02:16 +01:00
|
|
|
case *Resource:
|
|
|
|
// This is a complex resource
|
|
|
|
for i := 0; i < maxLen; i++ {
|
|
|
|
for k2, schema := range t.Schema {
|
|
|
|
subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
|
|
|
|
err := m.diff(subK, schema, diff, d, all)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
case *Schema:
|
2014-08-15 08:33:30 +02:00
|
|
|
// Copy the schema so that we can set Computed/ForceNew from
|
|
|
|
// the parent schema (the TypeList).
|
2014-08-15 08:32:20 +02:00
|
|
|
t2 := *t
|
|
|
|
t2.ForceNew = schema.ForceNew
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
// This is just a primitive element, so go through each and
|
|
|
|
// just diff each.
|
2014-08-19 01:54:30 +02:00
|
|
|
for i := 0; i < maxLen; i++ {
|
2014-08-15 08:17:53 +02:00
|
|
|
subK := fmt.Sprintf("%s.%d", k, i)
|
2014-10-21 19:49:27 +02:00
|
|
|
err := m.diff(subK, &t2, diff, d, all)
|
2014-08-15 08:17:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s: unknown element type (internal)", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 00:07:09 +02:00
|
|
|
func (m schemaMap) diffMap(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *terraform.InstanceDiff,
|
2014-10-21 19:49:27 +02:00
|
|
|
d *ResourceData,
|
|
|
|
all bool) error {
|
2014-08-19 00:07:09 +02:00
|
|
|
prefix := k + "."
|
|
|
|
|
|
|
|
// First get all the values from the state
|
2014-08-21 00:45:34 +02:00
|
|
|
var stateMap, configMap map[string]string
|
2015-04-21 22:13:03 +02:00
|
|
|
o, n, _, nComputed := d.diffChange(k)
|
2014-08-21 00:45:34 +02:00
|
|
|
if err := mapstructure.WeakDecode(o, &stateMap); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
2014-08-19 00:07:09 +02:00
|
|
|
}
|
2014-08-21 00:45:34 +02:00
|
|
|
if err := mapstructure.WeakDecode(n, &configMap); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
2014-08-19 00:07:09 +02:00
|
|
|
}
|
|
|
|
|
2015-04-21 22:13:03 +02:00
|
|
|
// Keep track of whether the state _exists_ at all prior to clearing it
|
|
|
|
stateExists := o != nil
|
|
|
|
|
2014-12-16 18:05:16 +01:00
|
|
|
// Delete any count values, since we don't use those
|
core: Use .% instead of .# for maps in state
The flatmapped representation of state prior to this commit encoded maps
and lists (and therefore by extension, sets) with a key corresponding to
the number of elements, or the unknown variable indicator under a .# key
and then individual items. For example, the list ["a", "b", "c"] would
have been encoded as:
listname.# = 3
listname.0 = "a"
listname.1 = "b"
listname.2 = "c"
And the map {"key1": "value1", "key2", "value2"} would have been encoded
as:
mapname.# = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
Sets use the hash code as the key - for example a set with a (fictional)
hashcode calculation may look like:
setname.# = 2
setname.12312512 = "value1"
setname.56345233 = "value2"
Prior to the work done to extend the type system, this was sufficient
since the internal representation of these was effectively the same.
However, following the separation of maps and lists into distinct
first-class types, this encoding presents a problem: given a state file,
it is impossible to tell the encoding of an empty list and an empty map
apart. This presents problems for the type checker during interpolation,
as many interpolation functions will operate on only one of these two
structures.
This commit therefore changes the representation in state of maps to use
a "%" as the key for the number of elements. Consequently the map above
will now be encoded as:
mapname.% = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
This has the effect of an empty list (or set) now being encoded as:
listname.# = 0
And an empty map now being encoded as:
mapname.% = 0
Therefore we can eliminate some nasty guessing logic from the resource
variable supplier for interpolation, at the cost of having to migrate
state up front (to follow in a subsequent commit).
In order to reduce the number of potential situations in which resources
would be "forced new", we continue to accept "#" as the count key when
reading maps via helper/schema. There is no situation under which we can
allow "#" as an actual map key in any case, as it would not be
distinguishable from a list or set in state.
2016-06-05 10:34:43 +02:00
|
|
|
delete(configMap, "%")
|
|
|
|
delete(stateMap, "%")
|
2014-12-16 18:05:16 +01:00
|
|
|
|
2015-04-21 22:13:03 +02:00
|
|
|
// Check if the number of elements has changed.
|
2014-12-16 02:39:07 +01:00
|
|
|
oldLen, newLen := len(stateMap), len(configMap)
|
2014-12-16 02:35:16 +01:00
|
|
|
changed := oldLen != newLen
|
|
|
|
if oldLen != 0 && newLen == 0 && schema.Computed {
|
|
|
|
changed = false
|
|
|
|
}
|
2015-04-21 22:13:03 +02:00
|
|
|
|
|
|
|
// It is computed if we have no old value, no new value, the schema
|
|
|
|
// says it is computed, and it didn't exist in the state before. The
|
|
|
|
// last point means: if it existed in the state, even empty, then it
|
|
|
|
// has already been computed.
|
|
|
|
computed := oldLen == 0 && newLen == 0 && schema.Computed && !stateExists
|
|
|
|
|
|
|
|
// If the count has changed or we're computed, then add a diff for the
|
|
|
|
// count. "nComputed" means that the new value _contains_ a value that
|
|
|
|
// is computed. We don't do granular diffs for this yet, so we mark the
|
|
|
|
// whole map as computed.
|
|
|
|
if changed || computed || nComputed {
|
2014-12-16 02:35:16 +01:00
|
|
|
countSchema := &Schema{
|
|
|
|
Type: TypeInt,
|
2015-04-21 22:13:03 +02:00
|
|
|
Computed: schema.Computed || nComputed,
|
2014-12-16 02:35:16 +01:00
|
|
|
ForceNew: schema.ForceNew,
|
|
|
|
}
|
|
|
|
|
2014-12-16 02:39:07 +01:00
|
|
|
oldStr := strconv.FormatInt(int64(oldLen), 10)
|
2014-12-16 02:35:16 +01:00
|
|
|
newStr := ""
|
2015-04-21 22:13:03 +02:00
|
|
|
if !computed && !nComputed {
|
2014-12-16 02:35:16 +01:00
|
|
|
newStr = strconv.FormatInt(int64(newLen), 10)
|
|
|
|
} else {
|
|
|
|
oldStr = ""
|
|
|
|
}
|
|
|
|
|
core: Use .% instead of .# for maps in state
The flatmapped representation of state prior to this commit encoded maps
and lists (and therefore by extension, sets) with a key corresponding to
the number of elements, or the unknown variable indicator under a .# key
and then individual items. For example, the list ["a", "b", "c"] would
have been encoded as:
listname.# = 3
listname.0 = "a"
listname.1 = "b"
listname.2 = "c"
And the map {"key1": "value1", "key2", "value2"} would have been encoded
as:
mapname.# = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
Sets use the hash code as the key - for example a set with a (fictional)
hashcode calculation may look like:
setname.# = 2
setname.12312512 = "value1"
setname.56345233 = "value2"
Prior to the work done to extend the type system, this was sufficient
since the internal representation of these was effectively the same.
However, following the separation of maps and lists into distinct
first-class types, this encoding presents a problem: given a state file,
it is impossible to tell the encoding of an empty list and an empty map
apart. This presents problems for the type checker during interpolation,
as many interpolation functions will operate on only one of these two
structures.
This commit therefore changes the representation in state of maps to use
a "%" as the key for the number of elements. Consequently the map above
will now be encoded as:
mapname.% = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
This has the effect of an empty list (or set) now being encoded as:
listname.# = 0
And an empty map now being encoded as:
mapname.% = 0
Therefore we can eliminate some nasty guessing logic from the resource
variable supplier for interpolation, at the cost of having to migrate
state up front (to follow in a subsequent commit).
In order to reduce the number of potential situations in which resources
would be "forced new", we continue to accept "#" as the count key when
reading maps via helper/schema. There is no situation under which we can
allow "#" as an actual map key in any case, as it would not be
distinguishable from a list or set in state.
2016-06-05 10:34:43 +02:00
|
|
|
diff.Attributes[k+".%"] = countSchema.finalizeDiff(
|
2014-12-16 02:35:16 +01:00
|
|
|
&terraform.ResourceAttrDiff{
|
|
|
|
Old: oldStr,
|
|
|
|
New: newStr,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-10-20 23:23:06 +02:00
|
|
|
// If the new map is nil and we're computed, then ignore it.
|
|
|
|
if n == nil && schema.Computed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-19 00:07:09 +02:00
|
|
|
// Now we compare, preferring values from the config map
|
|
|
|
for k, v := range configMap {
|
2015-02-18 00:22:45 +01:00
|
|
|
old, ok := stateMap[k]
|
2014-08-19 00:07:09 +02:00
|
|
|
delete(stateMap, k)
|
|
|
|
|
2015-02-18 00:22:45 +01:00
|
|
|
if old == v && ok && !all {
|
2014-08-19 00:07:09 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
|
|
|
|
Old: old,
|
|
|
|
New: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for k, v := range stateMap {
|
|
|
|
diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
|
|
|
|
Old: v,
|
|
|
|
NewRemoved: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-21 03:30:28 +02:00
|
|
|
func (m schemaMap) diffSet(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *terraform.InstanceDiff,
|
2014-10-21 19:49:27 +02:00
|
|
|
d *ResourceData,
|
|
|
|
all bool) error {
|
2014-12-12 23:21:20 +01:00
|
|
|
o, n, _, computedSet := d.diffChange(k)
|
2015-01-09 03:02:19 +01:00
|
|
|
if computedSet {
|
|
|
|
n = nil
|
|
|
|
}
|
2014-12-12 23:21:20 +01:00
|
|
|
nSet := n != nil
|
|
|
|
|
|
|
|
// If we have an old value and no new value is set or will be
|
|
|
|
// computed once all variables can be interpolated and we're
|
|
|
|
// computed, then nothing has changed.
|
|
|
|
if o != nil && n == nil && !computedSet && schema.Computed {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if o == nil {
|
2015-08-18 04:26:58 +02:00
|
|
|
o = schema.ZeroValue().(*Set)
|
2014-12-12 23:21:20 +01:00
|
|
|
}
|
|
|
|
if n == nil {
|
2015-08-18 04:26:58 +02:00
|
|
|
n = schema.ZeroValue().(*Set)
|
2014-12-12 23:21:20 +01:00
|
|
|
}
|
|
|
|
os := o.(*Set)
|
|
|
|
ns := n.(*Set)
|
|
|
|
|
|
|
|
// If the new value was set, compare the listCode's to determine if
|
2015-09-11 20:56:20 +02:00
|
|
|
// the two are equal. Comparing listCode's instead of the actual values
|
2014-12-12 23:21:20 +01:00
|
|
|
// is needed because there could be computed values in the set which
|
|
|
|
// would result in false positives while comparing.
|
|
|
|
if !all && nSet && reflect.DeepEqual(os.listCode(), ns.listCode()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the counts
|
|
|
|
oldLen := os.Len()
|
|
|
|
newLen := ns.Len()
|
|
|
|
oldStr := strconv.Itoa(oldLen)
|
|
|
|
newStr := strconv.Itoa(newLen)
|
|
|
|
|
2016-11-15 20:02:14 +01:00
|
|
|
// Build a schema for our count
|
|
|
|
countSchema := &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
Computed: schema.Computed,
|
|
|
|
ForceNew: schema.ForceNew,
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
// If the set computed then say that the # is computed
|
2015-10-08 14:48:04 +02:00
|
|
|
if computedSet || schema.Computed && !nSet {
|
2014-12-12 23:21:20 +01:00
|
|
|
// If # already exists, equals 0 and no new set is supplied, there
|
|
|
|
// is nothing to record in the diff
|
|
|
|
count, ok := d.GetOk(k + ".#")
|
|
|
|
if ok && count.(int) == 0 && !nSet && !computedSet {
|
2014-10-21 19:49:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-12 23:21:20 +01:00
|
|
|
|
|
|
|
// Set the count but make sure that if # does not exist, we don't
|
|
|
|
// use the zeroed value
|
|
|
|
countStr := strconv.Itoa(count.(int))
|
|
|
|
if !ok {
|
|
|
|
countStr = ""
|
|
|
|
}
|
|
|
|
|
2016-11-15 20:02:14 +01:00
|
|
|
diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
|
2014-12-12 23:21:20 +01:00
|
|
|
Old: countStr,
|
|
|
|
NewComputed: true,
|
2016-11-15 20:02:14 +01:00
|
|
|
})
|
2014-12-12 23:21:20 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the counts are not the same, then record that diff
|
|
|
|
changed := oldLen != newLen
|
2015-02-18 21:54:08 +01:00
|
|
|
if changed || all {
|
2014-12-12 23:21:20 +01:00
|
|
|
diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
|
|
|
|
Old: oldStr,
|
|
|
|
New: newStr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-26 07:01:54 +02:00
|
|
|
// Build the list of codes that will make up our set. This is the
|
|
|
|
// removed codes as well as all the codes in the new codes.
|
2015-11-18 11:24:04 +01:00
|
|
|
codes := make([][]string, 2)
|
2015-06-26 07:01:54 +02:00
|
|
|
codes[0] = os.Difference(ns).listCode()
|
|
|
|
codes[1] = ns.listCode()
|
|
|
|
for _, list := range codes {
|
|
|
|
for _, code := range list {
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
// This is a complex resource
|
|
|
|
for k2, schema := range t.Schema {
|
2015-11-18 11:24:04 +01:00
|
|
|
subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
|
2015-06-26 07:01:54 +02:00
|
|
|
err := m.diff(subK, schema, diff, d, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *Schema:
|
|
|
|
// Copy the schema so that we can set Computed/ForceNew from
|
|
|
|
// the parent schema (the TypeSet).
|
|
|
|
t2 := *t
|
|
|
|
t2.ForceNew = schema.ForceNew
|
|
|
|
|
|
|
|
// This is just a primitive element, so go through each and
|
|
|
|
// just diff each.
|
2015-11-18 11:24:04 +01:00
|
|
|
subK := fmt.Sprintf("%s.%s", k, code)
|
2015-06-26 07:01:54 +02:00
|
|
|
err := m.diff(subK, &t2, diff, d, true)
|
2014-12-15 23:02:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-26 07:01:54 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s: unknown element type (internal)", k)
|
2014-12-15 23:02:16 +01:00
|
|
|
}
|
2014-12-12 23:21:20 +01:00
|
|
|
}
|
2014-10-21 19:49:27 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:21:20 +01:00
|
|
|
return nil
|
2014-08-21 03:30:28 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 04:55:47 +02:00
|
|
|
func (m schemaMap) diffString(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
2014-09-18 01:33:24 +02:00
|
|
|
diff *terraform.InstanceDiff,
|
2014-10-21 19:49:27 +02:00
|
|
|
d *ResourceData,
|
|
|
|
all bool) error {
|
2014-08-22 17:57:44 +02:00
|
|
|
var originalN interface{}
|
2014-08-21 00:45:34 +02:00
|
|
|
var os, ns string
|
helper/schema,terraform: handle computed primtives in diffs
Fixes #3309
There are two primary changes, one to how helper/schema creates diffs
and one to how Terraform compares diffs. Both require careful
understanding.
== 1. helper/schema Changes
helper/schema, given any primitive field (string, int, bool, etc.)
_used to_ create a basic diff when given a computed new value (i.e. from
an unkown interpolation). This would put in the plan that the old value
is whatever the old value was, and the new value was the actual
interpolation. For example, from #3309, the diff showed the following:
```
~ module.test.aws_eip.test-instance.0
instance: "<INSTANCE ID>" => "${element(aws_instance.test-instance.*.id, count.index)}"
```
Then, when running `apply`, the diff would be realized and you would get
a diff mismatch error because it would realize the final value is the
same and remove it from the diff.
**The change:** `helper/schema` now marks unknown primitive values with
`NewComputed` set to true. Semantically this is correct for the diff to
have this information.
== 2. Terraform Diff.Same Changes
Next, the way Terraform compares diffs needed to be updated
Specifically, the case where the diff from the plan had a NewComputed
primitive and the diff from the apply _no longer has that value_. This
is possible if the computed value ended up being the same as the old
value. This is allowed to pass through.
Together, these fix #3309.
2016-10-26 04:36:59 +02:00
|
|
|
o, n, _, computed := d.diffChange(k)
|
2015-11-20 21:02:20 +01:00
|
|
|
if schema.StateFunc != nil && n != nil {
|
2014-08-22 17:57:44 +02:00
|
|
|
originalN = n
|
|
|
|
n = schema.StateFunc(n)
|
|
|
|
}
|
2015-02-17 22:16:59 +01:00
|
|
|
nraw := n
|
2015-02-18 21:54:08 +01:00
|
|
|
if nraw == nil && o != nil {
|
2015-02-17 22:16:59 +01:00
|
|
|
nraw = schema.Type.Zero()
|
2015-02-17 20:10:45 +01:00
|
|
|
}
|
2014-08-21 00:45:34 +02:00
|
|
|
if err := mapstructure.WeakDecode(o, &os); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
2015-02-17 22:16:59 +01:00
|
|
|
if err := mapstructure.WeakDecode(nraw, &ns); err != nil {
|
2014-08-15 08:17:53 +02:00
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
|
|
|
}
|
|
|
|
|
2014-10-21 19:49:27 +02:00
|
|
|
if os == ns && !all {
|
2014-08-22 21:18:08 +02:00
|
|
|
// They're the same value. If there old value is not blank or we
|
|
|
|
// have an ID, then return right away since we're already setup.
|
|
|
|
if os != "" || d.Id() != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, only continue if we're computed
|
helper/schema,terraform: handle computed primtives in diffs
Fixes #3309
There are two primary changes, one to how helper/schema creates diffs
and one to how Terraform compares diffs. Both require careful
understanding.
== 1. helper/schema Changes
helper/schema, given any primitive field (string, int, bool, etc.)
_used to_ create a basic diff when given a computed new value (i.e. from
an unkown interpolation). This would put in the plan that the old value
is whatever the old value was, and the new value was the actual
interpolation. For example, from #3309, the diff showed the following:
```
~ module.test.aws_eip.test-instance.0
instance: "<INSTANCE ID>" => "${element(aws_instance.test-instance.*.id, count.index)}"
```
Then, when running `apply`, the diff would be realized and you would get
a diff mismatch error because it would realize the final value is the
same and remove it from the diff.
**The change:** `helper/schema` now marks unknown primitive values with
`NewComputed` set to true. Semantically this is correct for the diff to
have this information.
== 2. Terraform Diff.Same Changes
Next, the way Terraform compares diffs needed to be updated
Specifically, the case where the diff from the plan had a NewComputed
primitive and the diff from the apply _no longer has that value_. This
is possible if the computed value ended up being the same as the old
value. This is allowed to pass through.
Together, these fix #3309.
2016-10-26 04:36:59 +02:00
|
|
|
if !schema.Computed && !computed {
|
2014-08-21 00:45:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
|
|
|
|
2014-08-21 06:02:42 +02:00
|
|
|
removed := false
|
|
|
|
if o != nil && n == nil {
|
|
|
|
removed = true
|
|
|
|
}
|
2014-10-09 03:25:31 +02:00
|
|
|
if removed && schema.Computed {
|
|
|
|
return nil
|
|
|
|
}
|
2014-08-21 06:02:42 +02:00
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
|
helper/schema,terraform: handle computed primtives in diffs
Fixes #3309
There are two primary changes, one to how helper/schema creates diffs
and one to how Terraform compares diffs. Both require careful
understanding.
== 1. helper/schema Changes
helper/schema, given any primitive field (string, int, bool, etc.)
_used to_ create a basic diff when given a computed new value (i.e. from
an unkown interpolation). This would put in the plan that the old value
is whatever the old value was, and the new value was the actual
interpolation. For example, from #3309, the diff showed the following:
```
~ module.test.aws_eip.test-instance.0
instance: "<INSTANCE ID>" => "${element(aws_instance.test-instance.*.id, count.index)}"
```
Then, when running `apply`, the diff would be realized and you would get
a diff mismatch error because it would realize the final value is the
same and remove it from the diff.
**The change:** `helper/schema` now marks unknown primitive values with
`NewComputed` set to true. Semantically this is correct for the diff to
have this information.
== 2. Terraform Diff.Same Changes
Next, the way Terraform compares diffs needed to be updated
Specifically, the case where the diff from the plan had a NewComputed
primitive and the diff from the apply _no longer has that value_. This
is possible if the computed value ended up being the same as the old
value. This is allowed to pass through.
Together, these fix #3309.
2016-10-26 04:36:59 +02:00
|
|
|
Old: os,
|
|
|
|
New: ns,
|
|
|
|
NewExtra: originalN,
|
|
|
|
NewRemoved: removed,
|
|
|
|
NewComputed: computed,
|
2014-08-15 08:17:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
2014-08-16 07:00:16 +02:00
|
|
|
|
2014-09-29 19:25:43 +02:00
|
|
|
func (m schemaMap) inputString(
|
|
|
|
input terraform.UIInput,
|
|
|
|
k string,
|
|
|
|
schema *Schema) (interface{}, error) {
|
|
|
|
result, err := input.Input(&terraform.InputOpts{
|
2014-09-29 22:30:28 +02:00
|
|
|
Id: k,
|
|
|
|
Query: k,
|
|
|
|
Description: schema.Description,
|
2014-09-29 23:00:35 +02:00
|
|
|
Default: schema.InputDefault,
|
2014-09-29 19:25:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
func (m schemaMap) validate(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
|
|
|
c *terraform.ResourceConfig) ([]string, []error) {
|
|
|
|
raw, ok := c.Get(k)
|
2014-09-10 06:33:08 +02:00
|
|
|
if !ok && schema.DefaultFunc != nil {
|
|
|
|
// We have a dynamic default. Check if we have a value.
|
|
|
|
var err error
|
|
|
|
raw, err = schema.DefaultFunc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{fmt.Errorf(
|
2015-03-05 22:16:50 +01:00
|
|
|
"%q, error loading default: %s", k, err)}
|
2014-09-10 06:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're okay as long as we had a value set
|
|
|
|
ok = raw != nil
|
|
|
|
}
|
2014-08-16 07:00:16 +02:00
|
|
|
if !ok {
|
|
|
|
if schema.Required {
|
|
|
|
return nil, []error{fmt.Errorf(
|
2015-03-05 22:16:50 +01:00
|
|
|
"%q: required field is not set", k)}
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:18:45 +02:00
|
|
|
if !schema.Required && !schema.Optional {
|
|
|
|
// This is a computed-only field
|
|
|
|
return nil, []error{fmt.Errorf(
|
2015-03-05 22:16:50 +01:00
|
|
|
"%q: this field cannot be set", k)}
|
2014-08-16 18:18:45 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 12:54:45 +02:00
|
|
|
err := m.validateConflictingAttributes(k, schema, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
2015-01-12 13:57:47 +01:00
|
|
|
return m.validateType(k, raw, schema, c)
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
2015-04-19 12:54:45 +02:00
|
|
|
func (m schemaMap) validateConflictingAttributes(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
|
|
|
c *terraform.ResourceConfig) error {
|
|
|
|
|
|
|
|
if len(schema.ConflictsWith) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, conflicting_key := range schema.ConflictsWith {
|
|
|
|
if value, ok := c.Get(conflicting_key); ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%q: conflicts with %s (%#v)", k, conflicting_key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
func (m schemaMap) validateList(
|
|
|
|
k string,
|
|
|
|
raw interface{},
|
|
|
|
schema *Schema,
|
|
|
|
c *terraform.ResourceConfig) ([]string, []error) {
|
2014-08-18 23:20:57 +02:00
|
|
|
// We use reflection to verify the slice because you can't
|
|
|
|
// case to []interface{} unless the slice is exactly that type.
|
|
|
|
rawV := reflect.ValueOf(raw)
|
|
|
|
if rawV.Kind() != reflect.Slice {
|
2014-08-16 07:00:16 +02:00
|
|
|
return nil, []error{fmt.Errorf(
|
2015-01-14 18:29:37 +01:00
|
|
|
"%s: should be a list", k)}
|
2014-08-18 23:20:57 +02:00
|
|
|
}
|
|
|
|
|
2016-02-18 02:20:36 +01:00
|
|
|
// Validate length
|
|
|
|
if schema.MaxItems > 0 && rawV.Len() > schema.MaxItems {
|
|
|
|
return nil, []error{fmt.Errorf(
|
|
|
|
"%s: attribute supports %d item maximum, config has %d declared", k, schema.MaxItems, rawV.Len())}
|
|
|
|
}
|
|
|
|
|
2016-10-04 19:37:23 +02:00
|
|
|
if schema.MinItems > 0 && rawV.Len() < schema.MinItems {
|
|
|
|
return nil, []error{fmt.Errorf(
|
|
|
|
"%s: attribute supports %d item as a minimum, config has %d declared", k, schema.MinItems, rawV.Len())}
|
|
|
|
}
|
|
|
|
|
2014-08-18 23:20:57 +02:00
|
|
|
// Now build the []interface{}
|
|
|
|
raws := make([]interface{}, rawV.Len())
|
|
|
|
for i, _ := range raws {
|
|
|
|
raws[i] = rawV.Index(i).Interface()
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ws []string
|
|
|
|
var es []error
|
|
|
|
for i, raw := range raws {
|
|
|
|
key := fmt.Sprintf("%s.%d", k, i)
|
|
|
|
|
|
|
|
var ws2 []string
|
|
|
|
var es2 []error
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
// This is a sub-resource
|
|
|
|
ws2, es2 = m.validateObject(key, t.Schema, c)
|
|
|
|
case *Schema:
|
2015-01-12 13:57:47 +01:00
|
|
|
ws2, es2 = m.validateType(key, raw, t, c)
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ws2) > 0 {
|
|
|
|
ws = append(ws, ws2...)
|
|
|
|
}
|
|
|
|
if len(es2) > 0 {
|
|
|
|
es = append(es, es2...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ws, es
|
|
|
|
}
|
|
|
|
|
2014-10-20 05:33:00 +02:00
|
|
|
func (m schemaMap) validateMap(
|
|
|
|
k string,
|
|
|
|
raw interface{},
|
|
|
|
schema *Schema,
|
|
|
|
c *terraform.ResourceConfig) ([]string, []error) {
|
|
|
|
// We use reflection to verify the slice because you can't
|
|
|
|
// case to []interface{} unless the slice is exactly that type.
|
|
|
|
rawV := reflect.ValueOf(raw)
|
|
|
|
switch rawV.Kind() {
|
2016-04-27 23:41:17 +02:00
|
|
|
case reflect.String:
|
|
|
|
// If raw and reified are equal, this is a string and should
|
|
|
|
// be rejected.
|
|
|
|
reified, reifiedOk := c.Get(k)
|
|
|
|
if reifiedOk && raw == reified && !c.IsComputed(k) {
|
|
|
|
return nil, []error{fmt.Errorf("%s: should be a map", k)}
|
|
|
|
}
|
|
|
|
// Otherwise it's likely raw is an interpolation.
|
|
|
|
return nil, nil
|
2014-10-20 05:33:00 +02:00
|
|
|
case reflect.Map:
|
|
|
|
case reflect.Slice:
|
|
|
|
default:
|
2016-04-27 23:41:17 +02:00
|
|
|
return nil, []error{fmt.Errorf("%s: should be a map", k)}
|
2014-10-20 05:33:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it is not a slice, it is valid
|
|
|
|
if rawV.Kind() != reflect.Slice {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is a slice, verify that all the elements are maps
|
|
|
|
raws := make([]interface{}, rawV.Len())
|
|
|
|
for i, _ := range raws {
|
|
|
|
raws[i] = rawV.Index(i).Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, raw := range raws {
|
|
|
|
v := reflect.ValueOf(raw)
|
|
|
|
if v.Kind() != reflect.Map {
|
|
|
|
return nil, []error{fmt.Errorf(
|
|
|
|
"%s: should be a map", k)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-14 20:44:28 +02:00
|
|
|
if schema.ValidateFunc != nil {
|
|
|
|
validatableMap := make(map[string]interface{})
|
|
|
|
for _, raw := range raws {
|
|
|
|
for k, v := range raw.(map[string]interface{}) {
|
|
|
|
validatableMap[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.ValidateFunc(validatableMap, k)
|
|
|
|
}
|
|
|
|
|
2014-10-20 05:33:00 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
func (m schemaMap) validateObject(
|
|
|
|
k string,
|
|
|
|
schema map[string]*Schema,
|
|
|
|
c *terraform.ResourceConfig) ([]string, []error) {
|
2015-06-24 01:39:02 +02:00
|
|
|
raw, _ := c.GetRaw(k)
|
|
|
|
if _, ok := raw.(map[string]interface{}); !ok {
|
|
|
|
return nil, []error{fmt.Errorf(
|
|
|
|
"%s: expected object, got %s",
|
|
|
|
k, reflect.ValueOf(raw).Kind())}
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
var ws []string
|
|
|
|
var es []error
|
|
|
|
for subK, s := range schema {
|
2014-08-16 07:15:10 +02:00
|
|
|
key := subK
|
|
|
|
if k != "" {
|
|
|
|
key = fmt.Sprintf("%s.%s", k, subK)
|
|
|
|
}
|
2014-08-16 07:00:16 +02:00
|
|
|
|
2014-08-16 07:15:10 +02:00
|
|
|
ws2, es2 := m.validate(key, s, c)
|
2014-08-16 07:00:16 +02:00
|
|
|
if len(ws2) > 0 {
|
|
|
|
ws = append(ws, ws2...)
|
|
|
|
}
|
|
|
|
if len(es2) > 0 {
|
|
|
|
es = append(es, es2...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:15:10 +02:00
|
|
|
// Detect any extra/unknown keys and report those as errors.
|
2015-02-18 18:41:55 +01:00
|
|
|
if m, ok := raw.(map[string]interface{}); ok {
|
|
|
|
for subk, _ := range m {
|
|
|
|
if _, ok := schema[subk]; !ok {
|
|
|
|
es = append(es, fmt.Errorf(
|
|
|
|
"%s: invalid or unknown key: %s", k, subk))
|
2014-08-16 07:15:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
return ws, es
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m schemaMap) validatePrimitive(
|
|
|
|
k string,
|
|
|
|
raw interface{},
|
|
|
|
schema *Schema,
|
|
|
|
c *terraform.ResourceConfig) ([]string, []error) {
|
2015-08-16 02:16:14 +02:00
|
|
|
|
|
|
|
// Catch if the user gave a complex type where a primitive was
|
|
|
|
// expected, so we can return a friendly error message that
|
|
|
|
// doesn't contain Go type system terminology.
|
|
|
|
switch reflect.ValueOf(raw).Type().Kind() {
|
|
|
|
case reflect.Slice:
|
|
|
|
return nil, []error{
|
|
|
|
fmt.Errorf("%s must be a single value, not a list", k),
|
|
|
|
}
|
|
|
|
case reflect.Map:
|
|
|
|
return nil, []error{
|
|
|
|
fmt.Errorf("%s must be a single value, not a map", k),
|
|
|
|
}
|
|
|
|
default: // ok
|
|
|
|
}
|
|
|
|
|
2014-10-16 23:04:45 +02:00
|
|
|
if c.IsComputed(k) {
|
2015-08-16 02:16:14 +02:00
|
|
|
// If the key is being computed, then it is not an error as
|
|
|
|
// long as it's not a slice or map.
|
2014-10-16 23:04:45 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-06-11 14:06:30 +02:00
|
|
|
var decoded interface{}
|
2014-08-16 07:00:16 +02:00
|
|
|
switch schema.Type {
|
2014-10-20 04:56:46 +02:00
|
|
|
case TypeBool:
|
|
|
|
// Verify that we can parse this as the correct type
|
|
|
|
var n bool
|
|
|
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
2015-06-11 14:06:30 +02:00
|
|
|
decoded = n
|
2014-08-16 07:00:16 +02:00
|
|
|
case TypeInt:
|
|
|
|
// Verify that we can parse this as an int
|
|
|
|
var n int
|
|
|
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
2015-06-11 14:06:30 +02:00
|
|
|
decoded = n
|
2015-01-28 18:53:34 +01:00
|
|
|
case TypeFloat:
|
|
|
|
// Verify that we can parse this as an int
|
|
|
|
var n float64
|
|
|
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
2015-06-11 14:06:30 +02:00
|
|
|
decoded = n
|
2014-10-20 04:56:46 +02:00
|
|
|
case TypeString:
|
|
|
|
// Verify that we can parse this as a string
|
|
|
|
var n string
|
|
|
|
if err := mapstructure.WeakDecode(raw, &n); err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
2015-06-11 14:06:30 +02:00
|
|
|
decoded = n
|
2014-10-20 04:56:46 +02:00
|
|
|
default:
|
2014-11-02 13:56:44 +01:00
|
|
|
panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-11 14:06:30 +02:00
|
|
|
if schema.ValidateFunc != nil {
|
2015-06-24 18:29:24 +02:00
|
|
|
return schema.ValidateFunc(decoded, k)
|
2015-06-11 14:06:30 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-01-12 13:57:47 +01:00
|
|
|
|
|
|
|
func (m schemaMap) validateType(
|
|
|
|
k string,
|
|
|
|
raw interface{},
|
|
|
|
schema *Schema,
|
|
|
|
c *terraform.ResourceConfig) ([]string, []error) {
|
2015-03-05 22:16:50 +01:00
|
|
|
var ws []string
|
|
|
|
var es []error
|
2015-01-12 13:57:47 +01:00
|
|
|
switch schema.Type {
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeSet, TypeList:
|
2015-03-05 22:16:50 +01:00
|
|
|
ws, es = m.validateList(k, raw, schema, c)
|
2015-01-12 13:57:47 +01:00
|
|
|
case TypeMap:
|
2015-03-05 22:16:50 +01:00
|
|
|
ws, es = m.validateMap(k, raw, schema, c)
|
2015-01-12 13:57:47 +01:00
|
|
|
default:
|
2015-03-05 22:16:50 +01:00
|
|
|
ws, es = m.validatePrimitive(k, raw, schema, c)
|
2015-01-12 13:57:47 +01:00
|
|
|
}
|
2015-03-05 22:16:50 +01:00
|
|
|
|
|
|
|
if schema.Deprecated != "" {
|
|
|
|
ws = append(ws, fmt.Sprintf(
|
|
|
|
"%q: [DEPRECATED] %s", k, schema.Deprecated))
|
|
|
|
}
|
|
|
|
|
2015-03-05 22:33:56 +01:00
|
|
|
if schema.Removed != "" {
|
|
|
|
es = append(es, fmt.Errorf(
|
|
|
|
"%q: [REMOVED] %s", k, schema.Removed))
|
|
|
|
}
|
|
|
|
|
2015-03-05 22:16:50 +01:00
|
|
|
return ws, es
|
2015-01-12 13:57:47 +01:00
|
|
|
}
|
2015-02-18 01:58:47 +01:00
|
|
|
|
|
|
|
// Zero returns the zero value for a type.
|
|
|
|
func (t ValueType) Zero() interface{} {
|
|
|
|
switch t {
|
|
|
|
case TypeInvalid:
|
|
|
|
return nil
|
|
|
|
case TypeBool:
|
|
|
|
return false
|
|
|
|
case TypeInt:
|
|
|
|
return 0
|
|
|
|
case TypeFloat:
|
|
|
|
return 0.0
|
|
|
|
case TypeString:
|
|
|
|
return ""
|
|
|
|
case TypeList:
|
|
|
|
return []interface{}{}
|
|
|
|
case TypeMap:
|
|
|
|
return map[string]interface{}{}
|
|
|
|
case TypeSet:
|
|
|
|
return new(Set)
|
|
|
|
case typeObject:
|
|
|
|
return map[string]interface{}{}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown type %s", t))
|
|
|
|
}
|
|
|
|
}
|