2014-12-19 21:47:35 +01:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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
|
|
|
|
// because a diff on its own doesn't have complete data about full objects
|
|
|
|
// such as maps.
|
|
|
|
//
|
|
|
|
// 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:
|
2015-01-03 18:13:46 +01:00
|
|
|
return readListField(r, 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{})
|
|
|
|
resultSet := false
|
|
|
|
|
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{})
|
|
|
|
resultSet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-12-19 21:47:35 +01:00
|
|
|
resultSet = true
|
|
|
|
|
|
|
|
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{}
|
|
|
|
if resultSet {
|
|
|
|
resultVal = result
|
|
|
|
}
|
|
|
|
|
|
|
|
return FieldReadResult{
|
2015-01-03 15:55:38 +01:00
|
|
|
Value: resultVal,
|
|
|
|
Exists: resultSet,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Go through the map and find all the set items
|
2015-06-26 06:52:49 +02:00
|
|
|
for k, d := range r.Diff.Attributes {
|
|
|
|
if d.NewRemoved {
|
|
|
|
// If the field is removed, we always ignore it
|
|
|
|
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
|
|
|
|
}
|