]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blame - vendor/github.com/hashicorp/terraform/helper/schema/schema.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / helper / schema / schema.go
CommitLineData
bae9f6d2
JC
1// schema is a high-level framework for easily writing new providers
2// for Terraform. Usage of schema is recommended over attempting to write
3// to the low-level plugin interfaces manually.
4//
5// schema breaks down provider creation into simple CRUD operations for
6// resources. The logic of diffing, destroying before creating, updating
7// or creating, etc. is all handled by the framework. The plugin author
8// only needs to implement a configuration schema and the CRUD operations and
9// everything else is meant to just work.
10//
11// A good starting point is to view the Provider structure.
12package schema
13
14import (
107c1cdb 15 "context"
bae9f6d2
JC
16 "fmt"
17 "os"
18 "reflect"
c680a8e1 19 "regexp"
bae9f6d2
JC
20 "sort"
21 "strconv"
22 "strings"
107c1cdb 23 "sync"
bae9f6d2 24
107c1cdb 25 "github.com/hashicorp/terraform/config"
bae9f6d2 26 "github.com/hashicorp/terraform/terraform"
15c0b25d 27 "github.com/mitchellh/copystructure"
bae9f6d2
JC
28 "github.com/mitchellh/mapstructure"
29)
30
15c0b25d
AP
31// Name of ENV variable which (if not empty) prefers panic over error
32const PanicOnErr = "TF_SCHEMA_PANIC_ON_ERROR"
33
bae9f6d2
JC
34// type used for schema package context keys
35type contextKey string
36
107c1cdb
ND
37var (
38 protoVersionMu sync.Mutex
39 protoVersion5 = false
40)
41
42func isProto5() bool {
43 protoVersionMu.Lock()
44 defer protoVersionMu.Unlock()
45 return protoVersion5
46
47}
48
49// SetProto5 enables a feature flag for any internal changes required required
50// to work with the new plugin protocol. This should not be called by
51// provider.
52func SetProto5() {
53 protoVersionMu.Lock()
54 defer protoVersionMu.Unlock()
55 protoVersion5 = true
56}
57
bae9f6d2
JC
58// Schema is used to describe the structure of a value.
59//
60// Read the documentation of the struct elements for important details.
61type Schema struct {
62 // Type is the type of the value and must be one of the ValueType values.
63 //
64 // This type not only determines what type is expected/valid in configuring
65 // this value, but also what type is returned when ResourceData.Get is
66 // called. The types returned by Get are:
67 //
68 // TypeBool - bool
69 // TypeInt - int
70 // TypeFloat - float64
71 // TypeString - string
72 // TypeList - []interface{}
73 // TypeMap - map[string]interface{}
74 // TypeSet - *schema.Set
75 //
76 Type ValueType
77
107c1cdb
ND
78 // ConfigMode allows for overriding the default behaviors for mapping
79 // schema entries onto configuration constructs.
80 //
81 // By default, the Elem field is used to choose whether a particular
82 // schema is represented in configuration as an attribute or as a nested
83 // block; if Elem is a *schema.Resource then it's a block and it's an
84 // attribute otherwise.
85 //
86 // If Elem is *schema.Resource then setting ConfigMode to
87 // SchemaConfigModeAttr will force it to be represented in configuration
88 // as an attribute, which means that the Computed flag can be used to
89 // provide default elements when the argument isn't set at all, while still
90 // allowing the user to force zero elements by explicitly assigning an
91 // empty list.
92 //
93 // When Computed is set without Optional, the attribute is not settable
94 // in configuration at all and so SchemaConfigModeAttr is the automatic
95 // behavior, and SchemaConfigModeBlock is not permitted.
96 ConfigMode SchemaConfigMode
97
bae9f6d2
JC
98 // If one of these is set, then this item can come from the configuration.
99 // Both cannot be set. If Optional is set, the value is optional. If
100 // Required is set, the value is required.
101 //
102 // One of these must be set if the value is not computed. That is:
103 // value either comes from the config, is computed, or is both.
104 Optional bool
105 Required bool
106
107 // If this is non-nil, the provided function will be used during diff
108 // of this field. If this is nil, a default diff for the type of the
109 // schema will be used.
110 //
111 // This allows comparison based on something other than primitive, list
112 // or map equality - for example SSH public keys may be considered
113 // equivalent regardless of trailing whitespace.
114 DiffSuppressFunc SchemaDiffSuppressFunc
115
116 // If this is non-nil, then this will be a default value that is used
117 // when this item is not set in the configuration.
118 //
119 // DefaultFunc can be specified to compute a dynamic default.
120 // Only one of Default or DefaultFunc can be set. If DefaultFunc is
121 // used then its return value should be stable to avoid generating
122 // confusing/perpetual diffs.
123 //
124 // Changing either Default or the return value of DefaultFunc can be
125 // a breaking change, especially if the attribute in question has
126 // ForceNew set. If a default needs to change to align with changing
127 // assumptions in an upstream API then it may be necessary to also use
128 // the MigrateState function on the resource to change the state to match,
129 // or have the Read function adjust the state value to align with the
130 // new default.
131 //
132 // If Required is true above, then Default cannot be set. DefaultFunc
133 // can be set with Required. If the DefaultFunc returns nil, then there
134 // will be no default and the user will be asked to fill it in.
135 //
136 // If either of these is set, then the user won't be asked for input
137 // for this key if the default is not nil.
138 Default interface{}
139 DefaultFunc SchemaDefaultFunc
140
141 // Description is used as the description for docs or asking for user
142 // input. It should be relatively short (a few sentences max) and should
143 // be formatted to fit a CLI.
144 Description string
145
146 // InputDefault is the default value to use for when inputs are requested.
147 // This differs from Default in that if Default is set, no input is
148 // asked for. If Input is asked, this will be the default value offered.
149 InputDefault string
150
151 // The fields below relate to diffs.
152 //
153 // If Computed is true, then the result of this value is computed
154 // (unless specified by config) on creation.
155 //
156 // If ForceNew is true, then a change in this resource necessitates
157 // the creation of a new resource.
158 //
159 // StateFunc is a function called to change the value of this before
160 // storing it in the state (and likewise before comparing for diffs).
161 // The use for this is for example with large strings, you may want
162 // to simply store the hash of it.
163 Computed bool
164 ForceNew bool
165 StateFunc SchemaStateFunc
166
15c0b25d 167 // The following fields are only set for a TypeList, TypeSet, or TypeMap.
bae9f6d2 168 //
15c0b25d 169 // Elem represents the element type. For a TypeMap, it must be a *Schema
107c1cdb
ND
170 // with a Type that is one of the primitives: TypeString, TypeBool,
171 // TypeInt, or TypeFloat. Otherwise it may be either a *Schema or a
15c0b25d
AP
172 // *Resource. If it is *Schema, the element type is just a simple value.
173 // If it is *Resource, the element type is a complex structure,
174 // potentially with its own lifecycle.
175 Elem interface{}
176
177 // The following fields are only set for a TypeList or TypeSet.
bae9f6d2
JC
178 //
179 // MaxItems defines a maximum amount of items that can exist within a
180 // TypeSet or TypeList. Specific use cases would be if a TypeSet is being
181 // used to wrap a complex structure, however more than one instance would
182 // cause instability.
183 //
184 // MinItems defines a minimum amount of items that can exist within a
185 // TypeSet or TypeList. Specific use cases would be if a TypeSet is being
186 // used to wrap a complex structure, however less than one instance would
187 // cause instability.
188 //
107c1cdb
ND
189 // If the field Optional is set to true then MinItems is ignored and thus
190 // effectively zero.
191 MaxItems int
192 MinItems int
193
194 // PromoteSingle originally allowed for a single element to be assigned
195 // where a primitive list was expected, but this no longer works from
196 // Terraform v0.12 onwards (Terraform Core will require a list to be set
197 // regardless of what this is set to) and so only applies to Terraform v0.11
198 // and earlier, and so should be used only to retain this functionality
199 // for those still using v0.11 with a provider that formerly used this.
bae9f6d2
JC
200 PromoteSingle bool
201
202 // The following fields are only valid for a TypeSet type.
203 //
204 // Set defines a function to determine the unique ID of an item so that
205 // a proper set can be built.
206 Set SchemaSetFunc
207
208 // ComputedWhen is a set of queries on the configuration. Whenever any
209 // of these things is changed, it will require a recompute (this requires
210 // that Computed is set to true).
211 //
212 // NOTE: This currently does not work.
213 ComputedWhen []string
214
215 // ConflictsWith is a set of schema keys that conflict with this schema.
216 // This will only check that they're set in the _config_. This will not
217 // raise an error for a malfunctioning resource that sets a conflicting
218 // key.
219 ConflictsWith []string
220
221 // When Deprecated is set, this attribute is deprecated.
222 //
223 // A deprecated field still works, but will probably stop working in near
224 // future. This string is the message shown to the user with instructions on
225 // how to address the deprecation.
226 Deprecated string
227
228 // When Removed is set, this attribute has been removed from the schema
229 //
230 // Removed attributes can be left in the Schema to generate informative error
231 // messages for the user when they show up in resource configurations.
232 // This string is the message shown to the user with instructions on
233 // what do to about the removed attribute.
234 Removed string
235
236 // ValidateFunc allows individual fields to define arbitrary validation
237 // logic. It is yielded the provided config value as an interface{} that is
238 // guaranteed to be of the proper Schema type, and it can yield warnings or
239 // errors based on inspection of that value.
240 //
107c1cdb
ND
241 // ValidateFunc is honored only when the schema's Type is set to TypeInt,
242 // TypeFloat, TypeString, TypeBool, or TypeMap. It is ignored for all other types.
bae9f6d2
JC
243 ValidateFunc SchemaValidateFunc
244
245 // Sensitive ensures that the attribute's value does not get displayed in
246 // logs or regular output. It should be used for passwords or other
247 // secret fields. Future versions of Terraform may encrypt these
248 // values.
249 Sensitive bool
250}
251
107c1cdb
ND
252// SchemaConfigMode is used to influence how a schema item is mapped into a
253// corresponding configuration construct, using the ConfigMode field of
254// Schema.
255type SchemaConfigMode int
256
257const (
258 SchemaConfigModeAuto SchemaConfigMode = iota
259 SchemaConfigModeAttr
260 SchemaConfigModeBlock
261)
262
15c0b25d 263// SchemaDiffSuppressFunc is a function which can be used to determine
bae9f6d2
JC
264// whether a detected diff on a schema element is "valid" or not, and
265// suppress it from the plan if necessary.
266//
267// Return true if the diff should be suppressed, false to retain it.
268type SchemaDiffSuppressFunc func(k, old, new string, d *ResourceData) bool
269
270// SchemaDefaultFunc is a function called to return a default value for
271// a field.
272type SchemaDefaultFunc func() (interface{}, error)
273
274// EnvDefaultFunc is a helper function that returns the value of the
275// given environment variable, if one exists, or the default value
276// otherwise.
277func EnvDefaultFunc(k string, dv interface{}) SchemaDefaultFunc {
278 return func() (interface{}, error) {
279 if v := os.Getenv(k); v != "" {
280 return v, nil
281 }
282
283 return dv, nil
284 }
285}
286
287// MultiEnvDefaultFunc is a helper function that returns the value of the first
288// environment variable in the given list that returns a non-empty value. If
289// none of the environment variables return a value, the default value is
290// returned.
291func MultiEnvDefaultFunc(ks []string, dv interface{}) SchemaDefaultFunc {
292 return func() (interface{}, error) {
293 for _, k := range ks {
294 if v := os.Getenv(k); v != "" {
295 return v, nil
296 }
297 }
298 return dv, nil
299 }
300}
301
302// SchemaSetFunc is a function that must return a unique ID for the given
303// element. This unique ID is used to store the element in a hash.
304type SchemaSetFunc func(interface{}) int
305
306// SchemaStateFunc is a function used to convert some type to a string
307// to be stored in the state.
308type SchemaStateFunc func(interface{}) string
309
310// SchemaValidateFunc is a function used to validate a single field in the
311// schema.
312type SchemaValidateFunc func(interface{}, string) ([]string, []error)
313
314func (s *Schema) GoString() string {
315 return fmt.Sprintf("*%#v", *s)
316}
317
318// Returns a default value for this schema by either reading Default or
319// evaluating DefaultFunc. If neither of these are defined, returns nil.
320func (s *Schema) DefaultValue() (interface{}, error) {
321 if s.Default != nil {
322 return s.Default, nil
323 }
324
325 if s.DefaultFunc != nil {
326 defaultValue, err := s.DefaultFunc()
327 if err != nil {
328 return nil, fmt.Errorf("error loading default: %s", err)
329 }
330 return defaultValue, nil
331 }
332
333 return nil, nil
334}
335
336// Returns a zero value for the schema.
337func (s *Schema) ZeroValue() interface{} {
338 // If it's a set then we'll do a bit of extra work to provide the
339 // right hashing function in our empty value.
340 if s.Type == TypeSet {
341 setFunc := s.Set
342 if setFunc == nil {
343 // Default set function uses the schema to hash the whole value
344 elem := s.Elem
345 switch t := elem.(type) {
346 case *Schema:
347 setFunc = HashSchema(t)
348 case *Resource:
349 setFunc = HashResource(t)
350 default:
351 panic("invalid set element type")
352 }
353 }
354 return &Set{F: setFunc}
355 } else {
356 return s.Type.Zero()
357 }
358}
359
15c0b25d 360func (s *Schema) finalizeDiff(d *terraform.ResourceAttrDiff, customized bool) *terraform.ResourceAttrDiff {
bae9f6d2
JC
361 if d == nil {
362 return d
363 }
364
365 if s.Type == TypeBool {
366 normalizeBoolString := func(s string) string {
367 switch s {
368 case "0":
369 return "false"
370 case "1":
371 return "true"
372 }
373 return s
374 }
375 d.Old = normalizeBoolString(d.Old)
376 d.New = normalizeBoolString(d.New)
377 }
378
379 if s.Computed && !d.NewRemoved && d.New == "" {
380 // Computed attribute without a new value set
381 d.NewComputed = true
382 }
383
384 if s.ForceNew {
385 // ForceNew, mark that this field is requiring new under the
386 // following conditions, explained below:
387 //
388 // * Old != New - There is a change in value. This field
389 // is therefore causing a new resource.
390 //
391 // * NewComputed - This field is being computed, hence a
392 // potential change in value, mark as causing a new resource.
393 d.RequiresNew = d.Old != d.New || d.NewComputed
394 }
395
396 if d.NewRemoved {
397 return d
398 }
399
400 if s.Computed {
15c0b25d
AP
401 // FIXME: This is where the customized bool from getChange finally
402 // comes into play. It allows the previously incorrect behavior
403 // of an empty string being used as "unset" when the value is
404 // computed. This should be removed once we can properly
405 // represent an unset/nil value from the configuration.
406 if !customized {
407 if d.Old != "" && d.New == "" {
408 // This is a computed value with an old value set already,
409 // just let it go.
410 return nil
411 }
bae9f6d2
JC
412 }
413
15c0b25d 414 if d.New == "" && !d.NewComputed {
bae9f6d2
JC
415 // Computed attribute without a new value set
416 d.NewComputed = true
417 }
418 }
419
420 if s.Sensitive {
421 // Set the Sensitive flag so output is hidden in the UI
422 d.Sensitive = true
423 }
424
425 return d
426}
427
107c1cdb
ND
428// InternalMap is used to aid in the transition to the new schema types and
429// protocol. The name is not meant to convey any usefulness, as this is not to
430// be used directly by any providers.
431type InternalMap = schemaMap
432
bae9f6d2
JC
433// schemaMap is a wrapper that adds nice functions on top of schemas.
434type schemaMap map[string]*Schema
435
15c0b25d
AP
436func (m schemaMap) panicOnError() bool {
437 if os.Getenv(PanicOnErr) != "" {
438 return true
439 }
440 return false
441}
442
bae9f6d2
JC
443// Data returns a ResourceData for the given schema, state, and diff.
444//
445// The diff is optional.
446func (m schemaMap) Data(
447 s *terraform.InstanceState,
448 d *terraform.InstanceDiff) (*ResourceData, error) {
449 return &ResourceData{
15c0b25d
AP
450 schema: m,
451 state: s,
452 diff: d,
453 panicOnError: m.panicOnError(),
bae9f6d2
JC
454 }, nil
455}
456
15c0b25d
AP
457// DeepCopy returns a copy of this schemaMap. The copy can be safely modified
458// without affecting the original.
459func (m *schemaMap) DeepCopy() schemaMap {
460 copy, err := copystructure.Config{Lock: true}.Copy(m)
461 if err != nil {
462 panic(err)
463 }
464 return *copy.(*schemaMap)
465}
466
bae9f6d2
JC
467// Diff returns the diff for a resource given the schema map,
468// state, and configuration.
469func (m schemaMap) Diff(
470 s *terraform.InstanceState,
15c0b25d
AP
471 c *terraform.ResourceConfig,
472 customizeDiff CustomizeDiffFunc,
107c1cdb
ND
473 meta interface{},
474 handleRequiresNew bool) (*terraform.InstanceDiff, error) {
bae9f6d2
JC
475 result := new(terraform.InstanceDiff)
476 result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
477
478 // Make sure to mark if the resource is tainted
479 if s != nil {
480 result.DestroyTainted = s.Tainted
481 }
482
483 d := &ResourceData{
15c0b25d
AP
484 schema: m,
485 state: s,
486 config: c,
487 panicOnError: m.panicOnError(),
bae9f6d2
JC
488 }
489
490 for k, schema := range m {
491 err := m.diff(k, schema, result, d, false)
492 if err != nil {
493 return nil, err
494 }
495 }
496
15c0b25d
AP
497 // Remove any nil diffs just to keep things clean
498 for k, v := range result.Attributes {
499 if v == nil {
500 delete(result.Attributes, k)
501 }
502 }
503
504 // If this is a non-destroy diff, call any custom diff logic that has been
505 // defined.
506 if !result.DestroyTainted && customizeDiff != nil {
507 mc := m.DeepCopy()
508 rd := newResourceDiff(mc, c, s, result)
509 if err := customizeDiff(rd, meta); err != nil {
510 return nil, err
511 }
512 for _, k := range rd.UpdatedKeys() {
513 err := m.diff(k, mc[k], result, rd, false)
514 if err != nil {
515 return nil, err
516 }
517 }
518 }
519
107c1cdb
ND
520 if handleRequiresNew {
521 // If the diff requires a new resource, then we recompute the diff
522 // so we have the complete new resource diff, and preserve the
523 // RequiresNew fields where necessary so the user knows exactly what
524 // caused that.
525 if result.RequiresNew() {
526 // Create the new diff
527 result2 := new(terraform.InstanceDiff)
528 result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)
bae9f6d2 529
107c1cdb
ND
530 // Preserve the DestroyTainted flag
531 result2.DestroyTainted = result.DestroyTainted
bae9f6d2 532
107c1cdb
ND
533 // Reset the data to not contain state. We have to call init()
534 // again in order to reset the FieldReaders.
535 d.state = nil
536 d.init()
bae9f6d2 537
107c1cdb
ND
538 // Perform the diff again
539 for k, schema := range m {
540 err := m.diff(k, schema, result2, d, false)
15c0b25d
AP
541 if err != nil {
542 return nil, err
543 }
544 }
15c0b25d 545
107c1cdb
ND
546 // Re-run customization
547 if !result2.DestroyTainted && customizeDiff != nil {
548 mc := m.DeepCopy()
549 rd := newResourceDiff(mc, c, d.state, result2)
550 if err := customizeDiff(rd, meta); err != nil {
551 return nil, err
552 }
553 for _, k := range rd.UpdatedKeys() {
554 err := m.diff(k, mc[k], result2, rd, false)
555 if err != nil {
556 return nil, err
557 }
558 }
bae9f6d2
JC
559 }
560
107c1cdb
ND
561 // Force all the fields to not force a new since we know what we
562 // want to force new.
563 for k, attr := range result2.Attributes {
564 if attr == nil {
565 continue
566 }
bae9f6d2 567
107c1cdb
ND
568 if attr.RequiresNew {
569 attr.RequiresNew = false
570 }
bae9f6d2 571
107c1cdb
ND
572 if s != nil {
573 attr.Old = s.Attributes[k]
574 }
bae9f6d2
JC
575 }
576
107c1cdb
ND
577 // Now copy in all the requires new diffs...
578 for k, attr := range result.Attributes {
579 if attr == nil {
580 continue
581 }
bae9f6d2 582
107c1cdb
ND
583 newAttr, ok := result2.Attributes[k]
584 if !ok {
585 newAttr = attr
586 }
587
588 if attr.RequiresNew {
589 newAttr.RequiresNew = true
590 }
591
592 result2.Attributes[k] = newAttr
bae9f6d2
JC
593 }
594
107c1cdb
ND
595 // And set the diff!
596 result = result2
bae9f6d2
JC
597 }
598
bae9f6d2
JC
599 }
600
bae9f6d2
JC
601 // Go through and detect all of the ComputedWhens now that we've
602 // finished the diff.
603 // TODO
604
605 if result.Empty() {
606 // If we don't have any diff elements, just return nil
607 return nil, nil
608 }
609
610 return result, nil
611}
612
613// Input implements the terraform.ResourceProvider method by asking
614// for input for required configuration keys that don't have a value.
615func (m schemaMap) Input(
616 input terraform.UIInput,
617 c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
618 keys := make([]string, 0, len(m))
619 for k, _ := range m {
620 keys = append(keys, k)
621 }
622 sort.Strings(keys)
623
624 for _, k := range keys {
625 v := m[k]
626
627 // Skip things that don't require config, if that is even valid
628 // for a provider schema.
629 // Required XOR Optional must always be true to validate, so we only
630 // need to check one.
631 if v.Optional {
632 continue
633 }
634
635 // Deprecated fields should never prompt
636 if v.Deprecated != "" {
637 continue
638 }
639
640 // Skip things that have a value of some sort already
641 if _, ok := c.Raw[k]; ok {
642 continue
643 }
644
645 // Skip if it has a default value
646 defaultValue, err := v.DefaultValue()
647 if err != nil {
648 return nil, fmt.Errorf("%s: error loading default: %s", k, err)
649 }
650 if defaultValue != nil {
651 continue
652 }
653
654 var value interface{}
655 switch v.Type {
656 case TypeBool, TypeInt, TypeFloat, TypeSet, TypeList:
657 continue
658 case TypeString:
659 value, err = m.inputString(input, k, v)
660 default:
661 panic(fmt.Sprintf("Unknown type for input: %#v", v.Type))
662 }
663
664 if err != nil {
665 return nil, fmt.Errorf(
666 "%s: %s", k, err)
667 }
668
669 c.Config[k] = value
670 }
671
672 return c, nil
673}
674
675// Validate validates the configuration against this schema mapping.
676func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
677 return m.validateObject("", m, c)
678}
679
680// InternalValidate validates the format of this schema. This should be called
681// from a unit test (and not in user-path code) to verify that a schema
682// is properly built.
683func (m schemaMap) InternalValidate(topSchemaMap schemaMap) error {
107c1cdb
ND
684 return m.internalValidate(topSchemaMap, false)
685}
686
687func (m schemaMap) internalValidate(topSchemaMap schemaMap, attrsOnly bool) error {
bae9f6d2
JC
688 if topSchemaMap == nil {
689 topSchemaMap = m
690 }
691 for k, v := range m {
692 if v.Type == TypeInvalid {
693 return fmt.Errorf("%s: Type must be specified", k)
694 }
695
696 if v.Optional && v.Required {
697 return fmt.Errorf("%s: Optional or Required must be set, not both", k)
698 }
699
700 if v.Required && v.Computed {
701 return fmt.Errorf("%s: Cannot be both Required and Computed", k)
702 }
703
704 if !v.Required && !v.Optional && !v.Computed {
705 return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
706 }
707
107c1cdb
ND
708 computedOnly := v.Computed && !v.Optional
709
710 switch v.ConfigMode {
711 case SchemaConfigModeBlock:
712 if _, ok := v.Elem.(*Resource); !ok {
713 return fmt.Errorf("%s: ConfigMode of block is allowed only when Elem is *schema.Resource", k)
714 }
715 if attrsOnly {
716 return fmt.Errorf("%s: ConfigMode of block cannot be used in child of schema with ConfigMode of attribute", k)
717 }
718 if computedOnly {
719 return fmt.Errorf("%s: ConfigMode of block cannot be used for computed schema", k)
720 }
721 case SchemaConfigModeAttr:
722 // anything goes
723 case SchemaConfigModeAuto:
724 // Since "Auto" for Elem: *Resource would create a nested block,
725 // and that's impossible inside an attribute, we require it to be
726 // explicitly overridden as mode "Attr" for clarity.
727 if _, ok := v.Elem.(*Resource); ok {
728 if attrsOnly {
729 return fmt.Errorf("%s: in *schema.Resource with ConfigMode of attribute, so must also have ConfigMode of attribute", k)
730 }
731 }
732 default:
733 return fmt.Errorf("%s: invalid ConfigMode value", k)
734 }
735
bae9f6d2
JC
736 if v.Computed && v.Default != nil {
737 return fmt.Errorf("%s: Default must be nil if computed", k)
738 }
739
740 if v.Required && v.Default != nil {
741 return fmt.Errorf("%s: Default cannot be set with Required", k)
742 }
743
744 if len(v.ComputedWhen) > 0 && !v.Computed {
745 return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
746 }
747
748 if len(v.ConflictsWith) > 0 && v.Required {
749 return fmt.Errorf("%s: ConflictsWith cannot be set with Required", k)
750 }
751
752 if len(v.ConflictsWith) > 0 {
753 for _, key := range v.ConflictsWith {
754 parts := strings.Split(key, ".")
755 sm := topSchemaMap
756 var target *Schema
757 for _, part := range parts {
758 // Skip index fields
759 if _, err := strconv.Atoi(part); err == nil {
760 continue
761 }
762
763 var ok bool
764 if target, ok = sm[part]; !ok {
765 return fmt.Errorf("%s: ConflictsWith references unknown attribute (%s)", k, key)
766 }
767
768 if subResource, ok := target.Elem.(*Resource); ok {
769 sm = schemaMap(subResource.Schema)
770 }
771 }
772 if target == nil {
773 return fmt.Errorf("%s: ConflictsWith cannot find target attribute (%s), sm: %#v", k, key, sm)
774 }
775 if target.Required {
776 return fmt.Errorf("%s: ConflictsWith cannot contain Required attribute (%s)", k, key)
777 }
778
779 if len(target.ComputedWhen) > 0 {
780 return fmt.Errorf("%s: ConflictsWith cannot contain Computed(When) attribute (%s)", k, key)
781 }
782 }
783 }
784
785 if v.Type == TypeList || v.Type == TypeSet {
786 if v.Elem == nil {
787 return fmt.Errorf("%s: Elem must be set for lists", k)
788 }
789
790 if v.Default != nil {
791 return fmt.Errorf("%s: Default is not valid for lists or sets", k)
792 }
793
794 if v.Type != TypeSet && v.Set != nil {
795 return fmt.Errorf("%s: Set can only be set for TypeSet", k)
796 }
797
798 switch t := v.Elem.(type) {
799 case *Resource:
107c1cdb
ND
800 attrsOnly := attrsOnly || v.ConfigMode == SchemaConfigModeAttr
801
802 if err := schemaMap(t.Schema).internalValidate(topSchemaMap, attrsOnly); err != nil {
bae9f6d2
JC
803 return err
804 }
805 case *Schema:
806 bad := t.Computed || t.Optional || t.Required
807 if bad {
808 return fmt.Errorf(
809 "%s: Elem must have only Type set", k)
810 }
811 }
812 } else {
813 if v.MaxItems > 0 || v.MinItems > 0 {
814 return fmt.Errorf("%s: MaxItems and MinItems are only supported on lists or sets", k)
815 }
816 }
817
818 // Computed-only field
819 if v.Computed && !v.Optional {
820 if v.ValidateFunc != nil {
821 return fmt.Errorf("%s: ValidateFunc is for validating user input, "+
822 "there's nothing to validate on computed-only field", k)
823 }
824 if v.DiffSuppressFunc != nil {
825 return fmt.Errorf("%s: DiffSuppressFunc is for suppressing differences"+
826 " between config and state representation. "+
827 "There is no config for computed-only field, nothing to compare.", k)
828 }
829 }
830
831 if v.ValidateFunc != nil {
832 switch v.Type {
833 case TypeList, TypeSet:
c680a8e1
RS
834 return fmt.Errorf("%s: ValidateFunc is not yet supported on lists or sets.", k)
835 }
836 }
837
838 if v.Deprecated == "" && v.Removed == "" {
839 if !isValidFieldName(k) {
840 return fmt.Errorf("%s: Field name may only contain lowercase alphanumeric characters & underscores.", k)
bae9f6d2
JC
841 }
842 }
843 }
844
845 return nil
846}
847
c680a8e1
RS
848func isValidFieldName(name string) bool {
849 re := regexp.MustCompile("^[a-z0-9_]+$")
850 return re.MatchString(name)
851}
852
15c0b25d
AP
853// resourceDiffer is an interface that is used by the private diff functions.
854// This helps facilitate diff logic for both ResourceData and ResoureDiff with
855// minimal divergence in code.
856type resourceDiffer interface {
857 diffChange(string) (interface{}, interface{}, bool, bool, bool)
858 Get(string) interface{}
859 GetChange(string) (interface{}, interface{})
860 GetOk(string) (interface{}, bool)
861 HasChange(string) bool
862 Id() string
863}
864
bae9f6d2
JC
865func (m schemaMap) diff(
866 k string,
867 schema *Schema,
868 diff *terraform.InstanceDiff,
15c0b25d 869 d resourceDiffer,
bae9f6d2
JC
870 all bool) error {
871
872 unsupressedDiff := new(terraform.InstanceDiff)
873 unsupressedDiff.Attributes = make(map[string]*terraform.ResourceAttrDiff)
874
875 var err error
876 switch schema.Type {
877 case TypeBool, TypeInt, TypeFloat, TypeString:
878 err = m.diffString(k, schema, unsupressedDiff, d, all)
879 case TypeList:
880 err = m.diffList(k, schema, unsupressedDiff, d, all)
881 case TypeMap:
882 err = m.diffMap(k, schema, unsupressedDiff, d, all)
883 case TypeSet:
884 err = m.diffSet(k, schema, unsupressedDiff, d, all)
885 default:
886 err = fmt.Errorf("%s: unknown type %#v", k, schema.Type)
887 }
888
889 for attrK, attrV := range unsupressedDiff.Attributes {
15c0b25d
AP
890 switch rd := d.(type) {
891 case *ResourceData:
107c1cdb 892 if schema.DiffSuppressFunc != nil && attrV != nil &&
15c0b25d 893 schema.DiffSuppressFunc(attrK, attrV.Old, attrV.New, rd) {
107c1cdb
ND
894 // If this attr diff is suppressed, we may still need it in the
895 // overall diff if it's contained within a set. Rather than
896 // dropping the diff, make it a NOOP.
897 if !all {
898 continue
899 }
900
901 attrV = &terraform.ResourceAttrDiff{
902 Old: attrV.Old,
903 New: attrV.Old,
904 }
15c0b25d 905 }
bae9f6d2 906 }
bae9f6d2
JC
907 diff.Attributes[attrK] = attrV
908 }
909
910 return err
911}
912
913func (m schemaMap) diffList(
914 k string,
915 schema *Schema,
916 diff *terraform.InstanceDiff,
15c0b25d 917 d resourceDiffer,
bae9f6d2 918 all bool) error {
15c0b25d 919 o, n, _, computedList, customized := d.diffChange(k)
bae9f6d2
JC
920 if computedList {
921 n = nil
922 }
923 nSet := n != nil
924
925 // If we have an old value and no new value is set or will be
926 // computed once all variables can be interpolated and we're
927 // computed, then nothing has changed.
928 if o != nil && n == nil && !computedList && schema.Computed {
929 return nil
930 }
931
932 if o == nil {
933 o = []interface{}{}
934 }
935 if n == nil {
936 n = []interface{}{}
937 }
938 if s, ok := o.(*Set); ok {
939 o = s.List()
940 }
941 if s, ok := n.(*Set); ok {
942 n = s.List()
943 }
944 os := o.([]interface{})
945 vs := n.([]interface{})
946
947 // If the new value was set, and the two are equal, then we're done.
948 // We have to do this check here because sets might be NOT
949 // reflect.DeepEqual so we need to wait until we get the []interface{}
950 if !all && nSet && reflect.DeepEqual(os, vs) {
951 return nil
952 }
953
954 // Get the counts
955 oldLen := len(os)
956 newLen := len(vs)
957 oldStr := strconv.FormatInt(int64(oldLen), 10)
958
959 // If the whole list is computed, then say that the # is computed
960 if computedList {
961 diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{
962 Old: oldStr,
963 NewComputed: true,
964 RequiresNew: schema.ForceNew,
965 }
966 return nil
967 }
968
969 // If the counts are not the same, then record that diff
970 changed := oldLen != newLen
971 computed := oldLen == 0 && newLen == 0 && schema.Computed
972 if changed || computed || all {
973 countSchema := &Schema{
974 Type: TypeInt,
975 Computed: schema.Computed,
976 ForceNew: schema.ForceNew,
977 }
978
979 newStr := ""
980 if !computed {
981 newStr = strconv.FormatInt(int64(newLen), 10)
982 } else {
983 oldStr = ""
984 }
985
15c0b25d
AP
986 diff.Attributes[k+".#"] = countSchema.finalizeDiff(
987 &terraform.ResourceAttrDiff{
988 Old: oldStr,
989 New: newStr,
990 },
991 customized,
992 )
bae9f6d2
JC
993 }
994
995 // Figure out the maximum
996 maxLen := oldLen
997 if newLen > maxLen {
998 maxLen = newLen
999 }
1000
1001 switch t := schema.Elem.(type) {
1002 case *Resource:
1003 // This is a complex resource
1004 for i := 0; i < maxLen; i++ {
1005 for k2, schema := range t.Schema {
1006 subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
1007 err := m.diff(subK, schema, diff, d, all)
1008 if err != nil {
1009 return err
1010 }
1011 }
1012 }
1013 case *Schema:
1014 // Copy the schema so that we can set Computed/ForceNew from
1015 // the parent schema (the TypeList).
1016 t2 := *t
1017 t2.ForceNew = schema.ForceNew
1018
1019 // This is just a primitive element, so go through each and
1020 // just diff each.
1021 for i := 0; i < maxLen; i++ {
1022 subK := fmt.Sprintf("%s.%d", k, i)
1023 err := m.diff(subK, &t2, diff, d, all)
1024 if err != nil {
1025 return err
1026 }
1027 }
1028 default:
1029 return fmt.Errorf("%s: unknown element type (internal)", k)
1030 }
1031
1032 return nil
1033}
1034
1035func (m schemaMap) diffMap(
1036 k string,
1037 schema *Schema,
1038 diff *terraform.InstanceDiff,
15c0b25d 1039 d resourceDiffer,
bae9f6d2
JC
1040 all bool) error {
1041 prefix := k + "."
1042
1043 // First get all the values from the state
1044 var stateMap, configMap map[string]string
15c0b25d 1045 o, n, _, nComputed, customized := d.diffChange(k)
bae9f6d2
JC
1046 if err := mapstructure.WeakDecode(o, &stateMap); err != nil {
1047 return fmt.Errorf("%s: %s", k, err)
1048 }
1049 if err := mapstructure.WeakDecode(n, &configMap); err != nil {
1050 return fmt.Errorf("%s: %s", k, err)
1051 }
1052
1053 // Keep track of whether the state _exists_ at all prior to clearing it
1054 stateExists := o != nil
1055
1056 // Delete any count values, since we don't use those
1057 delete(configMap, "%")
1058 delete(stateMap, "%")
1059
1060 // Check if the number of elements has changed.
1061 oldLen, newLen := len(stateMap), len(configMap)
1062 changed := oldLen != newLen
1063 if oldLen != 0 && newLen == 0 && schema.Computed {
1064 changed = false
1065 }
1066
1067 // It is computed if we have no old value, no new value, the schema
1068 // says it is computed, and it didn't exist in the state before. The
1069 // last point means: if it existed in the state, even empty, then it
1070 // has already been computed.
1071 computed := oldLen == 0 && newLen == 0 && schema.Computed && !stateExists
1072
1073 // If the count has changed or we're computed, then add a diff for the
1074 // count. "nComputed" means that the new value _contains_ a value that
1075 // is computed. We don't do granular diffs for this yet, so we mark the
1076 // whole map as computed.
1077 if changed || computed || nComputed {
1078 countSchema := &Schema{
1079 Type: TypeInt,
1080 Computed: schema.Computed || nComputed,
1081 ForceNew: schema.ForceNew,
1082 }
1083
1084 oldStr := strconv.FormatInt(int64(oldLen), 10)
1085 newStr := ""
1086 if !computed && !nComputed {
1087 newStr = strconv.FormatInt(int64(newLen), 10)
1088 } else {
1089 oldStr = ""
1090 }
1091
1092 diff.Attributes[k+".%"] = countSchema.finalizeDiff(
1093 &terraform.ResourceAttrDiff{
1094 Old: oldStr,
1095 New: newStr,
1096 },
15c0b25d 1097 customized,
bae9f6d2
JC
1098 )
1099 }
1100
1101 // If the new map is nil and we're computed, then ignore it.
1102 if n == nil && schema.Computed {
1103 return nil
1104 }
1105
1106 // Now we compare, preferring values from the config map
1107 for k, v := range configMap {
1108 old, ok := stateMap[k]
1109 delete(stateMap, k)
1110
1111 if old == v && ok && !all {
1112 continue
1113 }
1114
15c0b25d
AP
1115 diff.Attributes[prefix+k] = schema.finalizeDiff(
1116 &terraform.ResourceAttrDiff{
1117 Old: old,
1118 New: v,
1119 },
1120 customized,
1121 )
bae9f6d2
JC
1122 }
1123 for k, v := range stateMap {
15c0b25d
AP
1124 diff.Attributes[prefix+k] = schema.finalizeDiff(
1125 &terraform.ResourceAttrDiff{
1126 Old: v,
1127 NewRemoved: true,
1128 },
1129 customized,
1130 )
bae9f6d2
JC
1131 }
1132
1133 return nil
1134}
1135
1136func (m schemaMap) diffSet(
1137 k string,
1138 schema *Schema,
1139 diff *terraform.InstanceDiff,
15c0b25d 1140 d resourceDiffer,
bae9f6d2
JC
1141 all bool) error {
1142
15c0b25d 1143 o, n, _, computedSet, customized := d.diffChange(k)
bae9f6d2
JC
1144 if computedSet {
1145 n = nil
1146 }
1147 nSet := n != nil
1148
1149 // If we have an old value and no new value is set or will be
1150 // computed once all variables can be interpolated and we're
1151 // computed, then nothing has changed.
1152 if o != nil && n == nil && !computedSet && schema.Computed {
1153 return nil
1154 }
1155
1156 if o == nil {
1157 o = schema.ZeroValue().(*Set)
1158 }
1159 if n == nil {
1160 n = schema.ZeroValue().(*Set)
1161 }
1162 os := o.(*Set)
1163 ns := n.(*Set)
1164
1165 // If the new value was set, compare the listCode's to determine if
1166 // the two are equal. Comparing listCode's instead of the actual values
1167 // is needed because there could be computed values in the set which
1168 // would result in false positives while comparing.
1169 if !all && nSet && reflect.DeepEqual(os.listCode(), ns.listCode()) {
1170 return nil
1171 }
1172
1173 // Get the counts
1174 oldLen := os.Len()
1175 newLen := ns.Len()
1176 oldStr := strconv.Itoa(oldLen)
1177 newStr := strconv.Itoa(newLen)
1178
1179 // Build a schema for our count
1180 countSchema := &Schema{
1181 Type: TypeInt,
1182 Computed: schema.Computed,
1183 ForceNew: schema.ForceNew,
1184 }
1185
1186 // If the set computed then say that the # is computed
1187 if computedSet || schema.Computed && !nSet {
1188 // If # already exists, equals 0 and no new set is supplied, there
1189 // is nothing to record in the diff
1190 count, ok := d.GetOk(k + ".#")
1191 if ok && count.(int) == 0 && !nSet && !computedSet {
1192 return nil
1193 }
1194
1195 // Set the count but make sure that if # does not exist, we don't
1196 // use the zeroed value
1197 countStr := strconv.Itoa(count.(int))
1198 if !ok {
1199 countStr = ""
1200 }
1201
15c0b25d
AP
1202 diff.Attributes[k+".#"] = countSchema.finalizeDiff(
1203 &terraform.ResourceAttrDiff{
1204 Old: countStr,
1205 NewComputed: true,
1206 },
1207 customized,
1208 )
bae9f6d2
JC
1209 return nil
1210 }
1211
1212 // If the counts are not the same, then record that diff
1213 changed := oldLen != newLen
1214 if changed || all {
15c0b25d
AP
1215 diff.Attributes[k+".#"] = countSchema.finalizeDiff(
1216 &terraform.ResourceAttrDiff{
1217 Old: oldStr,
1218 New: newStr,
1219 },
1220 customized,
1221 )
bae9f6d2
JC
1222 }
1223
1224 // Build the list of codes that will make up our set. This is the
1225 // removed codes as well as all the codes in the new codes.
1226 codes := make([][]string, 2)
1227 codes[0] = os.Difference(ns).listCode()
1228 codes[1] = ns.listCode()
1229 for _, list := range codes {
1230 for _, code := range list {
1231 switch t := schema.Elem.(type) {
1232 case *Resource:
1233 // This is a complex resource
1234 for k2, schema := range t.Schema {
1235 subK := fmt.Sprintf("%s.%s.%s", k, code, k2)
1236 err := m.diff(subK, schema, diff, d, true)
1237 if err != nil {
1238 return err
1239 }
1240 }
1241 case *Schema:
1242 // Copy the schema so that we can set Computed/ForceNew from
1243 // the parent schema (the TypeSet).
1244 t2 := *t
1245 t2.ForceNew = schema.ForceNew
1246
1247 // This is just a primitive element, so go through each and
1248 // just diff each.
1249 subK := fmt.Sprintf("%s.%s", k, code)
1250 err := m.diff(subK, &t2, diff, d, true)
1251 if err != nil {
1252 return err
1253 }
1254 default:
1255 return fmt.Errorf("%s: unknown element type (internal)", k)
1256 }
1257 }
1258 }
1259
1260 return nil
1261}
1262
1263func (m schemaMap) diffString(
1264 k string,
1265 schema *Schema,
1266 diff *terraform.InstanceDiff,
15c0b25d 1267 d resourceDiffer,
bae9f6d2
JC
1268 all bool) error {
1269 var originalN interface{}
1270 var os, ns string
15c0b25d 1271 o, n, _, computed, customized := d.diffChange(k)
bae9f6d2
JC
1272 if schema.StateFunc != nil && n != nil {
1273 originalN = n
1274 n = schema.StateFunc(n)
1275 }
1276 nraw := n
1277 if nraw == nil && o != nil {
1278 nraw = schema.Type.Zero()
1279 }
1280 if err := mapstructure.WeakDecode(o, &os); err != nil {
1281 return fmt.Errorf("%s: %s", k, err)
1282 }
1283 if err := mapstructure.WeakDecode(nraw, &ns); err != nil {
1284 return fmt.Errorf("%s: %s", k, err)
1285 }
1286
107c1cdb 1287 if os == ns && !all && !computed {
bae9f6d2
JC
1288 // They're the same value. If there old value is not blank or we
1289 // have an ID, then return right away since we're already setup.
1290 if os != "" || d.Id() != "" {
1291 return nil
1292 }
1293
1294 // Otherwise, only continue if we're computed
107c1cdb 1295 if !schema.Computed {
bae9f6d2
JC
1296 return nil
1297 }
1298 }
1299
1300 removed := false
15c0b25d 1301 if o != nil && n == nil && !computed {
bae9f6d2
JC
1302 removed = true
1303 }
1304 if removed && schema.Computed {
1305 return nil
1306 }
1307
15c0b25d
AP
1308 diff.Attributes[k] = schema.finalizeDiff(
1309 &terraform.ResourceAttrDiff{
1310 Old: os,
1311 New: ns,
1312 NewExtra: originalN,
1313 NewRemoved: removed,
1314 NewComputed: computed,
1315 },
1316 customized,
1317 )
bae9f6d2
JC
1318
1319 return nil
1320}
1321
1322func (m schemaMap) inputString(
1323 input terraform.UIInput,
1324 k string,
1325 schema *Schema) (interface{}, error) {
107c1cdb 1326 result, err := input.Input(context.Background(), &terraform.InputOpts{
bae9f6d2
JC
1327 Id: k,
1328 Query: k,
1329 Description: schema.Description,
1330 Default: schema.InputDefault,
1331 })
1332
1333 return result, err
1334}
1335
1336func (m schemaMap) validate(
1337 k string,
1338 schema *Schema,
1339 c *terraform.ResourceConfig) ([]string, []error) {
1340 raw, ok := c.Get(k)
1341 if !ok && schema.DefaultFunc != nil {
1342 // We have a dynamic default. Check if we have a value.
1343 var err error
1344 raw, err = schema.DefaultFunc()
1345 if err != nil {
1346 return nil, []error{fmt.Errorf(
1347 "%q, error loading default: %s", k, err)}
1348 }
1349
1350 // We're okay as long as we had a value set
1351 ok = raw != nil
1352 }
1353 if !ok {
1354 if schema.Required {
1355 return nil, []error{fmt.Errorf(
1356 "%q: required field is not set", k)}
1357 }
1358
1359 return nil, nil
1360 }
1361
1362 if !schema.Required && !schema.Optional {
1363 // This is a computed-only field
1364 return nil, []error{fmt.Errorf(
1365 "%q: this field cannot be set", k)}
1366 }
1367
107c1cdb
ND
1368 if raw == config.UnknownVariableValue {
1369 // If the value is unknown then we can't validate it yet.
1370 // In particular, this avoids spurious type errors where downstream
1371 // validation code sees UnknownVariableValue as being just a string.
1372 return nil, nil
1373 }
1374
bae9f6d2
JC
1375 err := m.validateConflictingAttributes(k, schema, c)
1376 if err != nil {
1377 return nil, []error{err}
1378 }
1379
1380 return m.validateType(k, raw, schema, c)
1381}
1382
1383func (m schemaMap) validateConflictingAttributes(
1384 k string,
1385 schema *Schema,
1386 c *terraform.ResourceConfig) error {
1387
1388 if len(schema.ConflictsWith) == 0 {
1389 return nil
1390 }
1391
107c1cdb
ND
1392 for _, conflictingKey := range schema.ConflictsWith {
1393 if raw, ok := c.Get(conflictingKey); ok {
1394 if raw == config.UnknownVariableValue {
1395 // An unknown value might become unset (null) once known, so
1396 // we must defer validation until it's known.
1397 continue
1398 }
bae9f6d2 1399 return fmt.Errorf(
107c1cdb 1400 "%q: conflicts with %s", k, conflictingKey)
bae9f6d2
JC
1401 }
1402 }
1403
1404 return nil
1405}
1406
1407func (m schemaMap) validateList(
1408 k string,
1409 raw interface{},
1410 schema *Schema,
1411 c *terraform.ResourceConfig) ([]string, []error) {
107c1cdb
ND
1412 // first check if the list is wholly unknown
1413 if s, ok := raw.(string); ok {
1414 if s == config.UnknownVariableValue {
1415 return nil, nil
1416 }
1417 }
1418
bae9f6d2
JC
1419 // We use reflection to verify the slice because you can't
1420 // case to []interface{} unless the slice is exactly that type.
1421 rawV := reflect.ValueOf(raw)
1422
1423 // If we support promotion and the raw value isn't a slice, wrap
1424 // it in []interface{} and check again.
1425 if schema.PromoteSingle && rawV.Kind() != reflect.Slice {
1426 raw = []interface{}{raw}
1427 rawV = reflect.ValueOf(raw)
1428 }
1429
1430 if rawV.Kind() != reflect.Slice {
1431 return nil, []error{fmt.Errorf(
1432 "%s: should be a list", k)}
1433 }
1434
1435 // Validate length
1436 if schema.MaxItems > 0 && rawV.Len() > schema.MaxItems {
1437 return nil, []error{fmt.Errorf(
1438 "%s: attribute supports %d item maximum, config has %d declared", k, schema.MaxItems, rawV.Len())}
1439 }
1440
1441 if schema.MinItems > 0 && rawV.Len() < schema.MinItems {
1442 return nil, []error{fmt.Errorf(
1443 "%s: attribute supports %d item as a minimum, config has %d declared", k, schema.MinItems, rawV.Len())}
1444 }
1445
1446 // Now build the []interface{}
1447 raws := make([]interface{}, rawV.Len())
1448 for i, _ := range raws {
1449 raws[i] = rawV.Index(i).Interface()
1450 }
1451
1452 var ws []string
1453 var es []error
1454 for i, raw := range raws {
1455 key := fmt.Sprintf("%s.%d", k, i)
1456
1457 // Reify the key value from the ResourceConfig.
1458 // If the list was computed we have all raw values, but some of these
1459 // may be known in the config, and aren't individually marked as Computed.
1460 if r, ok := c.Get(key); ok {
1461 raw = r
1462 }
1463
1464 var ws2 []string
1465 var es2 []error
1466 switch t := schema.Elem.(type) {
1467 case *Resource:
1468 // This is a sub-resource
1469 ws2, es2 = m.validateObject(key, t.Schema, c)
1470 case *Schema:
1471 ws2, es2 = m.validateType(key, raw, t, c)
1472 }
1473
1474 if len(ws2) > 0 {
1475 ws = append(ws, ws2...)
1476 }
1477 if len(es2) > 0 {
1478 es = append(es, es2...)
1479 }
1480 }
1481
1482 return ws, es
1483}
1484
1485func (m schemaMap) validateMap(
1486 k string,
1487 raw interface{},
1488 schema *Schema,
1489 c *terraform.ResourceConfig) ([]string, []error) {
107c1cdb
ND
1490 // first check if the list is wholly unknown
1491 if s, ok := raw.(string); ok {
1492 if s == config.UnknownVariableValue {
1493 return nil, nil
1494 }
1495 }
1496
bae9f6d2
JC
1497 // We use reflection to verify the slice because you can't
1498 // case to []interface{} unless the slice is exactly that type.
1499 rawV := reflect.ValueOf(raw)
1500 switch rawV.Kind() {
1501 case reflect.String:
1502 // If raw and reified are equal, this is a string and should
1503 // be rejected.
1504 reified, reifiedOk := c.Get(k)
1505 if reifiedOk && raw == reified && !c.IsComputed(k) {
1506 return nil, []error{fmt.Errorf("%s: should be a map", k)}
1507 }
1508 // Otherwise it's likely raw is an interpolation.
1509 return nil, nil
1510 case reflect.Map:
1511 case reflect.Slice:
1512 default:
1513 return nil, []error{fmt.Errorf("%s: should be a map", k)}
1514 }
1515
1516 // If it is not a slice, validate directly
1517 if rawV.Kind() != reflect.Slice {
1518 mapIface := rawV.Interface()
1519 if _, errs := validateMapValues(k, mapIface.(map[string]interface{}), schema); len(errs) > 0 {
1520 return nil, errs
1521 }
1522 if schema.ValidateFunc != nil {
1523 return schema.ValidateFunc(mapIface, k)
1524 }
1525 return nil, nil
1526 }
1527
1528 // It is a slice, verify that all the elements are maps
1529 raws := make([]interface{}, rawV.Len())
1530 for i, _ := range raws {
1531 raws[i] = rawV.Index(i).Interface()
1532 }
1533
1534 for _, raw := range raws {
1535 v := reflect.ValueOf(raw)
1536 if v.Kind() != reflect.Map {
1537 return nil, []error{fmt.Errorf(
1538 "%s: should be a map", k)}
1539 }
1540 mapIface := v.Interface()
1541 if _, errs := validateMapValues(k, mapIface.(map[string]interface{}), schema); len(errs) > 0 {
1542 return nil, errs
1543 }
1544 }
1545
1546 if schema.ValidateFunc != nil {
1547 validatableMap := make(map[string]interface{})
1548 for _, raw := range raws {
1549 for k, v := range raw.(map[string]interface{}) {
1550 validatableMap[k] = v
1551 }
1552 }
1553
1554 return schema.ValidateFunc(validatableMap, k)
1555 }
1556
1557 return nil, nil
1558}
1559
1560func validateMapValues(k string, m map[string]interface{}, schema *Schema) ([]string, []error) {
1561 for key, raw := range m {
1562 valueType, err := getValueType(k, schema)
1563 if err != nil {
1564 return nil, []error{err}
1565 }
1566
1567 switch valueType {
1568 case TypeBool:
1569 var n bool
1570 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1571 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1572 }
1573 case TypeInt:
1574 var n int
1575 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1576 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1577 }
1578 case TypeFloat:
1579 var n float64
1580 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1581 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1582 }
1583 case TypeString:
1584 var n string
1585 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1586 return nil, []error{fmt.Errorf("%s (%s): %s", k, key, err)}
1587 }
1588 default:
1589 panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
1590 }
1591 }
1592 return nil, nil
1593}
1594
1595func getValueType(k string, schema *Schema) (ValueType, error) {
1596 if schema.Elem == nil {
1597 return TypeString, nil
1598 }
1599 if vt, ok := schema.Elem.(ValueType); ok {
1600 return vt, nil
1601 }
1602
15c0b25d
AP
1603 // If a Schema is provided to a Map, we use the Type of that schema
1604 // as the type for each element in the Map.
bae9f6d2 1605 if s, ok := schema.Elem.(*Schema); ok {
15c0b25d 1606 return s.Type, nil
bae9f6d2
JC
1607 }
1608
1609 if _, ok := schema.Elem.(*Resource); ok {
1610 // TODO: We don't actually support this (yet)
1611 // but silently pass the validation, until we decide
1612 // how to handle nested structures in maps
1613 return TypeString, nil
1614 }
1615 return 0, fmt.Errorf("%s: unexpected map value type: %#v", k, schema.Elem)
1616}
1617
1618func (m schemaMap) validateObject(
1619 k string,
1620 schema map[string]*Schema,
1621 c *terraform.ResourceConfig) ([]string, []error) {
9b12e4fe
JC
1622 raw, _ := c.Get(k)
1623 if _, ok := raw.(map[string]interface{}); !ok && !c.IsComputed(k) {
bae9f6d2
JC
1624 return nil, []error{fmt.Errorf(
1625 "%s: expected object, got %s",
1626 k, reflect.ValueOf(raw).Kind())}
1627 }
1628
1629 var ws []string
1630 var es []error
1631 for subK, s := range schema {
1632 key := subK
1633 if k != "" {
1634 key = fmt.Sprintf("%s.%s", k, subK)
1635 }
1636
1637 ws2, es2 := m.validate(key, s, c)
1638 if len(ws2) > 0 {
1639 ws = append(ws, ws2...)
1640 }
1641 if len(es2) > 0 {
1642 es = append(es, es2...)
1643 }
1644 }
1645
1646 // Detect any extra/unknown keys and report those as errors.
1647 if m, ok := raw.(map[string]interface{}); ok {
1648 for subk, _ := range m {
1649 if _, ok := schema[subk]; !ok {
1650 if subk == TimeoutsConfigKey {
1651 continue
1652 }
1653 es = append(es, fmt.Errorf(
1654 "%s: invalid or unknown key: %s", k, subk))
1655 }
1656 }
1657 }
1658
1659 return ws, es
1660}
1661
1662func (m schemaMap) validatePrimitive(
1663 k string,
1664 raw interface{},
1665 schema *Schema,
1666 c *terraform.ResourceConfig) ([]string, []error) {
bae9f6d2
JC
1667 // Catch if the user gave a complex type where a primitive was
1668 // expected, so we can return a friendly error message that
1669 // doesn't contain Go type system terminology.
1670 switch reflect.ValueOf(raw).Type().Kind() {
1671 case reflect.Slice:
1672 return nil, []error{
1673 fmt.Errorf("%s must be a single value, not a list", k),
1674 }
1675 case reflect.Map:
1676 return nil, []error{
1677 fmt.Errorf("%s must be a single value, not a map", k),
1678 }
1679 default: // ok
1680 }
1681
1682 if c.IsComputed(k) {
1683 // If the key is being computed, then it is not an error as
1684 // long as it's not a slice or map.
1685 return nil, nil
1686 }
1687
1688 var decoded interface{}
1689 switch schema.Type {
1690 case TypeBool:
1691 // Verify that we can parse this as the correct type
1692 var n bool
1693 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1694 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1695 }
1696 decoded = n
1697 case TypeInt:
107c1cdb
ND
1698 switch {
1699 case isProto5():
1700 // We need to verify the type precisely, because WeakDecode will
1701 // decode a float as an integer.
1702
1703 // the config shims only use int for integral number values
1704 if v, ok := raw.(int); ok {
1705 decoded = v
1706 } else {
1707 return nil, []error{fmt.Errorf("%s: must be a whole number, got %v", k, raw)}
1708 }
1709 default:
1710 // Verify that we can parse this as an int
1711 var n int
1712 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1713 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1714 }
1715 decoded = n
bae9f6d2 1716 }
bae9f6d2
JC
1717 case TypeFloat:
1718 // Verify that we can parse this as an int
1719 var n float64
1720 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1721 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1722 }
1723 decoded = n
1724 case TypeString:
1725 // Verify that we can parse this as a string
1726 var n string
1727 if err := mapstructure.WeakDecode(raw, &n); err != nil {
1728 return nil, []error{fmt.Errorf("%s: %s", k, err)}
1729 }
1730 decoded = n
1731 default:
1732 panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type))
1733 }
1734
1735 if schema.ValidateFunc != nil {
1736 return schema.ValidateFunc(decoded, k)
1737 }
1738
1739 return nil, nil
1740}
1741
1742func (m schemaMap) validateType(
1743 k string,
1744 raw interface{},
1745 schema *Schema,
1746 c *terraform.ResourceConfig) ([]string, []error) {
1747 var ws []string
1748 var es []error
1749 switch schema.Type {
1750 case TypeSet, TypeList:
1751 ws, es = m.validateList(k, raw, schema, c)
1752 case TypeMap:
1753 ws, es = m.validateMap(k, raw, schema, c)
1754 default:
1755 ws, es = m.validatePrimitive(k, raw, schema, c)
1756 }
1757
1758 if schema.Deprecated != "" {
1759 ws = append(ws, fmt.Sprintf(
1760 "%q: [DEPRECATED] %s", k, schema.Deprecated))
1761 }
1762
1763 if schema.Removed != "" {
1764 es = append(es, fmt.Errorf(
1765 "%q: [REMOVED] %s", k, schema.Removed))
1766 }
1767
1768 return ws, es
1769}
1770
1771// Zero returns the zero value for a type.
1772func (t ValueType) Zero() interface{} {
1773 switch t {
1774 case TypeInvalid:
1775 return nil
1776 case TypeBool:
1777 return false
1778 case TypeInt:
1779 return 0
1780 case TypeFloat:
1781 return 0.0
1782 case TypeString:
1783 return ""
1784 case TypeList:
1785 return []interface{}{}
1786 case TypeMap:
1787 return map[string]interface{}{}
1788 case TypeSet:
1789 return new(Set)
1790 case typeObject:
1791 return map[string]interface{}{}
1792 default:
1793 panic(fmt.Sprintf("unknown type %s", t))
1794 }
1795}