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 {
|
|
|
|
case TypeBool:
|
|
|
|
fallthrough
|
2015-01-11 01:04:01 +01:00
|
|
|
case TypeFloat:
|
|
|
|
fallthrough
|
2014-12-19 21:47:35 +01:00
|
|
|
case TypeInt:
|
|
|
|
fallthrough
|
|
|
|
case 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
|
|
|
|
}
|
|
|
|
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) {
|
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-01-03 18:13:46 +01:00
|
|
|
prefix := strings.Join(address, ".") + "."
|
2014-12-19 21:47:35 +01:00
|
|
|
for k, _ := range r.Diff.Attributes {
|
|
|
|
if !strings.HasPrefix(k, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(k, prefix+"#") {
|
|
|
|
// Ignore the count field
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return FieldReadResult{
|
|
|
|
Value: set,
|
2015-01-09 03:02:19 +01:00
|
|
|
Exists: set.Len() > 0,
|
2014-12-19 21:47:35 +01:00
|
|
|
}, nil
|
|
|
|
}
|