2014-12-19 21:47:35 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-27 21:54:51 +02:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2014-12-19 21:47:35 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DiffFieldReader reads fields out of a diff structures.
|
2015-01-03 15:55:38 +01:00
|
|
|
//
|
|
|
|
// It also requires access to a Reader that reads fields from the structure
|
|
|
|
// that the diff was derived from. This is usually the state. This is required
|
2016-06-27 21:54:51 +02:00
|
|
|
// because a diff on its own doesn't have complete data about non-primitive
|
|
|
|
// objects such as maps, lists and sets.
|
2015-01-03 15:55:38 +01:00
|
|
|
//
|
|
|
|
// The Source MUST be the data that the diff was derived from. If it isn't,
|
|
|
|
// the behavior of this struct is undefined.
|
|
|
|
//
|
|
|
|
// Reading fields from a DiffFieldReader is identical to reading from
|
|
|
|
// Source except the diff will be applied to the end result.
|
|
|
|
//
|
|
|
|
// The "Exists" field on the result will be set to true if the complete
|
|
|
|
// field exists whether its from the source, diff, or a combination of both.
|
|
|
|
// It cannot be determined whether a retrieved value is composed of
|
|
|
|
// diff elements.
|
2014-12-19 21:47:35 +01:00
|
|
|
type DiffFieldReader struct {
|
2015-01-03 15:55:38 +01:00
|
|
|
Diff *terraform.InstanceDiff
|
|
|
|
Source FieldReader
|
2015-01-03 18:13:46 +01:00
|
|
|
Schema map[string]*Schema
|
2014-12-19 21:47:35 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
func (r *DiffFieldReader) ReadField(address []string) (FieldReadResult, error) {
|
2015-01-09 03:02:19 +01:00
|
|
|
schemaList := addrToSchema(address, r.Schema)
|
|
|
|
if len(schemaList) == 0 {
|
2015-01-03 18:13:46 +01:00
|
|
|
return FieldReadResult{}, nil
|
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
|
2015-01-09 03:02:19 +01:00
|
|
|
schema := schemaList[len(schemaList)-1]
|
2014-12-19 21:47:35 +01:00
|
|
|
switch schema.Type {
|
2015-05-27 03:52:36 +02:00
|
|
|
case TypeBool, TypeInt, TypeFloat, TypeString:
|
2015-01-03 18:13:46 +01:00
|
|
|
return r.readPrimitive(address, schema)
|
2014-12-19 21:47:35 +01:00
|
|
|
case TypeList:
|
2016-06-27 21:54:51 +02:00
|
|
|
return r.readList(address, schema)
|
2014-12-19 21:47:35 +01:00
|
|
|
case TypeMap:
|
2015-01-03 18:13:46 +01:00
|
|
|
return r.readMap(address, schema)
|
2014-12-19 21:47:35 +01:00
|
|
|
case TypeSet:
|
2015-01-03 18:13:46 +01:00
|
|
|
return r.readSet(address, schema)
|
2014-12-19 21:47:35 +01:00
|
|
|
case typeObject:
|
2015-01-03 18:13:46 +01:00
|
|
|
return readObjectField(r, address, schema.Elem.(map[string]*Schema))
|
2014-12-19 21:47:35 +01:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:55:38 +01:00
|
|
|
func (r *DiffFieldReader) readMap(
|
2015-01-03 18:13:46 +01:00
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
2014-12-19 21:47:35 +01:00
|
|
|
result := make(map[string]interface{})
|
2016-06-25 09:50:41 +02:00
|
|
|
exists := false
|
2014-12-19 21:47:35 +01:00
|
|
|
|
2015-01-03 15:55:38 +01:00
|
|
|
// First read the map from the underlying source
|
2015-01-03 18:13:46 +01:00
|
|
|
source, err := r.Source.ReadField(address)
|
2015-01-03 15:55:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
if source.Exists {
|
|
|
|
result = source.Value.(map[string]interface{})
|
2016-06-25 09:50:41 +02:00
|
|
|
exists = true
|
2015-01-03 15:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next, read all the elements we have in our diff, and apply
|
|
|
|
// the diff to our result.
|
2015-01-03 18:13:46 +01:00
|
|
|
prefix := strings.Join(address, ".") + "."
|
2014-12-19 21:47:35 +01:00
|
|
|
for k, v := range r.Diff.Attributes {
|
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
core: Use .% instead of .# for maps in state
The flatmapped representation of state prior to this commit encoded maps
and lists (and therefore by extension, sets) with a key corresponding to
the number of elements, or the unknown variable indicator under a .# key
and then individual items. For example, the list ["a", "b", "c"] would
have been encoded as:
listname.# = 3
listname.0 = "a"
listname.1 = "b"
listname.2 = "c"
And the map {"key1": "value1", "key2", "value2"} would have been encoded
as:
mapname.# = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
Sets use the hash code as the key - for example a set with a (fictional)
hashcode calculation may look like:
setname.# = 2
setname.12312512 = "value1"
setname.56345233 = "value2"
Prior to the work done to extend the type system, this was sufficient
since the internal representation of these was effectively the same.
However, following the separation of maps and lists into distinct
first-class types, this encoding presents a problem: given a state file,
it is impossible to tell the encoding of an empty list and an empty map
apart. This presents problems for the type checker during interpolation,
as many interpolation functions will operate on only one of these two
structures.
This commit therefore changes the representation in state of maps to use
a "%" as the key for the number of elements. Consequently the map above
will now be encoded as:
mapname.% = 2
mapname.key1 = "value1"
mapname.key2 = "value2"
This has the effect of an empty list (or set) now being encoded as:
listname.# = 0
And an empty map now being encoded as:
mapname.% = 0
Therefore we can eliminate some nasty guessing logic from the resource
variable supplier for interpolation, at the cost of having to migrate
state up front (to follow in a subsequent commit).
In order to reduce the number of potential situations in which resources
would be "forced new", we continue to accept "#" as the count key when
reading maps via helper/schema. There is no situation under which we can
allow "#" as an actual map key in any case, as it would not be
distinguishable from a list or set in state.
2016-06-05 10:34:43 +02:00
|
|
|
if strings.HasPrefix(k, prefix+"%") {
|
2015-02-04 02:48:25 +01:00
|
|
|
// Ignore the count field
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-25 09:50:41 +02:00
|
|
|
exists = true
|
2014-12-19 21:47:35 +01:00
|
|
|
|
|
|
|
k = k[len(prefix):]
|
|
|
|
if v.NewRemoved {
|
2015-01-03 15:55:38 +01:00
|
|
|
delete(result, k)
|
2014-12-19 21:47:35 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
result[k] = v.New
|
|
|
|
}
|
|
|
|
|
|
|
|
var resultVal interface{}
|
2016-06-25 09:50:41 +02:00
|
|
|
if exists {
|
2014-12-19 21:47:35 +01:00
|
|
|
resultVal = result
|
|
|
|
}
|
|
|
|
|
|
|
|
return FieldReadResult{
|
2015-01-03 15:55:38 +01:00
|
|
|
Value: resultVal,
|
2016-06-25 09:50:41 +02:00
|
|
|
Exists: exists,
|
2014-12-19 21:47:35 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DiffFieldReader) readPrimitive(
|
2015-01-03 18:13:46 +01:00
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
|
|
|
result, err := r.Source.ReadField(address)
|
2015-01-03 15:55:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
attrD, ok := r.Diff.Attributes[strings.Join(address, ".")]
|
2014-12-19 21:47:35 +01:00
|
|
|
if !ok {
|
2015-01-03 15:55:38 +01:00
|
|
|
return result, nil
|
2014-12-19 21:47:35 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 15:55:38 +01:00
|
|
|
var resultVal string
|
2015-01-03 07:32:49 +01:00
|
|
|
if !attrD.NewComputed {
|
2015-01-03 15:55:38 +01:00
|
|
|
resultVal = attrD.New
|
2015-01-03 07:32:49 +01:00
|
|
|
if attrD.NewExtra != nil {
|
2015-01-09 03:02:19 +01:00
|
|
|
result.ValueProcessed = resultVal
|
2015-01-03 15:55:38 +01:00
|
|
|
if err := mapstructure.WeakDecode(attrD.NewExtra, &resultVal); err != nil {
|
2015-01-03 07:32:49 +01:00
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:55:38 +01:00
|
|
|
result.Computed = attrD.NewComputed
|
2015-01-09 03:02:19 +01:00
|
|
|
result.Exists = true
|
2015-01-03 15:55:38 +01:00
|
|
|
result.Value, err = stringToPrimitive(resultVal, false, schema)
|
2014-12-19 21:47:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
|
2015-01-03 15:55:38 +01:00
|
|
|
return result, nil
|
2014-12-19 21:47:35 +01:00
|
|
|
}
|
|
|
|
|
2016-06-27 21:54:51 +02:00
|
|
|
func (r *DiffFieldReader) readList(
|
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
|
|
|
prefix := strings.Join(address, ".") + "."
|
|
|
|
|
|
|
|
addrPadded := make([]string, len(address)+1)
|
|
|
|
copy(addrPadded, address)
|
|
|
|
|
|
|
|
// Get the number of elements in the list
|
|
|
|
addrPadded[len(addrPadded)-1] = "#"
|
|
|
|
countResult, err := r.readPrimitive(addrPadded, &Schema{Type: TypeInt})
|
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
if !countResult.Exists {
|
|
|
|
// No count, means we have no list
|
|
|
|
countResult.Value = 0
|
|
|
|
}
|
|
|
|
// If we have an empty list, then return an empty list
|
|
|
|
if countResult.Computed || countResult.Value.(int) == 0 {
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: []interface{}{},
|
|
|
|
Exists: countResult.Exists,
|
|
|
|
Computed: countResult.Computed,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail out if diff doesn't contain the given field at all
|
|
|
|
// This has to be a separate loop because we're only
|
|
|
|
// iterating over raw list items (list.idx).
|
|
|
|
// Other fields (list.idx.*) are left for other read* methods
|
|
|
|
// which can deal with these fields appropriately.
|
|
|
|
diffContainsField := false
|
|
|
|
for k, _ := range r.Diff.Attributes {
|
|
|
|
if strings.HasPrefix(k, address[0]+".") {
|
|
|
|
diffContainsField = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !diffContainsField {
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: []interface{}{},
|
|
|
|
Exists: false,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the list that will be our result
|
|
|
|
list := []interface{}{}
|
|
|
|
|
|
|
|
// Go through the diff and find all the list items
|
|
|
|
// We are not iterating over the diff directly as some indexes
|
|
|
|
// may be missing and we expect the whole list to be returned.
|
|
|
|
for i := 0; i < countResult.Value.(int); i++ {
|
|
|
|
idx := strconv.Itoa(i)
|
|
|
|
addrString := prefix + idx
|
|
|
|
|
|
|
|
d, ok := r.Diff.Attributes[addrString]
|
|
|
|
if ok && d.NewRemoved {
|
|
|
|
// If the field is being removed, we ignore it
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
addrPadded[len(addrPadded)-1] = idx
|
|
|
|
raw, err := r.ReadField(addrPadded)
|
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
if !raw.Exists {
|
|
|
|
// This should never happen, because by the time the data
|
|
|
|
// gets to the FieldReaders, all the defaults should be set by
|
|
|
|
// Schema.
|
|
|
|
panic("missing field in set: " + addrString + "." + idx)
|
|
|
|
}
|
|
|
|
list = append(list, raw.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the list "exists". It exists if there are items or if
|
|
|
|
// the diff explicitly wanted it empty.
|
|
|
|
exists := len(list) > 0
|
|
|
|
if !exists {
|
|
|
|
// We could check if the diff value is "0" here but I think the
|
|
|
|
// existence of "#" on its own is enough to show it existed. This
|
|
|
|
// protects us in the future from the zero value changing from
|
|
|
|
// "0" to "" breaking us (if that were to happen).
|
|
|
|
if _, ok := r.Diff.Attributes[prefix+"#"]; ok {
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: list,
|
|
|
|
Exists: exists,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
func (r *DiffFieldReader) readSet(
|
2015-01-03 18:13:46 +01:00
|
|
|
address []string, schema *Schema) (FieldReadResult, error) {
|
2015-02-17 20:38:56 +01:00
|
|
|
prefix := strings.Join(address, ".") + "."
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
// Create the set that will be our result
|
2015-08-18 04:26:58 +02:00
|
|
|
set := schema.ZeroValue().(*Set)
|
2014-12-19 21:47:35 +01:00
|
|
|
|
2016-06-26 17:58:43 +02:00
|
|
|
// Check if we're supposed to remove it
|
|
|
|
v, ok := r.Diff.Attributes[prefix+"#"]
|
|
|
|
if ok && v.New == "0" {
|
|
|
|
// I'm not entirely sure what's the point of
|
|
|
|
// returning empty set w/ Exists: true
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
|
|
|
Exists: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compose list of all keys (diff + source)
|
|
|
|
var keys []string
|
|
|
|
|
|
|
|
// Add keys from diff
|
|
|
|
diffContainsField := false
|
|
|
|
for k, _ := range r.Diff.Attributes {
|
|
|
|
if strings.HasPrefix(k, address[0]+".") {
|
|
|
|
diffContainsField = true
|
|
|
|
}
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
// Bail out if diff doesn't contain the given field at all
|
|
|
|
if !diffContainsField {
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
|
|
|
Exists: false,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
// Add keys from source
|
|
|
|
sourceResult, err := r.Source.ReadField(address)
|
|
|
|
if err == nil && sourceResult.Exists {
|
|
|
|
sourceSet := sourceResult.Value.(*Set)
|
|
|
|
sourceMap := sourceSet.Map()
|
|
|
|
|
|
|
|
for k, _ := range sourceMap {
|
|
|
|
key := prefix + k
|
|
|
|
_, ok := r.Diff.Attributes[key]
|
|
|
|
if !ok {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the order consistent for hashing functions
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
// Go through the map and find all the set items
|
2016-06-26 17:58:43 +02:00
|
|
|
// We are not iterating over the diff directly as some indexes
|
|
|
|
// may be missing and we expect the whole set to be returned.
|
|
|
|
for _, k := range keys {
|
|
|
|
d, ok := r.Diff.Attributes[k]
|
|
|
|
if ok && d.NewRemoved {
|
|
|
|
// If the field is being removed, we ignore it
|
2015-06-26 06:52:49 +02:00
|
|
|
continue
|
|
|
|
}
|
2014-12-19 21:47:35 +01:00
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
2015-02-04 16:25:45 +01:00
|
|
|
if strings.HasSuffix(k, "#") {
|
|
|
|
// Ignore any count field
|
2014-12-19 21:47:35 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the key, since it might be a sub-object like "idx.field"
|
|
|
|
parts := strings.Split(k[len(prefix):], ".")
|
|
|
|
idx := parts[0]
|
|
|
|
|
2015-01-03 18:13:46 +01:00
|
|
|
raw, err := r.ReadField(append(address, idx))
|
2014-12-19 21:47:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return FieldReadResult{}, err
|
|
|
|
}
|
|
|
|
if !raw.Exists {
|
|
|
|
// This shouldn't happen because we just verified it does exist
|
|
|
|
panic("missing field in set: " + k + "." + idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
set.Add(raw.Value)
|
|
|
|
}
|
|
|
|
|
2015-02-17 20:38:56 +01:00
|
|
|
// Determine if the set "exists". It exists if there are items or if
|
|
|
|
// the diff explicitly wanted it empty.
|
|
|
|
exists := set.Len() > 0
|
|
|
|
if !exists {
|
|
|
|
// We could check if the diff value is "0" here but I think the
|
|
|
|
// existence of "#" on its own is enough to show it existed. This
|
|
|
|
// protects us in the future from the zero value changing from
|
|
|
|
// "0" to "" breaking us (if that were to happen).
|
|
|
|
if _, ok := r.Diff.Attributes[prefix+"#"]; ok {
|
|
|
|
exists = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
2015-02-17 20:38:56 +01:00
|
|
|
Exists: exists,
|
2014-12-19 21:47:35 +01:00
|
|
|
}, nil
|
|
|
|
}
|