2014-12-19 18:22:53 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-07 01:19:10 +02:00
|
|
|
"log"
|
2015-01-09 03:02:19 +01:00
|
|
|
"strconv"
|
2014-12-19 18:22:53 +01:00
|
|
|
"strings"
|
2015-01-09 03:02:19 +01:00
|
|
|
"sync"
|
2014-12-19 18:22:53 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2015-01-27 21:23:49 +01:00
|
|
|
// ConfigFieldReader reads fields out of an untyped map[string]string to the
|
|
|
|
// best of its ability. It also applies defaults from the Schema. (The other
|
|
|
|
// field readers do not need default handling because they source fully
|
|
|
|
// populated data structures.)
|
2014-12-19 18:22:53 +01:00
|
|
|
type ConfigFieldReader struct {
|
|
|
|
Config *terraform.ResourceConfig
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema map[string]*Schema
|
2015-01-09 03:02:19 +01:00
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
indexMaps map[string]map[string]int
|
|
|
|
once sync.Once
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
func (r *ConfigFieldReader) ReadField(address []string) (FieldReadResult, error) {
|
2015-11-18 11:24:04 +01:00
|
|
|
r.once.Do(func() { r.indexMaps = make(map[string]map[string]int) })
|
2015-01-09 03:02:19 +01:00
|
|
|
return r.readField(address, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ConfigFieldReader) readField(
|
|
|
|
address []string, nested bool) (FieldReadResult, error) {
|
|
|
|
schemaList := addrToSchema(address, r.Schema)
|
|
|
|
if len(schemaList) == 0 {
|
2015-01-03 18:13:46 +01:00
|
|
|
return FieldReadResult{}, nil
|
|
|
|
}
|
2014-12-19 18:22:53 +01:00
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
if !nested {
|
|
|
|
// If we have a set anywhere in the address, then we need to
|
|
|
|
// read that set out in order and actually replace that part of
|
|
|
|
// the address with the real list index. i.e. set.50 might actually
|
|
|
|
// map to set.12 in the config, since it is in list order in the
|
|
|
|
// config, not indexed by set value.
|
|
|
|
for i, v := range schemaList {
|
|
|
|
// Sets are the only thing that cause this issue.
|
|
|
|
if v.Type != TypeSet {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're at the end of the list, then we don't have to worry
|
|
|
|
// about this because we're just requesting the whole set.
|
|
|
|
if i == len(schemaList)-1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're looking for the count, then ignore...
|
|
|
|
if address[i+1] == "#" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
indexMap, ok := r.indexMaps[strings.Join(address[:i+1], ".")]
|
|
|
|
if !ok {
|
|
|
|
// Get the set so we can get the index map that tells us the
|
|
|
|
// mapping of the hash code to the list index
|
|
|
|
_, err := r.readSet(address[:i+1], v)
|
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
indexMap = r.indexMaps[strings.Join(address[:i+1], ".")]
|
2015-01-09 03:02:19 +01:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
index, ok := indexMap[address[i+1]]
|
2015-01-09 03:02:19 +01:00
|
|
|
if !ok {
|
|
|
|
return FieldReadResult{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
address[i+1] = strconv.FormatInt(int64(index), 10)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
k := strings.Join(address, ".")
|
|
|
|
schema := schemaList[len(schemaList)-1]
|
2016-12-23 02:55:23 +01:00
|
|
|
|
|
|
|
// If we're getting the single element of a promoted list, then
|
|
|
|
// check to see if we have a single element we need to promote.
|
|
|
|
if address[len(address)-1] == "0" && len(schemaList) > 1 {
|
|
|
|
lastSchema := schemaList[len(schemaList)-2]
|
|
|
|
if lastSchema.Type == TypeList && lastSchema.PromoteSingle {
|
|
|
|
k := strings.Join(address[:len(address)-1], ".")
|
|
|
|
result, err := r.readPrimitive(k, schema)
|
|
|
|
if err == nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 22:20:59 +02:00
|
|
|
if protoVersion5 {
|
2019-05-07 01:19:10 +02:00
|
|
|
switch schema.Type {
|
|
|
|
case TypeList, TypeSet, TypeMap, typeObject:
|
|
|
|
// Check if the value itself is unknown.
|
|
|
|
// The new protocol shims will add unknown values to this list of
|
|
|
|
// ComputedKeys. This is the only way we have to indicate that a
|
|
|
|
// collection is unknown in the config
|
|
|
|
for _, unknown := range r.Config.ComputedKeys {
|
|
|
|
if k == unknown {
|
|
|
|
log.Printf("[DEBUG] setting computed for %q from ComputedKeys", k)
|
|
|
|
return FieldReadResult{Computed: true, Exists: true}, nil
|
|
|
|
}
|
2019-05-02 22:20:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:22:53 +01:00
|
|
|
switch schema.Type {
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeBool, TypeFloat, TypeInt, TypeString:
|
2014-12-19 18:22:53 +01:00
|
|
|
return r.readPrimitive(k, schema)
|
|
|
|
case TypeList:
|
2016-12-23 02:55:23 +01:00
|
|
|
// If we support promotion then we first check if we have a lone
|
|
|
|
// value that we must promote.
|
|
|
|
// a value that is alone.
|
|
|
|
if schema.PromoteSingle {
|
|
|
|
result, err := r.readPrimitive(k, schema.Elem.(*Schema))
|
|
|
|
if err == nil && result.Exists {
|
|
|
|
result.Value = []interface{}{result.Value}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-30 17:48:52 +02:00
|
|
|
return readListField(&nestedConfigFieldReader{r}, address, schema)
|
2014-12-19 18:22:53 +01:00
|
|
|
case TypeMap:
|
2016-11-17 21:12:29 +01:00
|
|
|
return r.readMap(k, schema)
|
2014-12-19 18:22:53 +01:00
|
|
|
case TypeSet:
|
2015-11-18 11:24:04 +01:00
|
|
|
return r.readSet(address, schema)
|
2014-12-19 18:22:53 +01:00
|
|
|
case typeObject:
|
2015-01-09 03:02:19 +01:00
|
|
|
return readObjectField(
|
|
|
|
&nestedConfigFieldReader{r},
|
|
|
|
address, schema.Elem.(map[string]*Schema))
|
2014-12-19 18:22:53 +01:00
|
|
|
default:
|
2015-01-11 01:04:01 +01:00
|
|
|
panic(fmt.Sprintf("Unknown type: %s", schema.Type))
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 21:12:29 +01:00
|
|
|
func (r *ConfigFieldReader) readMap(k string, schema *Schema) (FieldReadResult, error) {
|
2015-04-21 22:07:52 +02:00
|
|
|
// We want both the raw value and the interpolated. We use the interpolated
|
|
|
|
// to store actual values and we use the raw one to check for
|
2016-04-27 23:41:17 +02:00
|
|
|
// computed keys. Actual values are obtained in the switch, depending on
|
|
|
|
// the type of the raw value.
|
2015-04-21 22:07:52 +02:00
|
|
|
mraw, ok := r.Config.GetRaw(k)
|
2014-12-19 18:22:53 +01:00
|
|
|
if !ok {
|
2016-11-23 19:43:56 +01:00
|
|
|
// check if this is from an interpolated field by seeing if it exists
|
|
|
|
// in the config
|
|
|
|
_, ok := r.Config.Get(k)
|
|
|
|
if !ok {
|
|
|
|
// this really doesn't exist
|
|
|
|
return FieldReadResult{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We couldn't fetch the value from a nested data structure, so treat the
|
|
|
|
// raw value as an interpolation string. The mraw value is only used
|
|
|
|
// for the type switch below.
|
|
|
|
mraw = "${INTERPOLATED}"
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result := make(map[string]interface{})
|
2015-04-21 22:07:52 +02:00
|
|
|
computed := false
|
2014-12-19 18:22:53 +01:00
|
|
|
switch m := mraw.(type) {
|
2016-04-27 23:41:17 +02:00
|
|
|
case string:
|
|
|
|
// This is a map which has come out of an interpolated variable, so we
|
|
|
|
// can just get the value directly from config. Values cannot be computed
|
|
|
|
// currently.
|
|
|
|
v, _ := r.Config.Get(k)
|
|
|
|
|
|
|
|
// If this isn't a map[string]interface, it must be computed.
|
|
|
|
mapV, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return FieldReadResult{
|
|
|
|
Exists: true,
|
|
|
|
Computed: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise we can proceed as usual.
|
|
|
|
for i, iv := range mapV {
|
|
|
|
result[i] = iv
|
|
|
|
}
|
2014-12-19 18:22:53 +01:00
|
|
|
case []interface{}:
|
2015-04-21 22:07:52 +02:00
|
|
|
for i, innerRaw := range m {
|
2015-11-18 11:24:04 +01:00
|
|
|
for ik := range innerRaw.(map[string]interface{}) {
|
2015-04-21 22:07:52 +02:00
|
|
|
key := fmt.Sprintf("%s.%d.%s", k, i, ik)
|
|
|
|
if r.Config.IsComputed(key) {
|
|
|
|
computed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := r.Config.Get(key)
|
|
|
|
result[ik] = v
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case []map[string]interface{}:
|
2015-04-21 22:07:52 +02:00
|
|
|
for i, innerRaw := range m {
|
2015-11-18 11:24:04 +01:00
|
|
|
for ik := range innerRaw {
|
2015-04-21 22:07:52 +02:00
|
|
|
key := fmt.Sprintf("%s.%d.%s", k, i, ik)
|
|
|
|
if r.Config.IsComputed(key) {
|
|
|
|
computed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := r.Config.Get(key)
|
|
|
|
result[ik] = v
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case map[string]interface{}:
|
2015-11-18 11:24:04 +01:00
|
|
|
for ik := range m {
|
2015-04-21 22:07:52 +02:00
|
|
|
key := fmt.Sprintf("%s.%s", k, ik)
|
|
|
|
if r.Config.IsComputed(key) {
|
|
|
|
computed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
v, _ := r.Config.Get(key)
|
|
|
|
result[ik] = v
|
|
|
|
}
|
2014-12-19 18:22:53 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown type: %#v", mraw))
|
|
|
|
}
|
|
|
|
|
2018-03-14 22:50:41 +01:00
|
|
|
err := mapValuesToPrimitive(k, result, schema)
|
2016-11-17 21:12:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, nil
|
|
|
|
}
|
|
|
|
|
2015-04-21 22:07:52 +02:00
|
|
|
var value interface{}
|
|
|
|
if !computed {
|
|
|
|
value = result
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
2015-04-21 22:07:52 +02:00
|
|
|
Value: value,
|
|
|
|
Exists: true,
|
|
|
|
Computed: computed,
|
2014-12-19 21:47:35 +01:00
|
|
|
}, nil
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ConfigFieldReader) readPrimitive(
|
2014-12-19 21:47:35 +01:00
|
|
|
k string, schema *Schema) (FieldReadResult, error) {
|
2014-12-19 18:22:53 +01:00
|
|
|
raw, ok := r.Config.Get(k)
|
|
|
|
if !ok {
|
2015-01-27 21:23:49 +01:00
|
|
|
// Nothing in config, but we might still have a default from the schema
|
|
|
|
var err error
|
|
|
|
raw, err = schema.DefaultValue()
|
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, fmt.Errorf("%s, error loading default: %s", k, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if raw == nil {
|
|
|
|
return FieldReadResult{}, nil
|
|
|
|
}
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var result string
|
|
|
|
if err := mapstructure.WeakDecode(raw, &result); err != nil {
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{}, err
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
computed := r.Config.IsComputed(k)
|
|
|
|
returnVal, err := stringToPrimitive(result, computed, schema)
|
|
|
|
if err != nil {
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{}, err
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: returnVal,
|
|
|
|
Exists: true,
|
|
|
|
Computed: computed,
|
|
|
|
}, nil
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ConfigFieldReader) readSet(
|
2015-11-18 11:24:04 +01:00
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
|
|
|
indexMap := make(map[string]int)
|
2015-01-10 21:42:15 +01:00
|
|
|
// Create the set that will be our result
|
2015-08-18 04:26:58 +02:00
|
|
|
set := schema.ZeroValue().(*Set)
|
2015-01-10 21:42:15 +01:00
|
|
|
|
2016-06-30 17:48:52 +02:00
|
|
|
raw, err := readListField(&nestedConfigFieldReader{r}, address, schema)
|
2014-12-19 18:22:53 +01:00
|
|
|
if err != nil {
|
2015-11-18 11:24:04 +01:00
|
|
|
return FieldReadResult{}, err
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if !raw.Exists {
|
2015-11-18 11:24:04 +01:00
|
|
|
return FieldReadResult{Value: set}, nil
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the list is computed, the set is necessarilly computed
|
2014-12-19 21:47:35 +01:00
|
|
|
if raw.Computed {
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
|
|
|
Exists: true,
|
|
|
|
Computed: raw.Computed,
|
2015-11-18 11:24:04 +01:00
|
|
|
}, nil
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build up the set from the list elements
|
2015-01-09 03:02:19 +01:00
|
|
|
for i, v := range raw.Value.([]interface{}) {
|
|
|
|
// Check if any of the keys in this item are computed
|
|
|
|
computed := r.hasComputedSubKeys(
|
|
|
|
fmt.Sprintf("%s.%d", strings.Join(address, "."), i), schema)
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
code := set.add(v, computed)
|
2015-01-09 03:02:19 +01:00
|
|
|
indexMap[code] = i
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|
|
|
|
|
2015-11-18 11:24:04 +01:00
|
|
|
r.indexMaps[strings.Join(address, ".")] = indexMap
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
|
|
|
Exists: true,
|
2015-11-18 11:24:04 +01:00
|
|
|
}, nil
|
2015-01-09 03:02:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// hasComputedSubKeys walks through a schema and returns whether or not the
|
|
|
|
// given key contains any subkeys that are computed.
|
|
|
|
func (r *ConfigFieldReader) hasComputedSubKeys(key string, schema *Schema) bool {
|
|
|
|
prefix := key + "."
|
|
|
|
|
|
|
|
switch t := schema.Elem.(type) {
|
|
|
|
case *Resource:
|
|
|
|
for k, schema := range t.Schema {
|
|
|
|
if r.Config.IsComputed(prefix + k) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.hasComputedSubKeys(prefix+k, schema) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// nestedConfigFieldReader is a funny little thing that just wraps a
|
|
|
|
// ConfigFieldReader to call readField when ReadField is called so that
|
|
|
|
// we don't recalculate the set rewrites in the address, which leads to
|
|
|
|
// an infinite loop.
|
|
|
|
type nestedConfigFieldReader struct {
|
|
|
|
Reader *ConfigFieldReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *nestedConfigFieldReader) ReadField(
|
|
|
|
address []string) (FieldReadResult, error) {
|
|
|
|
return r.Reader.readField(address, true)
|
2014-12-19 18:22:53 +01:00
|
|
|
}
|