2014-07-08 19:02:02 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-08 20:14:07 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2014-07-08 19:02:02 +02:00
|
|
|
|
2014-07-08 20:14:07 +02:00
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
2014-07-08 19:02:02 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Validator is a helper that helps you validate the configuration
|
|
|
|
// of your resource, resource provider, etc.
|
2014-07-08 20:14:07 +02:00
|
|
|
//
|
|
|
|
// At the most basic level, set the Required and Optional lists to be
|
|
|
|
// specifiers of keys that are required or optional. If a key shows up
|
|
|
|
// that isn't in one of these two lists, then an error is generated.
|
|
|
|
//
|
|
|
|
// The "specifiers" allowed in this is a fairly rich syntax to help
|
|
|
|
// describe the format of your configuration:
|
|
|
|
//
|
|
|
|
// * Basic keys are just strings. For example: "foo" will match the
|
|
|
|
// "foo" key.
|
|
|
|
//
|
|
|
|
// * Nested structure keys can be matched by doing
|
|
|
|
// "listener.*.foo". This will verify that there is at least one
|
|
|
|
// listener element that has the "foo" key set.
|
|
|
|
//
|
|
|
|
// * The existence of a nested structure can be checked by simply
|
|
|
|
// doing "listener.*" which will verify that there is at least
|
|
|
|
// one element in the "listener" structure. This is NOT
|
|
|
|
// validating that "listener" is an array. It is validating
|
|
|
|
// that it is a nested structure in the configuration.
|
|
|
|
//
|
2014-07-08 19:02:02 +02:00
|
|
|
type Validator struct {
|
|
|
|
Required []string
|
|
|
|
Optional []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Validator) Validate(
|
|
|
|
c *terraform.ResourceConfig) (ws []string, es []error) {
|
2014-07-08 20:14:07 +02:00
|
|
|
// Flatten the configuration so it is easier to reason about
|
|
|
|
flat := flatmap.Flatten(c.Raw)
|
|
|
|
|
|
|
|
keySet := make(map[string]validatorKey)
|
|
|
|
for i, vs := range [][]string{v.Required, v.Optional} {
|
|
|
|
req := i == 0
|
|
|
|
for _, k := range vs {
|
|
|
|
vk, err := newValidatorKey(k, req)
|
|
|
|
if err != nil {
|
|
|
|
es = append(es, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
keySet[k] = vk
|
|
|
|
}
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 20:14:07 +02:00
|
|
|
purged := make([]string, 0)
|
|
|
|
for _, kv := range keySet {
|
|
|
|
p, w, e := kv.Validate(flat)
|
|
|
|
if len(w) > 0 {
|
|
|
|
ws = append(ws, w...)
|
|
|
|
}
|
|
|
|
if len(e) > 0 {
|
|
|
|
es = append(es, e...)
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 20:14:07 +02:00
|
|
|
purged = append(purged, p...)
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
|
2014-07-08 20:14:07 +02:00
|
|
|
// Delete all the keys we processed in order to find
|
|
|
|
// the unknown keys.
|
|
|
|
for _, p := range purged {
|
|
|
|
delete(flat, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The rest are unknown
|
|
|
|
for k, _ := range flat {
|
|
|
|
es = append(es, fmt.Errorf("Unknown configuration: %s", k))
|
2014-07-08 19:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2014-07-08 20:14:07 +02:00
|
|
|
|
|
|
|
type validatorKey interface {
|
2014-07-15 03:22:05 +02:00
|
|
|
// Validate validates the given configuration and returns viewed keys,
|
|
|
|
// warnings, and errors.
|
2014-07-08 20:14:07 +02:00
|
|
|
Validate(map[string]string) ([]string, []string, []error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newValidatorKey(k string, req bool) (validatorKey, error) {
|
|
|
|
var result validatorKey
|
|
|
|
|
|
|
|
parts := strings.Split(k, ".")
|
|
|
|
if len(parts) > 1 && parts[1] == "*" {
|
|
|
|
result = &nestedValidatorKey{
|
2014-07-17 00:07:46 +02:00
|
|
|
Parts: parts,
|
2014-07-08 20:14:07 +02:00
|
|
|
Required: req,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = &basicValidatorKey{
|
|
|
|
Key: k,
|
|
|
|
Required: req,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// basicValidatorKey validates keys that are basic such as "foo"
|
|
|
|
type basicValidatorKey struct {
|
|
|
|
Key string
|
|
|
|
Required bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *basicValidatorKey) Validate(
|
|
|
|
m map[string]string) ([]string, []string, []error) {
|
|
|
|
for k, _ := range m {
|
|
|
|
// If we have the exact key its a match
|
|
|
|
if k == v.Key {
|
|
|
|
return []string{k}, nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v.Required {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, []error{fmt.Errorf(
|
|
|
|
"Key not found: %s", v.Key)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type nestedValidatorKey struct {
|
2014-07-17 00:07:46 +02:00
|
|
|
Parts []string
|
2014-07-08 20:14:07 +02:00
|
|
|
Required bool
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:07:46 +02:00
|
|
|
func (v *nestedValidatorKey) validate(
|
|
|
|
m map[string]string,
|
|
|
|
prefix string,
|
|
|
|
offset int) ([]string, []string, []error) {
|
|
|
|
if offset >= len(v.Parts) {
|
|
|
|
// We're at the end. Look for a specific key.
|
|
|
|
v2 := &basicValidatorKey{Key: prefix, Required: v.Required}
|
|
|
|
return v2.Validate(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
current := v.Parts[offset]
|
|
|
|
|
|
|
|
// If we're at offset 0, special case to start at the next one.
|
|
|
|
if offset == 0 {
|
|
|
|
return v.validate(m, current, offset+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if we're doing a "for all" or a specific key
|
|
|
|
if current != "*" {
|
|
|
|
// We're looking at a specific key, continue on.
|
|
|
|
return v.validate(m, prefix+"."+current, offset+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're doing a "for all", so we loop over.
|
|
|
|
countStr, ok := m[prefix+".#"]
|
2014-07-08 20:14:07 +02:00
|
|
|
if !ok {
|
2014-07-17 00:07:46 +02:00
|
|
|
if !v.Required {
|
|
|
|
// It wasn't required, so its no problem.
|
2014-07-08 20:14:07 +02:00
|
|
|
return nil, nil, nil
|
|
|
|
}
|
2014-07-17 00:07:46 +02:00
|
|
|
|
|
|
|
return nil, nil, []error{fmt.Errorf(
|
|
|
|
"Key not found: %s", prefix)}
|
2014-07-08 20:14:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
count, err := strconv.ParseInt(countStr, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
// This shouldn't happen if flatmap works properly
|
|
|
|
panic("invalid flatmap array")
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:07:46 +02:00
|
|
|
var e []error
|
|
|
|
var w []string
|
|
|
|
u := make([]string, 1, count+1)
|
|
|
|
u[0] = prefix + ".#"
|
2014-07-08 20:14:07 +02:00
|
|
|
for i := 0; i < int(count); i++ {
|
2014-07-17 00:07:46 +02:00
|
|
|
prefix := fmt.Sprintf("%s.%d", prefix, i)
|
2014-07-08 20:14:07 +02:00
|
|
|
|
2014-07-17 00:07:46 +02:00
|
|
|
// Mark that we saw this specific key
|
|
|
|
u = append(u, prefix)
|
|
|
|
|
|
|
|
// Mark all prefixes of this
|
2014-07-08 20:14:07 +02:00
|
|
|
for k, _ := range m {
|
2014-07-17 00:07:46 +02:00
|
|
|
if !strings.HasPrefix(k, prefix+".") {
|
|
|
|
continue
|
2014-07-08 20:14:07 +02:00
|
|
|
}
|
2014-07-17 00:07:46 +02:00
|
|
|
u = append(u, k)
|
|
|
|
}
|
2014-07-08 20:14:07 +02:00
|
|
|
|
2014-07-17 00:07:46 +02:00
|
|
|
// If we have more parts, then validate deeper
|
|
|
|
if offset+1 < len(v.Parts) {
|
|
|
|
u2, w2, e2 := v.validate(m, prefix, offset+1)
|
|
|
|
|
|
|
|
u = append(u, u2...)
|
|
|
|
w = append(w, w2...)
|
|
|
|
e = append(e, e2...)
|
2014-07-08 20:14:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-17 00:07:46 +02:00
|
|
|
return u, w, e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *nestedValidatorKey) Validate(
|
|
|
|
m map[string]string) ([]string, []string, []error) {
|
|
|
|
return v.validate(m, "", 0)
|
2014-07-08 20:14:07 +02:00
|
|
|
}
|