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