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
|
|
|
|
}
|
2015-02-04 02:48:25 +01:00
|
|
|
if strings.HasPrefix(k, prefix+"#") {
|
|
|
|
// 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
|
|
|
|
set := &Set{F: schema.Set}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|