helper/schema: Read native maps from configuration

This adds a test and the support necessary to read from native maps
passed as variables via interpolation - for example:

```
resource ...... {
     mapValue = "${var.map}"
}
```

We also add support for interpolating maps from the flat-mapped resource
config, which is necessary to support assignment of computed maps, which
is now valid.

Unfortunately there is no good way to distinguish between a list and a
map in the flatmap. In lieu of changing that representation (which is
risky), we assume that if all the keys are numeric, this is intended to
be a list, and if not it is intended to be a map. This does preclude
maps which have purely numeric keys, which should be noted as a
backwards compatibility concern.
This commit is contained in:
James Nugent 2016-04-27 16:41:17 -05:00
parent 244da895cd
commit 7b6df27e4a
4 changed files with 98 additions and 17 deletions

View File

@ -100,7 +100,8 @@ func (r *ConfigFieldReader) readField(
func (r *ConfigFieldReader) readMap(k string) (FieldReadResult, error) { func (r *ConfigFieldReader) readMap(k string) (FieldReadResult, error) {
// We want both the raw value and the interpolated. We use the interpolated // 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 // to store actual values and we use the raw one to check for
// computed keys. // computed keys. Actual values are obtained in the switch, depending on
// the type of the raw value.
mraw, ok := r.Config.GetRaw(k) mraw, ok := r.Config.GetRaw(k)
if !ok { if !ok {
return FieldReadResult{}, nil return FieldReadResult{}, nil
@ -109,6 +110,25 @@ func (r *ConfigFieldReader) readMap(k string) (FieldReadResult, error) {
result := make(map[string]interface{}) result := make(map[string]interface{})
computed := false computed := false
switch m := mraw.(type) { switch m := mraw.(type) {
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
}
case []interface{}: case []interface{}:
for i, innerRaw := range m { for i, innerRaw := range m {
for ik := range innerRaw.(map[string]interface{}) { for ik := range innerRaw.(map[string]interface{}) {

View File

@ -183,6 +183,36 @@ func TestConfigFieldReader_ComputedMap(t *testing.T) {
}), }),
false, false,
}, },
"native map": {
[]string{"map"},
FieldReadResult{
Value: map[string]interface{}{
"bar": "baz",
"baz": "bar",
},
Exists: true,
Computed: false,
},
testConfigInterpolate(t, map[string]interface{}{
"map": "${var.foo}",
}, map[string]ast.Variable{
"var.foo": ast.Variable{
Type: ast.TypeMap,
Value: map[string]ast.Variable{
"bar": ast.Variable{
Type: ast.TypeString,
Value: "baz",
},
"baz": ast.Variable{
Type: ast.TypeString,
Value: "bar",
},
},
},
}),
false,
},
} }
for name, tc := range cases { for name, tc := range cases {

View File

@ -19,8 +19,10 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/davecgh/go-spew/spew"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"log"
) )
// Schema is used to describe the structure of a value. // Schema is used to describe the structure of a value.
@ -1120,11 +1122,21 @@ func (m schemaMap) validateMap(
// case to []interface{} unless the slice is exactly that type. // case to []interface{} unless the slice is exactly that type.
rawV := reflect.ValueOf(raw) rawV := reflect.ValueOf(raw)
switch rawV.Kind() { switch rawV.Kind() {
case reflect.String:
// If raw and reified are equal, this is a string and should
// be rejected.
reified, reifiedOk := c.Get(k)
log.Printf("[jen20] reified: %s", spew.Sdump(reified))
log.Printf("[jen20] raw: %s", spew.Sdump(raw))
if reifiedOk && raw == reified && !c.IsComputed(k) {
return nil, []error{fmt.Errorf("%s: should be a map", k)}
}
// Otherwise it's likely raw is an interpolation.
return nil, nil
case reflect.Map: case reflect.Map:
case reflect.Slice: case reflect.Slice:
default: default:
return nil, []error{fmt.Errorf( return nil, []error{fmt.Errorf("%s: should be a map", k)}
"%s: should be a map", k)}
} }
// If it is not a slice, it is valid // If it is not a slice, it is valid

View File

@ -384,9 +384,9 @@ func (i *Interpolater) computeResourceVariable(
return &ast.Variable{Type: ast.TypeString, Value: attr}, nil return &ast.Variable{Type: ast.TypeString, Value: attr}, nil
} }
// computed list attribute // computed list or map attribute
if _, ok := r.Primary.Attributes[v.Field+".#"]; ok { if _, ok := r.Primary.Attributes[v.Field+".#"]; ok {
variable, err := i.interpolateListAttribute(v.Field, r.Primary.Attributes) variable, err := i.interpolateComplexTypeAttribute(v.Field, r.Primary.Attributes)
return &variable, err return &variable, err
} }
@ -513,7 +513,7 @@ func (i *Interpolater) computeResourceMultiVariable(
if !ok { if !ok {
continue continue
} }
multiAttr, err := i.interpolateListAttribute(v.Field, r.Primary.Attributes) multiAttr, err := i.interpolateComplexTypeAttribute(v.Field, r.Primary.Attributes)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -559,12 +559,12 @@ func (i *Interpolater) computeResourceMultiVariable(
return &variable, err return &variable, err
} }
func (i *Interpolater) interpolateListAttribute( func (i *Interpolater) interpolateComplexTypeAttribute(
resourceID string, resourceID string,
attributes map[string]string) (ast.Variable, error) { attributes map[string]string) (ast.Variable, error) {
attr := attributes[resourceID+".#"] attr := attributes[resourceID+".#"]
log.Printf("[DEBUG] Interpolating computed list attribute %s (%s)", log.Printf("[DEBUG] Interpolating computed complex type attribute %s (%s)",
resourceID, attr) resourceID, attr)
// In Terraform's internal dotted representation of list-like attributes, the // In Terraform's internal dotted representation of list-like attributes, the
@ -575,18 +575,37 @@ func (i *Interpolater) interpolateListAttribute(
return unknownVariable(), nil return unknownVariable(), nil
} }
// Otherwise we gather the values from the list-like attribute and return // At this stage we don't know whether the item is a list or a map, so we
// them. // examine the keys to see whether they are all numeric.
var members []string var numericKeys []string
numberedListMember := regexp.MustCompile("^" + resourceID + "\\.[0-9]+$") var allKeys []string
for id, value := range attributes { numberedListKey := regexp.MustCompile("^" + resourceID + "\\.[0-9]+$")
if numberedListMember.MatchString(id) { otherListKey := regexp.MustCompile("^" + resourceID + "\\.([^#]+)$")
members = append(members, value) for id, _ := range attributes {
if numberedListKey.MatchString(id) {
numericKeys = append(numericKeys, id)
}
if submatches := otherListKey.FindAllStringSubmatch(id, -1); len(submatches) > 0 {
allKeys = append(allKeys, submatches[0][1])
} }
} }
if len(numericKeys) == len(allKeys) {
// This is a list
var members []string
for _, key := range numericKeys {
members = append(members, attributes[key])
}
sort.Strings(members) sort.Strings(members)
return hil.InterfaceToVariable(members) return hil.InterfaceToVariable(members)
} else {
// This is a map
members := make(map[string]interface{})
for _, key := range allKeys {
members[key] = attributes[resourceID+"."+key]
}
return hil.InterfaceToVariable(members)
}
} }
func (i *Interpolater) resourceVariableInfo( func (i *Interpolater) resourceVariableInfo(