2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
2014-08-15 04:55:47 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-18 23:20:57 +02:00
|
|
|
"reflect"
|
2014-08-19 01:54:30 +02:00
|
|
|
"strconv"
|
2014-08-16 07:15:10 +02:00
|
|
|
"strings"
|
2014-08-15 04:55:47 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2014-08-13 23:23:22 +02:00
|
|
|
// ValueType is an enum of the type that can be represented by a schema.
|
|
|
|
type ValueType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
TypeInvalid ValueType = iota
|
2014-08-15 05:02:52 +02:00
|
|
|
TypeBool
|
2014-08-13 23:23:22 +02:00
|
|
|
TypeInt
|
|
|
|
TypeString
|
|
|
|
TypeList
|
2014-08-18 23:00:03 +02:00
|
|
|
TypeMap
|
2014-08-13 23:23:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Schema is used to describe the structure of a value.
|
|
|
|
type Schema struct {
|
|
|
|
// Type is the type of the value and must be one of the ValueType values.
|
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.
|
|
|
|
Optional bool
|
|
|
|
Required bool
|
|
|
|
|
2014-08-16 18:49:22 +02:00
|
|
|
// The fields below relate to diffs.
|
|
|
|
//
|
|
|
|
// If Computed is true, then the result of this value is computed
|
|
|
|
// (unless specified by config) on creation.
|
|
|
|
//
|
|
|
|
// If ForceNew is true, then a change in this resource necessitates
|
|
|
|
// the creation of a new resource.
|
2014-08-13 23:23:22 +02:00
|
|
|
Computed bool
|
|
|
|
ForceNew bool
|
|
|
|
|
2014-08-16 01:32:43 +02:00
|
|
|
// The following fields are only set for a TypeList 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{}
|
2014-08-16 18:49:22 +02:00
|
|
|
|
|
|
|
// ComputedWhen is a set of queries on the configuration. Whenever any
|
|
|
|
// of these things is changed, it will require a recompute (this requires
|
|
|
|
// that Computed is set to true).
|
|
|
|
ComputedWhen []string
|
2014-08-13 23:23:22 +02:00
|
|
|
}
|
2014-08-15 04:55:47 +02:00
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
func (s *Schema) finalizeDiff(
|
|
|
|
d *terraform.ResourceAttrDiff) *terraform.ResourceAttrDiff {
|
|
|
|
if d == nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:49:22 +02:00
|
|
|
if s.Computed {
|
|
|
|
if d.Old != "" && d.New == "" {
|
|
|
|
// This is a computed value with an old value set already,
|
|
|
|
// just let it go.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.New == "" {
|
|
|
|
// Computed attribute without a new value set
|
|
|
|
d.NewComputed = true
|
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-08-16 01:32:43 +02:00
|
|
|
return &ResourceData{
|
|
|
|
schema: m,
|
|
|
|
state: s,
|
|
|
|
diff: d,
|
|
|
|
}, nil
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Diff returns the diff for a resource given the schema map,
|
|
|
|
// state, and configuration.
|
|
|
|
func (m schemaMap) Diff(
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig) (*terraform.ResourceDiff, error) {
|
|
|
|
result := new(terraform.ResourceDiff)
|
|
|
|
result.Attributes = make(map[string]*terraform.ResourceAttrDiff)
|
|
|
|
|
|
|
|
for k, schema := range m {
|
2014-08-15 08:17:53 +02:00
|
|
|
err := m.diff(k, schema, result, s, c)
|
2014-08-15 04:55:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
|
|
|
|
2014-08-16 22:32:21 +02:00
|
|
|
// Remove any nil diffs just to keep things clean
|
2014-08-16 18:49:22 +02:00
|
|
|
for k, v := range result.Attributes {
|
|
|
|
if v == nil {
|
|
|
|
delete(result.Attributes, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 22:32:21 +02:00
|
|
|
// Go through and detect all of the ComputedWhens now that we've
|
|
|
|
// finished the diff.
|
|
|
|
// TODO
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
if result.Empty() {
|
|
|
|
// If we don't have any diff elements, just return nil
|
|
|
|
return nil, nil
|
|
|
|
}
|
2014-08-15 04:55:47 +02:00
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:00:16 +02:00
|
|
|
// Validate validates the configuration against this schema mapping.
|
|
|
|
func (m schemaMap) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
2014-08-16 07:15:10 +02:00
|
|
|
return m.validateObject("", m, c)
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 23:50:44 +02:00
|
|
|
// InternalValidate validates the format of this schema. This should be called
|
|
|
|
// from a unit test (and not in user-path code) to verify that a schema
|
|
|
|
// is properly built.
|
|
|
|
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 len(v.ComputedWhen) > 0 && !v.Computed {
|
|
|
|
return fmt.Errorf("%s: ComputedWhen can only be set with Computed", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Type == TypeList {
|
|
|
|
if v.Elem == nil {
|
|
|
|
return fmt.Errorf("%s: Elem must be set for lists", 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
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
func (m schemaMap) diff(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
|
|
|
diff *terraform.ResourceDiff,
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig) error {
|
|
|
|
var err error
|
|
|
|
switch schema.Type {
|
|
|
|
case TypeBool:
|
|
|
|
fallthrough
|
|
|
|
case TypeInt:
|
|
|
|
fallthrough
|
|
|
|
case TypeString:
|
|
|
|
err = m.diffString(k, schema, diff, s, c)
|
|
|
|
case TypeList:
|
|
|
|
err = m.diffList(k, schema, diff, s, c)
|
2014-08-19 00:07:09 +02:00
|
|
|
case TypeMap:
|
|
|
|
err = m.diffMap(k, schema, diff, s, c)
|
2014-08-15 08:17:53 +02:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("%s: unknown type %s", k, schema.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m schemaMap) diffList(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
|
|
|
diff *terraform.ResourceDiff,
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig) error {
|
2014-08-19 01:54:30 +02:00
|
|
|
var vs []interface{}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
v, ok := c.Get(k)
|
2014-08-19 01:54:30 +02:00
|
|
|
if ok {
|
|
|
|
// We have to use reflection to build the []interface{} list
|
|
|
|
rawV := reflect.ValueOf(v)
|
|
|
|
if rawV.Kind() != reflect.Slice {
|
|
|
|
return fmt.Errorf("%s: must be a list", k)
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
2014-08-19 01:54:30 +02:00
|
|
|
vs = make([]interface{}, rawV.Len())
|
|
|
|
for i, _ := range vs {
|
|
|
|
vs[i] = rawV.Index(i).Interface()
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
2014-08-18 23:24:04 +02:00
|
|
|
}
|
2014-08-15 04:55:47 +02:00
|
|
|
|
2014-08-15 19:39:40 +02:00
|
|
|
// If this field is required, then it must also be non-empty
|
|
|
|
if len(vs) == 0 && schema.Required {
|
|
|
|
return fmt.Errorf("%s: required field is not set", k)
|
|
|
|
}
|
|
|
|
|
2014-08-19 01:54:30 +02:00
|
|
|
// Get the counts
|
|
|
|
var oldLen, newLen int
|
|
|
|
if s != nil {
|
|
|
|
if v, ok := s.Attributes[k+".#"]; ok {
|
|
|
|
old64, err := strconv.ParseInt(v, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldLen = int(old64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newLen = len(vs)
|
|
|
|
|
|
|
|
// If the counts are not the same, then record that diff
|
2014-08-19 06:22:27 +02:00
|
|
|
changed := oldLen != newLen
|
|
|
|
computed := oldLen == 0 && newLen == 0 && schema.Computed
|
|
|
|
if changed || computed {
|
2014-08-19 01:54:30 +02:00
|
|
|
countSchema := &Schema{
|
|
|
|
Type: TypeInt,
|
2014-08-19 06:22:27 +02:00
|
|
|
Computed: schema.Computed,
|
2014-08-19 01:54:30 +02:00
|
|
|
ForceNew: schema.ForceNew,
|
|
|
|
}
|
|
|
|
|
2014-08-19 06:22:27 +02:00
|
|
|
oldStr := ""
|
|
|
|
newStr := ""
|
|
|
|
if !computed {
|
|
|
|
oldStr = strconv.FormatInt(int64(oldLen), 10)
|
|
|
|
newStr = strconv.FormatInt(int64(newLen), 10)
|
|
|
|
}
|
|
|
|
|
2014-08-19 01:54:30 +02:00
|
|
|
diff.Attributes[k+".#"] = countSchema.finalizeDiff(&terraform.ResourceAttrDiff{
|
2014-08-19 06:22:27 +02:00
|
|
|
Old: oldStr,
|
|
|
|
New: newStr,
|
2014-08-19 01:54:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the maximum
|
|
|
|
maxLen := oldLen
|
|
|
|
if newLen > maxLen {
|
|
|
|
maxLen = newLen
|
2014-08-15 08:32:20 +02:00
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Schema:
|
2014-08-15 08:33:30 +02:00
|
|
|
// Copy the schema so that we can set Computed/ForceNew from
|
|
|
|
// the parent schema (the TypeList).
|
2014-08-15 08:32:20 +02:00
|
|
|
t2 := *t
|
|
|
|
t2.ForceNew = schema.ForceNew
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
// This is just a primitive element, so go through each and
|
|
|
|
// just diff each.
|
2014-08-19 01:54:30 +02:00
|
|
|
for i := 0; i < maxLen; i++ {
|
2014-08-15 08:17:53 +02:00
|
|
|
subK := fmt.Sprintf("%s.%d", k, i)
|
2014-08-15 08:32:20 +02:00
|
|
|
err := m.diff(subK, &t2, diff, s, c)
|
2014-08-15 08:17:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *Resource:
|
|
|
|
// This is a complex resource
|
2014-08-19 01:54:30 +02:00
|
|
|
for i := 0; i < maxLen; i++ {
|
2014-08-15 19:25:25 +02:00
|
|
|
for k2, schema := range t.Schema {
|
|
|
|
subK := fmt.Sprintf("%s.%d.%s", k, i, k2)
|
|
|
|
err := m.diff(subK, schema, diff, s, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
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,
|
|
|
|
s *terraform.ResourceState,
|
|
|
|
c *terraform.ResourceConfig) error {
|
|
|
|
//elemSchema := &Schema{Type: TypeString}
|
|
|
|
prefix := k + "."
|
|
|
|
|
|
|
|
// First get all the values from the state
|
|
|
|
stateMap := make(map[string]string)
|
|
|
|
if s != nil {
|
|
|
|
for sk, sv := range s.Attributes {
|
|
|
|
if !strings.HasPrefix(sk, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stateMap[sk[len(prefix):]] = sv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then get all the values from the configuration
|
|
|
|
configMap := make(map[string]string)
|
|
|
|
if c != nil {
|
|
|
|
if raw, ok := c.Get(k); ok {
|
|
|
|
for k, v := range raw.(map[string]interface{}) {
|
|
|
|
configMap[k] = v.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-15 04:55:47 +02:00
|
|
|
func (m schemaMap) diffString(
|
|
|
|
k string,
|
|
|
|
schema *Schema,
|
2014-08-15 08:17:53 +02:00
|
|
|
diff *terraform.ResourceDiff,
|
2014-08-15 04:55:47 +02:00
|
|
|
s *terraform.ResourceState,
|
2014-08-15 08:17:53 +02:00
|
|
|
c *terraform.ResourceConfig) error {
|
2014-08-15 04:55:47 +02:00
|
|
|
var old, n string
|
|
|
|
if s != nil {
|
|
|
|
old = s.Attributes[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
v, ok := c.Get(k)
|
|
|
|
if !ok {
|
|
|
|
// We don't have a value, if it is required then it is an error
|
|
|
|
if schema.Required {
|
2014-08-15 08:17:53 +02:00
|
|
|
return fmt.Errorf("%s: required field not set", k)
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 01:54:30 +02:00
|
|
|
// If we don't have an old value, just return
|
|
|
|
if old == "" && !schema.Computed {
|
2014-08-15 08:17:53 +02:00
|
|
|
return nil
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := mapstructure.WeakDecode(v, &n); err != nil {
|
2014-08-15 08:17:53 +02:00
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if old == n {
|
|
|
|
// They're the same value
|
2014-08-15 08:17:53 +02:00
|
|
|
return nil
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
|
|
|
|
Old: old,
|
|
|
|
New: n,
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m schemaMap) diffPrimitive(
|
|
|
|
k string,
|
|
|
|
nraw interface{},
|
|
|
|
schema *Schema,
|
|
|
|
diff *terraform.ResourceDiff,
|
|
|
|
s *terraform.ResourceState) error {
|
|
|
|
var old, n string
|
|
|
|
if s != nil {
|
|
|
|
old = s.Attributes[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(nraw, &n); err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if old == n {
|
|
|
|
// They're the same value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
diff.Attributes[k] = schema.finalizeDiff(&terraform.ResourceAttrDiff{
|
|
|
|
Old: old,
|
|
|
|
New: n,
|
|
|
|
})
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if schema.Required {
|
|
|
|
return nil, []error{fmt.Errorf(
|
2014-08-16 18:18:45 +02:00
|
|
|
"%s: required field is not set", k)}
|
2014-08-16 07:00:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-08-16 18:18:45 +02:00
|
|
|
if !schema.Required && !schema.Optional {
|
|
|
|
// This is a computed-only field
|
|
|
|
return nil, []error{fmt.Errorf(
|
|
|
|
"%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) {
|
2014-08-18 23:20:57 +02:00
|
|
|
// We use reflection to verify the slice because you can't
|
|
|
|
// case to []interface{} unless the slice is exactly that type.
|
|
|
|
rawV := reflect.ValueOf(raw)
|
|
|
|
if rawV.Kind() != reflect.Slice {
|
2014-08-16 07:00:16 +02:00
|
|
|
return nil, []error{fmt.Errorf(
|
2014-08-18 23:20:57 +02:00
|
|
|
"%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 {
|
2014-08-16 07:15:10 +02:00
|
|
|
key := subK
|
|
|
|
if k != "" {
|
|
|
|
key = fmt.Sprintf("%s.%s", k, subK)
|
|
|
|
}
|
2014-08-16 07:00:16 +02:00
|
|
|
|
2014-08-16 07:15:10 +02:00
|
|
|
ws2, es2 := m.validate(key, s, c)
|
2014-08-16 07:00:16 +02:00
|
|
|
if len(ws2) > 0 {
|
|
|
|
ws = append(ws, ws2...)
|
|
|
|
}
|
|
|
|
if len(es2) > 0 {
|
|
|
|
es = append(es, es2...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 07:15:10 +02:00
|
|
|
// Detect any extra/unknown keys and report those as errors.
|
|
|
|
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
|
|
|
|
}
|