2014-08-13 23:23:22 +02:00
|
|
|
package schema
|
|
|
|
|
2014-08-15 04:55:47 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// The fields below relate to diffs: if Computed is true, then the
|
|
|
|
// result of this value is computed (unless specified by config).
|
|
|
|
// If ForceNew is true
|
|
|
|
Computed bool
|
|
|
|
ForceNew bool
|
|
|
|
|
|
|
|
// 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-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
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Computed && 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 nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("%s: required field not set", k)
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
// We don't have a configuration value.
|
|
|
|
if !schema.Computed {
|
|
|
|
return nil
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
2014-08-15 08:17:53 +02:00
|
|
|
}
|
2014-08-15 04:55:47 +02:00
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
vs, ok := v.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%s: must be a list", k)
|
2014-08-15 04:55:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 08:17:53 +02:00
|
|
|
// Diff the count no matter what
|
2014-08-15 08:32:20 +02:00
|
|
|
countSchema := &Schema{
|
|
|
|
Type: TypeInt,
|
|
|
|
ForceNew: schema.ForceNew,
|
|
|
|
}
|
|
|
|
m.diffString(k+".#", countSchema, diff, s, c)
|
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.Computed = schema.Computed
|
|
|
|
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.
|
|
|
|
for i, _ := range vs {
|
|
|
|
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-15 19:25:25 +02:00
|
|
|
for i, _ := range vs {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have a configuration value.
|
2014-08-15 08:17:53 +02:00
|
|
|
if !schema.Computed {
|
|
|
|
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
|
|
|
}
|