github.com/nalum/terraform@v0.3.2-0.20141223102918-aa2c22ffeff6/helper/schema/schema.go (about) 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. 12 package schema 13 14 import ( 15 "fmt" 16 "reflect" 17 "sort" 18 "strconv" 19 "strings" 20 21 "github.com/hashicorp/terraform/terraform" 22 "github.com/mitchellh/mapstructure" 23 ) 24 25 // ValueType is an enum of the type that can be represented by a schema. 26 type ValueType int 27 28 const ( 29 TypeInvalid ValueType = iota 30 TypeBool 31 TypeInt 32 TypeString 33 TypeList 34 TypeMap 35 TypeSet 36 ) 37 38 // Schema is used to describe the structure of a value. 39 // 40 // Read the documentation of the struct elements for important details. 41 type Schema struct { 42 // Type is the type of the value and must be one of the ValueType values. 43 // 44 // This type not only determines what type is expected/valid in configuring 45 // this value, but also what type is returned when ResourceData.Get is 46 // called. The types returned by Get are: 47 // 48 // TypeBool - bool 49 // TypeInt - int 50 // TypeString - string 51 // TypeList - []interface{} 52 // TypeMap - map[string]interface{} 53 // TypeSet - *schema.Set 54 // 55 Type ValueType 56 57 // If one of these is set, then this item can come from the configuration. 58 // Both cannot be set. If Optional is set, the value is optional. If 59 // Required is set, the value is required. 60 // 61 // One of these must be set if the value is not computed. That is: 62 // value either comes from the config, is computed, or is both. 63 Optional bool 64 Required bool 65 66 // If this is non-nil, then this will be a default value that is used 67 // when this item is not set in the configuration/state. 68 // 69 // DefaultFunc can be specified if you want a dynamic default value. 70 // Only one of Default or DefaultFunc can be set. 71 // 72 // If Required is true above, then Default cannot be set. DefaultFunc 73 // can be set with Required. If the DefaultFunc returns nil, then there 74 // will no default and the user will be asked to fill it in. 75 // 76 // If either of these is set, then the user won't be asked for input 77 // for this key if the default is not nil. 78 Default interface{} 79 DefaultFunc SchemaDefaultFunc 80 81 // Description is used as the description for docs or asking for user 82 // input. It should be relatively short (a few sentences max) and should 83 // be formatted to fit a CLI. 84 Description string 85 86 // InputDefault is the default value to use for when inputs are requested. 87 // This differs from Default in that if Default is set, no input is 88 // asked for. If Input is asked, this will be the default value offered. 89 InputDefault string 90 91 // The fields below relate to diffs. 92 // 93 // If Computed is true, then the result of this value is computed 94 // (unless specified by config) on creation. 95 // 96 // If ForceNew is true, then a change in this resource necessitates 97 // the creation of a new resource. 98 // 99 // StateFunc is a function called to change the value of this before 100 // storing it in the state (and likewise before comparing for diffs). 101 // The use for this is for example with large strings, you may want 102 // to simply store the hash of it. 103 Computed bool 104 ForceNew bool 105 StateFunc SchemaStateFunc 106 107 // The following fields are only set for a TypeList or TypeSet Type. 108 // 109 // Elem must be either a *Schema or a *Resource only if the Type is 110 // TypeList, and represents what the element type is. If it is *Schema, 111 // the element type is just a simple value. If it is *Resource, the 112 // element type is a complex structure, potentially with its own lifecycle. 113 Elem interface{} 114 115 // The follow fields are only valid for a TypeSet type. 116 // 117 // Set defines a function to determine the unique ID of an item so that 118 // a proper set can be built. 119 Set SchemaSetFunc 120 121 // ComputedWhen is a set of queries on the configuration. Whenever any 122 // of these things is changed, it will require a recompute (this requires 123 // that Computed is set to true). 124 // 125 // NOTE: This currently does not work. 126 ComputedWhen []string 127 } 128 129 // SchemaDefaultFunc is a function called to return a default value for 130 // a field. 131 type SchemaDefaultFunc func() (interface{}, error) 132 133 // SchemaSetFunc is a function that must return a unique ID for the given 134 // element. This unique ID is used to store the element in a hash. 135 type SchemaSetFunc func(interface{}) int 136 137 // SchemaStateFunc is a function used to convert some type to a string 138 // to be stored in the state. 139 type SchemaStateFunc func(interface{}) string 140 141 func (s *Schema) GoString() string { 142 return fmt.Sprintf("*%#v", *s) 143 } 144 145 func (s *Schema) finalizeDiff( 146 d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff { 147 if d == nil { 148 return d 149 } 150 151 if d.NewRemoved { 152 return d 153 } 154 155 if s.Computed { 156 if d.Old != "" && d.New == "" { 157 // This is a computed value with an old value set already, 158 // just let it go. 159 return nil 160 } 161 162 if d.New == "" { 163 // Computed attribute without a new value set 164 d.NewComputed = true 165 } 166 } 167 168 if s.ForceNew { 169 // Force new, set it to true in the diff 170 d.RequiresNew = true 171 } 172 173 return d 174 } 175 176 // schemaMap is a wrapper that adds nice functions on top of schemas. 177 type schemaMap map[string]*Schema 178 179 // Data returns a ResourceData for the given schema, state, and diff. 180 // 181 // The diff is optional. 182 func (m schemaMap) Data( 183 s *terraform.InstanceState, 184 d *terraform.InstanceDiff) (*ResourceData, error) { 185 return &ResourceData{ 186 schema: m, 187 state: s, 188 diff: d, 189 }, nil 190 } 191 192 // Diff returns the diff for a resource given the schema map, 193 // state, and configuration. 194 func (m schemaMap) Diff( 195 s *terraform.InstanceState, 196 c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { 197 result := new(terraform.InstanceDiff) 198 result.Attributes = make(map[string]*terraform.ResourceAttrDiff) 199 200 d := &ResourceData{ 201 schema: m, 202 state: s, 203 config: c, 204 diffing: true, 205 } 206 207 for k, schema := range m { 208 err := m.diff(k, schema, result, d, false) 209 if err != nil { 210 return nil, err 211 } 212 } 213 214 // If the diff requires a new resource, then we recompute the diff 215 // so we have the complete new resource diff, and preserve the 216 // RequiresNew fields where necessary so the user knows exactly what 217 // caused that. 218 if result.RequiresNew() { 219 // Create the new diff 220 result2 := new(terraform.InstanceDiff) 221 result2.Attributes = make(map[string]*terraform.ResourceAttrDiff) 222 223 // Reset the data to not contain state 224 d.state = nil 225 226 // Perform the diff again 227 for k, schema := range m { 228 err := m.diff(k, schema, result2, d, false) 229 if err != nil { 230 return nil, err 231 } 232 } 233 234 // Force all the fields to not force a new since we know what we 235 // want to force new. 236 for k, attr := range result2.Attributes { 237 if attr == nil { 238 continue 239 } 240 241 if attr.RequiresNew { 242 attr.RequiresNew = false 243 } 244 245 if s != nil { 246 attr.Old = s.Attributes[k] 247 } 248 } 249 250 // Now copy in all the requires new diffs... 251 for k, attr := range result.Attributes { 252 if attr == nil { 253 continue 254 } 255 256 newAttr, ok := result2.Attributes[k] 257 if !ok { 258 newAttr = attr 259 } 260 261 if attr.RequiresNew { 262 newAttr.RequiresNew = true 263 } 264 265 result2.Attributes[k] = newAttr 266 } 267 268 // And set the diff! 269 result = result2 270 } 271 272 // Remove any nil diffs just to keep things clean 273 for k, v := range result.Attributes { 274 if v == nil { 275 delete(result.Attributes, k) 276 } 277 } 278 279 // Go through and detect all of the ComputedWhens now that we've 280 // finished the diff. 281 // TODO 282 283 if result.Empty() { 284 // If we don't have any diff elements, just return nil 285 return nil, nil 286 } 287 288 return result, nil 289 } 290 291 // Input implements the terraform.ResourceProvider method by asking 292 // for input for required configuration keys that don't have a value. 293 func (m schemaMap) Input( 294 input terraform.UIInput, 295 c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) { 296 keys := make([]string, 0, len(m)) 297 for k, _ := range m { 298 keys = append(keys, k) 299 } 300 sort.Strings(keys) 301 302 for _, k := range keys { 303 v := m[k] 304 305 // Skip things that don't require config, if that is even valid 306 // for a provider schema. 307 if !v.Required && !v.Optional { 308 continue 309 } 310 311 // Skip things that have a value of some sort already 312 if _, ok := c.Raw[k]; ok { 313 continue 314 } 315 316 // Skip if it has a default 317 if v.Default != nil { 318 continue 319 } 320 if f := v.DefaultFunc; f != nil { 321 value, err := f() 322 if err != nil { 323 return nil, fmt.Errorf( 324 "%s: error loading default: %s", k, err) 325 } 326 if value != nil { 327 continue 328 } 329 } 330 331 var value interface{} 332 var err error 333 switch v.Type { 334 case TypeBool: 335 fallthrough 336 case TypeInt: 337 fallthrough 338 case TypeString: 339 value, err = m.inputString(input, k, v) 340 default: 341 panic(fmt.Sprintf("Unknown type for input: %#v", v.Type)) 342 } 343 344 if err != nil { 345 return nil, fmt.Errorf( 346 "%s: %s", k, err) 347 } 348 349 c.Config[k] = value 350 } 351 352 return c, nil 353 } 354 355 // Validate validates the configuration against this schema mapping. 356 func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) { 357 return m.validateObject("", m, c) 358 } 359 360 // InternalValidate validates the format of this schema. This should be called 361 // from a unit test (and not in user-path code) to verify that a schema 362 // is properly built. 363 func (m schemaMap) InternalValidate() error { 364 for k, v := range m { 365 if v.Type == TypeInvalid { 366 return fmt.Errorf("%s: Type must be specified", k) 367 } 368 369 if v.Optional && v.Required { 370 return fmt.Errorf("%s: Optional or Required must be set, not both", k) 371 } 372 373 if v.Required && v.Computed { 374 return fmt.Errorf("%s: Cannot be both Required and Computed", k) 375 } 376 377 if !v.Required && !v.Optional && !v.Computed { 378 return fmt.Errorf("%s: One of optional, required, or computed must be set", k) 379 } 380 381 if v.Computed && v.Default != nil { 382 return fmt.Errorf("%s: Default must be nil if computed", k) 383 } 384 385 if v.Required && v.Default != nil { 386 return fmt.Errorf("%s: Default cannot be set with Required", k) 387 } 388 389 if len(v.ComputedWhen) > 0 && !v.Computed { 390 return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k) 391 } 392 393 if v.Type == TypeList || v.Type == TypeSet { 394 if v.Elem == nil { 395 return fmt.Errorf("%s: Elem must be set for lists", k) 396 } 397 398 if v.Default != nil { 399 return fmt.Errorf("%s: Default is not valid for lists or sets", k) 400 } 401 402 if v.Type == TypeList && v.Set != nil { 403 return fmt.Errorf("%s: Set can only be set for TypeSet", k) 404 } else if v.Type == TypeSet && v.Set == nil { 405 return fmt.Errorf("%s: Set must be set", k) 406 } 407 408 switch t := v.Elem.(type) { 409 case *Resource: 410 if err := t.InternalValidate(); err != nil { 411 return err 412 } 413 case *Schema: 414 bad := t.Computed || t.Optional || t.Required 415 if bad { 416 return fmt.Errorf( 417 "%s: Elem must have only Type set", k) 418 } 419 } 420 } 421 } 422 423 return nil 424 } 425 426 func (m schemaMap) diff( 427 k string, 428 schema *Schema, 429 diff *terraform.InstanceDiff, 430 d *ResourceData, 431 all bool) error { 432 var err error 433 switch schema.Type { 434 case TypeBool: 435 fallthrough 436 case TypeInt: 437 fallthrough 438 case TypeString: 439 err = m.diffString(k, schema, diff, d, all) 440 case TypeList: 441 err = m.diffList(k, schema, diff, d, all) 442 case TypeMap: 443 err = m.diffMap(k, schema, diff, d, all) 444 case TypeSet: 445 err = m.diffSet(k, schema, diff, d, all) 446 default: 447 err = fmt.Errorf("%s: unknown type %#v", k, schema.Type) 448 } 449 450 return err 451 } 452 453 func (m schemaMap) diffList( 454 k string, 455 schema *Schema, 456 diff *terraform.InstanceDiff, 457 d *ResourceData, 458 all bool) error { 459 o, n, _, computedList := d.diffChange(k) 460 nSet := n != nil 461 462 // If we have an old value and no new value is set or will be 463 // computed once all variables can be interpolated and we're 464 // computed, then nothing has changed. 465 if o != nil && n == nil && !computedList && schema.Computed { 466 return nil 467 } 468 469 if o == nil { 470 o = []interface{}{} 471 } 472 if n == nil { 473 n = []interface{}{} 474 } 475 if s, ok := o.(*Set); ok { 476 o = s.List() 477 } 478 if s, ok := n.(*Set); ok { 479 n = s.List() 480 } 481 os := o.([]interface{}) 482 vs := n.([]interface{}) 483 484 // If the new value was set, and the two are equal, then we're done. 485 // We have to do this check here because sets might be NOT 486 // reflect.DeepEqual so we need to wait until we get the []interface{} 487 if !all && nSet && reflect.DeepEqual(os, vs) { 488 return nil 489 } 490 491 // Get the counts 492 oldLen := len(os) 493 newLen := len(vs) 494 oldStr := strconv.FormatInt(int64(oldLen), 10) 495 496 // If the whole list is computed, then say that the # is computed 497 if computedList { 498 diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{ 499 Old: oldStr, 500 NewComputed: true, 501 } 502 return nil 503 } 504 505 // If the counts are not the same, then record that diff 506 changed := oldLen != newLen 507 computed := oldLen == 0 && newLen == 0 && schema.Computed 508 if changed || computed || all { 509 countSchema := &Schema{ 510 Type: TypeInt, 511 Computed: schema.Computed, 512 ForceNew: schema.ForceNew, 513 } 514 515 newStr := "" 516 if !computed { 517 newStr = strconv.FormatInt(int64(newLen), 10) 518 } else { 519 oldStr = "" 520 } 521 522 diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{ 523 Old: oldStr, 524 New: newStr, 525 }) 526 } 527 528 // Figure out the maximum 529 maxLen := oldLen 530 if newLen > maxLen { 531 maxLen = newLen 532 } 533 534 switch t := schema.Elem.(type) { 535 case *Resource: 536 // This is a complex resource 537 for i := 0; i < maxLen; i++ { 538 for k2, schema := range t.Schema { 539 subK := fmt.Sprintf("%s.%d.%s", k, i, k2) 540 err := m.diff(subK, schema, diff, d, all) 541 if err != nil { 542 return err 543 } 544 } 545 } 546 case *Schema: 547 // Copy the schema so that we can set Computed/ForceNew from 548 // the parent schema (the TypeList). 549 t2 := *t 550 t2.ForceNew = schema.ForceNew 551 552 // This is just a primitive element, so go through each and 553 // just diff each. 554 for i := 0; i < maxLen; i++ { 555 subK := fmt.Sprintf("%s.%d", k, i) 556 err := m.diff(subK, &t2, diff, d, all) 557 if err != nil { 558 return err 559 } 560 } 561 default: 562 return fmt.Errorf("%s: unknown element type (internal)", k) 563 } 564 565 return nil 566 } 567 568 func (m schemaMap) diffMap( 569 k string, 570 schema *Schema, 571 diff *terraform.InstanceDiff, 572 d *ResourceData, 573 all bool) error { 574 prefix := k + "." 575 576 // First get all the values from the state 577 var stateMap, configMap map[string]string 578 o, n, _, _ := d.diffChange(k) 579 if err := mapstructure.WeakDecode(o, &stateMap); err != nil { 580 return fmt.Errorf("%s: %s", k, err) 581 } 582 if err := mapstructure.WeakDecode(n, &configMap); err != nil { 583 return fmt.Errorf("%s: %s", k, err) 584 } 585 586 // Delete any count values, since we don't use those 587 delete(configMap, "#") 588 delete(stateMap, "#") 589 590 // Check if the number of elements has changed. If we're computing 591 // a list and there isn't a config, then it hasn't changed. 592 oldLen, newLen := len(stateMap), len(configMap) 593 changed := oldLen != newLen 594 if oldLen != 0 && newLen == 0 && schema.Computed { 595 changed = false 596 } 597 computed := oldLen == 0 && newLen == 0 && schema.Computed 598 if changed || computed { 599 countSchema := &Schema{ 600 Type: TypeInt, 601 Computed: schema.Computed, 602 ForceNew: schema.ForceNew, 603 } 604 605 oldStr := strconv.FormatInt(int64(oldLen), 10) 606 newStr := "" 607 if !computed { 608 newStr = strconv.FormatInt(int64(newLen), 10) 609 } else { 610 oldStr = "" 611 } 612 613 diff.Attributes[k+".#"] = countSchema.finalizeDiff( 614 &terraform.ResourceAttrDiff{ 615 Old: oldStr, 616 New: newStr, 617 }, 618 ) 619 } 620 621 // If the new map is nil and we're computed, then ignore it. 622 if n == nil && schema.Computed { 623 return nil 624 } 625 626 // Now we compare, preferring values from the config map 627 for k, v := range configMap { 628 old := stateMap[k] 629 delete(stateMap, k) 630 631 if old == v && !all { 632 continue 633 } 634 635 diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{ 636 Old: old, 637 New: v, 638 }) 639 } 640 for k, v := range stateMap { 641 diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{ 642 Old: v, 643 NewRemoved: true, 644 }) 645 } 646 647 return nil 648 } 649 650 func (m schemaMap) diffSet( 651 k string, 652 schema *Schema, 653 diff *terraform.InstanceDiff, 654 d *ResourceData, 655 all bool) error { 656 o, n, _, computedSet := d.diffChange(k) 657 nSet := n != nil 658 659 // If we have an old value and no new value is set or will be 660 // computed once all variables can be interpolated and we're 661 // computed, then nothing has changed. 662 if o != nil && n == nil && !computedSet && schema.Computed { 663 return nil 664 } 665 666 if o == nil { 667 o = &Set{F: schema.Set} 668 } 669 if n == nil { 670 n = &Set{F: schema.Set} 671 } 672 os := o.(*Set) 673 ns := n.(*Set) 674 675 // If the new value was set, compare the listCode's to determine if 676 // the two are equal. Comparing listCode's instead of the actuall values 677 // is needed because there could be computed values in the set which 678 // would result in false positives while comparing. 679 if !all && nSet && reflect.DeepEqual(os.listCode(), ns.listCode()) { 680 return nil 681 } 682 683 // Get the counts 684 oldLen := os.Len() 685 newLen := ns.Len() 686 oldStr := strconv.Itoa(oldLen) 687 newStr := strconv.Itoa(newLen) 688 689 // If the set computed then say that the # is computed 690 if computedSet || (schema.Computed && !nSet) { 691 // If # already exists, equals 0 and no new set is supplied, there 692 // is nothing to record in the diff 693 count, ok := d.GetOk(k + ".#") 694 if ok && count.(int) == 0 && !nSet && !computedSet { 695 return nil 696 } 697 698 // Set the count but make sure that if # does not exist, we don't 699 // use the zeroed value 700 countStr := strconv.Itoa(count.(int)) 701 if !ok { 702 countStr = "" 703 } 704 705 diff.Attributes[k+".#"] = &terraform.ResourceAttrDiff{ 706 Old: countStr, 707 NewComputed: true, 708 } 709 return nil 710 } 711 712 // If the counts are not the same, then record that diff 713 changed := oldLen != newLen 714 if changed || all { 715 countSchema := &Schema{ 716 Type: TypeInt, 717 Computed: schema.Computed, 718 ForceNew: schema.ForceNew, 719 } 720 721 diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{ 722 Old: oldStr, 723 New: newStr, 724 }) 725 } 726 727 for _, code := range ns.listCode() { 728 switch t := schema.Elem.(type) { 729 case *Resource: 730 // This is a complex resource 731 for k2, schema := range t.Schema { 732 subK := fmt.Sprintf("%s.%d.%s", k, code, k2) 733 subK = strings.Replace(subK, "-", "~", -1) 734 err := m.diff(subK, schema, diff, d, true) 735 if err != nil { 736 return err 737 } 738 } 739 case *Schema: 740 // Copy the schema so that we can set Computed/ForceNew from 741 // the parent schema (the TypeSet). 742 t2 := *t 743 t2.ForceNew = schema.ForceNew 744 745 // This is just a primitive element, so go through each and 746 // just diff each. 747 subK := fmt.Sprintf("%s.%d", k, code) 748 subK = strings.Replace(subK, "-", "~", -1) 749 err := m.diff(subK, &t2, diff, d, true) 750 if err != nil { 751 return err 752 } 753 default: 754 return fmt.Errorf("%s: unknown element type (internal)", k) 755 } 756 } 757 758 return nil 759 } 760 761 func (m schemaMap) diffString( 762 k string, 763 schema *Schema, 764 diff *terraform.InstanceDiff, 765 d *ResourceData, 766 all bool) error { 767 var originalN interface{} 768 var os, ns string 769 o, n, _, _ := d.diffChange(k) 770 if n == nil { 771 n = schema.Default 772 if schema.DefaultFunc != nil { 773 var err error 774 n, err = schema.DefaultFunc() 775 if err != nil { 776 return fmt.Errorf("%s, error loading default: %s", k, err) 777 } 778 } 779 } 780 if schema.StateFunc != nil { 781 originalN = n 782 n = schema.StateFunc(n) 783 } 784 if err := mapstructure.WeakDecode(o, &os); err != nil { 785 return fmt.Errorf("%s: %s", k, err) 786 } 787 if err := mapstructure.WeakDecode(n, &ns); err != nil { 788 return fmt.Errorf("%s: %s", k, err) 789 } 790 791 if os == ns && !all { 792 // They're the same value. If there old value is not blank or we 793 // have an ID, then return right away since we're already setup. 794 if os != "" || d.Id() != "" { 795 return nil 796 } 797 798 // Otherwise, only continue if we're computed 799 if !schema.Computed { 800 return nil 801 } 802 } 803 804 removed := false 805 if o != nil && n == nil { 806 removed = true 807 } 808 if removed && schema.Computed { 809 return nil 810 } 811 812 diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{ 813 Old: os, 814 New: ns, 815 NewExtra: originalN, 816 NewRemoved: removed, 817 }) 818 819 return nil 820 } 821 822 func (m schemaMap) inputString( 823 input terraform.UIInput, 824 k string, 825 schema *Schema) (interface{}, error) { 826 result, err := input.Input(&terraform.InputOpts{ 827 Id: k, 828 Query: k, 829 Description: schema.Description, 830 Default: schema.InputDefault, 831 }) 832 833 return result, err 834 } 835 836 func (m schemaMap) validate( 837 k string, 838 schema *Schema, 839 c *terraform.ResourceConfig) ([]string, []error) { 840 raw, ok := c.Get(k) 841 if !ok && schema.DefaultFunc != nil { 842 // We have a dynamic default. Check if we have a value. 843 var err error 844 raw, err = schema.DefaultFunc() 845 if err != nil { 846 return nil, []error{fmt.Errorf( 847 "%s, error loading default: %s", k, err)} 848 } 849 850 // We're okay as long as we had a value set 851 ok = raw != nil 852 } 853 if !ok { 854 if schema.Required { 855 return nil, []error{fmt.Errorf( 856 "%s: required field is not set", k)} 857 } 858 859 return nil, nil 860 } 861 862 if !schema.Required && !schema.Optional { 863 // This is a computed-only field 864 return nil, []error{fmt.Errorf( 865 "%s: this field cannot be set", k)} 866 } 867 868 return m.validatePrimitive(k, raw, schema, c) 869 } 870 871 func (m schemaMap) validateList( 872 k string, 873 raw interface{}, 874 schema *Schema, 875 c *terraform.ResourceConfig) ([]string, []error) { 876 // We use reflection to verify the slice because you can't 877 // case to []interface{} unless the slice is exactly that type. 878 rawV := reflect.ValueOf(raw) 879 if rawV.Kind() != reflect.Slice { 880 return nil, []error{fmt.Errorf( 881 "%s: should be a list", k)} 882 } 883 884 // Now build the []interface{} 885 raws := make([]interface{}, rawV.Len()) 886 for i, _ := range raws { 887 raws[i] = rawV.Index(i).Interface() 888 } 889 890 var ws []string 891 var es []error 892 for i, raw := range raws { 893 key := fmt.Sprintf("%s.%d", k, i) 894 895 var ws2 []string 896 var es2 []error 897 switch t := schema.Elem.(type) { 898 case *Resource: 899 // This is a sub-resource 900 ws2, es2 = m.validateObject(key, t.Schema, c) 901 case *Schema: 902 // This is some sort of primitive 903 ws2, es2 = m.validatePrimitive(key, raw, t, c) 904 } 905 906 if len(ws2) > 0 { 907 ws = append(ws, ws2...) 908 } 909 if len(es2) > 0 { 910 es = append(es, es2...) 911 } 912 } 913 914 return ws, es 915 } 916 917 func (m schemaMap) validateMap( 918 k string, 919 raw interface{}, 920 schema *Schema, 921 c *terraform.ResourceConfig) ([]string, []error) { 922 // We use reflection to verify the slice because you can't 923 // case to []interface{} unless the slice is exactly that type. 924 rawV := reflect.ValueOf(raw) 925 switch rawV.Kind() { 926 case reflect.Map: 927 case reflect.Slice: 928 default: 929 return nil, []error{fmt.Errorf( 930 "%s: should be a map", k)} 931 } 932 933 // If it is not a slice, it is valid 934 if rawV.Kind() != reflect.Slice { 935 return nil, nil 936 } 937 938 // It is a slice, verify that all the elements are maps 939 raws := make([]interface{}, rawV.Len()) 940 for i, _ := range raws { 941 raws[i] = rawV.Index(i).Interface() 942 } 943 944 for _, raw := range raws { 945 v := reflect.ValueOf(raw) 946 if v.Kind() != reflect.Map { 947 return nil, []error{fmt.Errorf( 948 "%s: should be a map", k)} 949 } 950 } 951 952 return nil, nil 953 } 954 955 func (m schemaMap) validateObject( 956 k string, 957 schema map[string]*Schema, 958 c *terraform.ResourceConfig) ([]string, []error) { 959 var ws []string 960 var es []error 961 for subK, s := range schema { 962 key := subK 963 if k != "" { 964 key = fmt.Sprintf("%s.%s", k, subK) 965 } 966 967 ws2, es2 := m.validate(key, s, c) 968 if len(ws2) > 0 { 969 ws = append(ws, ws2...) 970 } 971 if len(es2) > 0 { 972 es = append(es, es2...) 973 } 974 } 975 976 // Detect any extra/unknown keys and report those as errors. 977 prefix := k + "." 978 for configK, _ := range c.Raw { 979 if k != "" { 980 if !strings.HasPrefix(configK, prefix) { 981 continue 982 } 983 984 configK = configK[len(prefix):] 985 } 986 987 if _, ok := schema[configK]; !ok { 988 es = append(es, fmt.Errorf( 989 "%s: invalid or unknown key: %s", k, configK)) 990 } 991 } 992 993 return ws, es 994 } 995 996 func (m schemaMap) validatePrimitive( 997 k string, 998 raw interface{}, 999 schema *Schema, 1000 c *terraform.ResourceConfig) ([]string, []error) { 1001 if c.IsComputed(k) { 1002 // If the key is being computed, then it is not an error 1003 return nil, nil 1004 } 1005 1006 switch schema.Type { 1007 case TypeSet: 1008 fallthrough 1009 case TypeList: 1010 return m.validateList(k, raw, schema, c) 1011 case TypeMap: 1012 return m.validateMap(k, raw, schema, c) 1013 case TypeBool: 1014 // Verify that we can parse this as the correct type 1015 var n bool 1016 if err := mapstructure.WeakDecode(raw, &n); err != nil { 1017 return nil, []error{err} 1018 } 1019 case TypeInt: 1020 // Verify that we can parse this as an int 1021 var n int 1022 if err := mapstructure.WeakDecode(raw, &n); err != nil { 1023 return nil, []error{err} 1024 } 1025 case TypeString: 1026 // Verify that we can parse this as a string 1027 var n string 1028 if err := mapstructure.WeakDecode(raw, &n); err != nil { 1029 return nil, []error{err} 1030 } 1031 default: 1032 panic(fmt.Sprintf("Unknown validation type: %#v", schema.Type)) 1033 } 1034 1035 return nil, nil 1036 }