terraform/helper/schema/schema.go

712 lines
17 KiB
Go
Raw Normal View History

2014-08-27 06:52:09 +02:00
// schema is a high-level framework for easily writing new providers
// for Terraform. Usage of schema is recommended over attempting to write
// to the low-level plugin interfaces manually.
//
// schema breaks down provider creation into simple CRUD operations for
// resources. The logic of diffing, destroying before creating, updating
// or creating, etc. is all handled by the framework. The plugin author
// only needs to implement a configuration schema and the CRUD operations and
// everything else is meant to just work.
//
// A good starting point is to view the Provider structure.
2014-08-13 23:23:22 +02:00
package schema
2014-08-15 04:55:47 +02:00
import (
"fmt"
"reflect"
"strconv"
"strings"
2014-08-15 04:55:47 +02:00
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure"
)
2014-08-13 23:23:22 +02:00
// ValueType is an enum of the type that can be represented by a schema.
type ValueType int
const (
TypeInvalid ValueType = iota
TypeBool
2014-08-13 23:23:22 +02:00
TypeInt
TypeString
TypeList
2014-08-18 23:00:03 +02:00
TypeMap
TypeSet
2014-08-13 23:23:22 +02:00
)
// Schema is used to describe the structure of a value.
2014-08-27 06:52:09 +02:00
//
// Read the documentation of the struct elements for important details.
2014-08-13 23:23:22 +02:00
type Schema struct {
// Type is the type of the value and must be one of the ValueType values.
2014-08-27 06:52:09 +02:00
//
// This type not only determines what type is expected/valid in configuring
// this value, but also what type is returned when ResourceData.Get is
// called. The types returned by Get are:
//
// TypeBool - bool
// TypeInt - int
// TypeString - string
// TypeList - []interface{}
// TypeMap - map[string]interface{}
// TypeSet - *schema.Set
//
2014-08-15 04:55:47 +02:00
Type ValueType
2014-08-13 23:23:22 +02:00
// If one of these is set, then this item can come from the configuration.
// Both cannot be set. If Optional is set, the value is optional. If
// Required is set, the value is required.
2014-08-25 01:53:27 +02:00
//
// One of these must be set if the value is not computed. That is:
// value either comes from the config, is computed, or is both.
2014-08-13 23:23:22 +02:00
Optional bool
Required bool
2014-09-10 06:17:29 +02:00
// If this is non-nil, then this will be a default value that is used
// when this item is not set in the configuration/state.
//
// DefaultFunc can be specified if you want a dynamic default value.
// Only one of Default or DefaultFunc can be set.
//
// If Required is true above, then Default cannot be set. DefaultFunc
// can be set with Required. If the DefaultFunc returns nil, then there
// will no default and the user will be asked to fill it in.
Default interface{}
DefaultFunc SchemaDefaultFunc
2014-09-10 06:17:29 +02:00
// The fields below relate to diffs.
//
// If Computed is true, then the result of this value is computed
// (unless specified by config) on creation.
//
// If ForceNew is true, then a change in this resource necessitates
// the creation of a new resource.
//
// StateFunc is a function called to change the value of this before
// storing it in the state (and likewise before comparing for diffs).
// The use for this is for example with large strings, you may want
// to simply store the hash of it.
Computed bool
ForceNew bool
StateFunc SchemaStateFunc
2014-08-13 23:23:22 +02:00
// The following fields are only set for a TypeList or TypeSet Type.
//
2014-08-13 23:23:22 +02:00
// Elem must be either a *Schema or a *Resource only if the Type is
// TypeList, and represents what the element type is. If it is *Schema,
// the element type is just a simple value. If it is *Resource, the
// element type is a complex structure, potentially with its own lifecycle.
Elem interface{}
// The follow fields are only valid for a TypeSet type.
//
// Set defines a function to determine the unique ID of an item so that
// a proper set can be built.
Set SchemaSetFunc
// ComputedWhen is a set of queries on the configuration. Whenever any
// of these things is changed, it will require a recompute (this requires
// that Computed is set to true).
2014-08-27 06:52:09 +02:00
//
// NOTE: This currently does not work.
ComputedWhen []string
2014-08-13 23:23:22 +02:00
}
2014-08-15 04:55:47 +02:00
// SchemaDefaultFunc is a function called to return a default value for
// a field.
type SchemaDefaultFunc func() (interface{}, error)
// SchemaSetFunc is a function that must return a unique ID for the given
// element. This unique ID is used to store the element in a hash.
type SchemaSetFunc func(interface{}) int
// SchemaStateFunc is a function used to convert some type to a string
// to be stored in the state.
type SchemaStateFunc func(interface{}) string
func (s *Schema) GoString() string {
return fmt.Sprintf("*%#v", *s)
}
func (s *Schema) finalizeDiff(
d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff {
if d == nil {
return d
}
if s.Computed {
if d.Old != "" && d.New == "" {
// This is a computed value with an old value set already,
// just let it go.
return nil
}
if d.New == "" {
// Computed attribute without a new value set
d.NewComputed = true
}
}
if s.ForceNew {
// Force new, set it to true in the diff
d.RequiresNew = true
}
return d
}
2014-08-15 04:55:47 +02:00
// schemaMap is a wrapper that adds nice functions on top of schemas.
type schemaMap map[string]*Schema
// Data returns a ResourceData for the given schema, state, and diff.
//
// The diff is optional.
func (m schemaMap) Data(
s *terraform.ResourceState,
d *terraform.ResourceDiff) (*ResourceData, error) {
return &ResourceData{
schema: m,
state: s,
diff: d,
}, nil
2014-08-15 04:55:47 +02:00
}
// Diff returns the diff for a resource given the schema map,
// state, and configuration.
func (m schemaMap) Diff(
s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
result := new(terraform.ResourceDiff)
result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
d := &ResourceData{
schema: m,
state: s,
config: c,
diffing: true,
}
2014-08-15 04:55:47 +02:00
for k, schema := range m {
err := m.diff(k, schema, result, d)
2014-08-15 04:55:47 +02:00
if err != nil {
return nil, err
}
}
// If the diff requires a new resource, then we recompute the diff
// so we have the complete new resource diff, and preserve the
// RequiresNew fields where necessary so the user knows exactly what
// caused that.
if result.RequiresNew() {
// Create the new diff
result2 := new(terraform.ResourceDiff)
result2.Attributes = make(map[string]*terraform.ResourceAttrDiff)
// Reset the data to not contain state
d.state = nil
// Perform the diff again
for k, schema := range m {
err := m.diff(k, schema, result2, d)
if err != nil {
return nil, err
}
}
// Force all the fields to not force a new since we know what we
// want to force new.
for k, attr := range result2.Attributes {
if attr == nil {
continue
}
if attr.RequiresNew {
attr.RequiresNew = false
}
if s != nil {
attr.Old = s.Attributes[k]
}
}
// Now copy in all the requires new diffs...
for k, attr := range result.Attributes {
if attr == nil {
continue
}
newAttr, ok := result2.Attributes[k]
if !ok {
newAttr = attr
}
if attr.RequiresNew {
newAttr.RequiresNew = true
}
result2.Attributes[k] = newAttr
}
// And set the diff!
result = result2
}
// Remove any nil diffs just to keep things clean
for k, v := range result.Attributes {
if v == nil {
delete(result.Attributes, k)
}
}
// Go through and detect all of the ComputedWhens now that we've
// finished the diff.
// TODO
if result.Empty() {
// If we don't have any diff elements, just return nil
return nil, nil
}
2014-08-15 04:55:47 +02:00
return result, nil
}
2014-08-16 07:00:16 +02:00
// Validate validates the configuration against this schema mapping.
func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
return m.validateObject("", m, c)
2014-08-16 07:00:16 +02:00
}
// InternalValidate validates the format of this schema. This should be called
// from a unit test (and not in user-path code) to verify that a schema
// is properly built.
func (m schemaMap) InternalValidate() error {
for k, v := range m {
if v.Type == TypeInvalid {
return fmt.Errorf("%s: Type must be specified", k)
}
if v.Optional && v.Required {
return fmt.Errorf("%s: Optional or Required must be set, not both", k)
}
if v.Required && v.Computed {
return fmt.Errorf("%s: Cannot be both Required and Computed", k)
}
if !v.Required && !v.Optional && !v.Computed {
return fmt.Errorf("%s: One of optional, required, or computed must be set", k)
}
2014-09-10 06:17:29 +02:00
if v.Computed && v.Default != nil {
return fmt.Errorf("%s: Default must be nil if computed", k)
}
if v.Required && v.Default != nil {
return fmt.Errorf("%s: Default cannot be set with Required", k)
}
if len(v.ComputedWhen) > 0 && !v.Computed {
return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
}
if v.Type == TypeList || v.Type == TypeSet {
if v.Elem == nil {
return fmt.Errorf("%s: Elem must be set for lists", k)
}
2014-09-10 06:17:29 +02:00
if v.Default != nil {
return fmt.Errorf("%s: Default is not valid for lists or sets", k)
}
if v.Type == TypeList && v.Set != nil {
return fmt.Errorf("%s: Set can only be set for TypeSet", k)
} else if v.Type == TypeSet && v.Set == nil {
return fmt.Errorf("%s: Set must be set", k)
}
switch t := v.Elem.(type) {
case *Resource:
if err := t.InternalValidate(); err != nil {
return err
}
case *Schema:
bad := t.Computed || t.Optional || t.Required
if bad {
return fmt.Errorf(
"%s: Elem must have only Type set", k)
}
}
}
}
return nil
}
func (m schemaMap) diff(
k string,
schema *Schema,
diff *terraform.ResourceDiff,
d *ResourceData) error {
var err error
switch schema.Type {
case TypeBool:
fallthrough
case TypeInt:
fallthrough
case TypeString:
err = m.diffString(k, schema, diff, d)
case TypeList:
err = m.diffList(k, schema, diff, d)
2014-08-19 00:07:09 +02:00
case TypeMap:
err = m.diffMap(k, schema, diff, d)
2014-08-21 03:30:28 +02:00
case TypeSet:
err = m.diffSet(k, schema, diff, d)
default:
2014-08-25 06:50:35 +02:00
err = fmt.Errorf("%s: unknown type %#v", k, schema.Type)
}
return err
}
func (m schemaMap) diffList(
k string,
schema *Schema,
diff *terraform.ResourceDiff,
d *ResourceData) error {
o, n, _ := d.diffChange(k)
// If we have an old value, but no new value set but we're computed,
// then nothing has changed.
if o != nil && n == nil && schema.Computed {
return nil
}
if o == nil {
o = []interface{}{}
}
if n == nil {
n = []interface{}{}
}
2014-08-21 06:02:42 +02:00
if s, ok := o.(*Set); ok {
o = s.List()
}
if s, ok := n.(*Set); ok {
n = s.List()
}
os := o.([]interface{})
vs := n.([]interface{})
2014-08-15 19:39:40 +02:00
// Get the counts
oldLen := len(os)
newLen := len(vs)
// If the counts are not the same, then record that diff
changed := oldLen != newLen
computed := oldLen == 0 && newLen == 0 && schema.Computed
if changed || computed {
countSchema := &Schema{
Type: TypeInt,
Computed: schema.Computed,
ForceNew: schema.ForceNew,
}
oldStr := ""
newStr := ""
if !computed {
oldStr = strconv.FormatInt(int64(oldLen), 10)
newStr = strconv.FormatInt(int64(newLen), 10)
}
diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
Old: oldStr,
New: newStr,
})
}
// Figure out the maximum
maxLen := oldLen
if newLen > maxLen {
maxLen = newLen
2014-08-15 08:32:20 +02:00
}
switch t := schema.Elem.(type) {
case *Schema:
2014-08-15 08:33:30 +02:00
// Copy the schema so that we can set Computed/ForceNew from
// the parent schema (the TypeList).
2014-08-15 08:32:20 +02:00
t2 := *t
t2.ForceNew = schema.ForceNew
// This is just a primitive element, so go through each and
// just diff each.
for i := 0; i < maxLen; i++ {
subK := fmt.Sprintf("%s.%d", k, i)
err := m.diff(subK, &t2, diff, d)
if err != nil {
return err
}
}
case *Resource:
// This is a complex resource
for i := 0; i < maxLen; i++ {
for k2, schema := range t.Schema {
subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
err := m.diff(subK, schema, diff, d)
if err != nil {
return err
}
}
}
default:
return fmt.Errorf("%s: unknown element type (internal)", k)
}
return nil
2014-08-15 04:55:47 +02:00
}
2014-08-19 00:07:09 +02:00
func (m schemaMap) diffMap(
k string,
schema *Schema,
diff *terraform.ResourceDiff,
d *ResourceData) error {
2014-08-19 00:07:09 +02:00
//elemSchema := &Schema{Type: TypeString}
prefix := k + "."
// First get all the values from the state
var stateMap, configMap map[string]string
o, n, _ := d.diffChange(k)
if err := mapstructure.WeakDecode(o, &stateMap); err != nil {
return fmt.Errorf("%s: %s", k, err)
2014-08-19 00:07:09 +02:00
}
if err := mapstructure.WeakDecode(n, &configMap); err != nil {
return fmt.Errorf("%s: %s", k, err)
2014-08-19 00:07:09 +02:00
}
// Now we compare, preferring values from the config map
for k, v := range configMap {
old := stateMap[k]
delete(stateMap, k)
if old == v {
continue
}
diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
Old: old,
New: v,
})
}
for k, v := range stateMap {
diff.Attributes[prefix+k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
Old: v,
NewRemoved: true,
})
}
return nil
}
2014-08-21 03:30:28 +02:00
func (m schemaMap) diffSet(
k string,
schema *Schema,
diff *terraform.ResourceDiff,
d *ResourceData) error {
2014-08-21 06:02:42 +02:00
return m.diffList(k, schema, diff, d)
2014-08-21 03:30:28 +02:00
}
2014-08-15 04:55:47 +02:00
func (m schemaMap) diffString(
k string,
schema *Schema,
diff *terraform.ResourceDiff,
d *ResourceData) error {
var originalN interface{}
var os, ns string
o, n, _ := d.diffChange(k)
2014-09-10 06:17:29 +02:00
if n == nil {
n = schema.Default
if schema.DefaultFunc != nil {
var err error
n, err = schema.DefaultFunc()
if err != nil {
return fmt.Errorf("%s, error loading default: %s", err)
}
}
2014-09-10 06:17:29 +02:00
}
if schema.StateFunc != nil {
originalN = n
n = schema.StateFunc(n)
}
if err := mapstructure.WeakDecode(o, &os); err != nil {
return fmt.Errorf("%s: %s", k, err)
}
if err := mapstructure.WeakDecode(n, &ns); err != nil {
return fmt.Errorf("%s: %s", k, err)
}
if os == ns {
// They're the same value. If there old value is not blank or we
// have an ID, then return right away since we're already setup.
if os != "" || d.Id() != "" {
return nil
}
// Otherwise, only continue if we're computed
if !schema.Computed {
return nil
}
}
2014-08-21 06:02:42 +02:00
removed := false
if o != nil && n == nil {
removed = true
}
diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
2014-08-21 06:02:42 +02:00
Old: os,
New: ns,
NewExtra: originalN,
2014-08-21 06:02:42 +02:00
NewRemoved: removed,
})
return nil
2014-08-15 04:55:47 +02:00
}
2014-08-16 07:00:16 +02:00
func (m schemaMap) validate(
k string,
schema *Schema,
c *terraform.ResourceConfig) ([]string, []error) {
raw, ok := c.Get(k)
if !ok && schema.DefaultFunc != nil {
// We have a dynamic default. Check if we have a value.
var err error
raw, err = schema.DefaultFunc()
if err != nil {
return nil, []error{fmt.Errorf(
"%s, error loading default: %s", k, err)}
}
// We're okay as long as we had a value set
ok = raw != nil
}
2014-08-16 07:00:16 +02:00
if !ok {
if schema.Required {
return nil, []error{fmt.Errorf(
"%s: required field is not set", k)}
2014-08-16 07:00:16 +02:00
}
return nil, nil
}
if !schema.Required && !schema.Optional {
// This is a computed-only field
return nil, []error{fmt.Errorf(
"%s: this field cannot be set", k)}
}
2014-08-16 07:00:16 +02:00
return m.validatePrimitive(k, raw, schema, c)
}
func (m schemaMap) validateList(
k string,
raw interface{},
schema *Schema,
c *terraform.ResourceConfig) ([]string, []error) {
// We use reflection to verify the slice because you can't
// case to []interface{} unless the slice is exactly that type.
rawV := reflect.ValueOf(raw)
if rawV.Kind() != reflect.Slice {
2014-08-16 07:00:16 +02:00
return nil, []error{fmt.Errorf(
"%s: should be a list", k)}
}
// Now build the []interface{}
raws := make([]interface{}, rawV.Len())
for i, _ := range raws {
raws[i] = rawV.Index(i).Interface()
2014-08-16 07:00:16 +02:00
}
var ws []string
var es []error
for i, raw := range raws {
key := fmt.Sprintf("%s.%d", k, i)
var ws2 []string
var es2 []error
switch t := schema.Elem.(type) {
case *Resource:
// This is a sub-resource
ws2, es2 = m.validateObject(key, t.Schema, c)
case *Schema:
// This is some sort of primitive
ws2, es2 = m.validatePrimitive(key, raw, t, c)
}
if len(ws2) > 0 {
ws = append(ws, ws2...)
}
if len(es2) > 0 {
es = append(es, es2...)
}
}
return ws, es
}
func (m schemaMap) validateObject(
k string,
schema map[string]*Schema,
c *terraform.ResourceConfig) ([]string, []error) {
var ws []string
var es []error
for subK, s := range schema {
key := subK
if k != "" {
key = fmt.Sprintf("%s.%s", k, subK)
}
2014-08-16 07:00:16 +02:00
ws2, es2 := m.validate(key, s, c)
2014-08-16 07:00:16 +02:00
if len(ws2) > 0 {
ws = append(ws, ws2...)
}
if len(es2) > 0 {
es = append(es, es2...)
}
}
// Detect any extra/unknown keys and report those as errors.
prefix := k + "."
for configK, _ := range c.Raw {
if k != "" {
if !strings.HasPrefix(configK, prefix) {
continue
}
configK = configK[len(prefix):]
}
if _, ok := schema[configK]; !ok {
es = append(es, fmt.Errorf(
"%s: invalid or unknown key: %s", k, configK))
}
}
2014-08-16 07:00:16 +02:00
return ws, es
}
func (m schemaMap) validatePrimitive(
k string,
raw interface{},
schema *Schema,
c *terraform.ResourceConfig) ([]string, []error) {
switch schema.Type {
case TypeList:
return m.validateList(k, raw, schema, c)
case TypeInt:
// Verify that we can parse this as an int
var n int
if err := mapstructure.WeakDecode(raw, &n); err != nil {
return nil, []error{err}
}
}
return nil, nil
}