github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/helper/schema/field_reader_diff.go (about)

     1  package schema
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"github.com/mitchellh/mapstructure"
     9  )
    10  
    11  // DiffFieldReader reads fields out of a diff structures.
    12  //
    13  // It also requires access to a Reader that reads fields from the structure
    14  // that the diff was derived from. This is usually the state. This is required
    15  // because a diff on its own doesn't have complete data about full objects
    16  // such as maps.
    17  //
    18  // The Source MUST be the data that the diff was derived from. If it isn't,
    19  // the behavior of this struct is undefined.
    20  //
    21  // Reading fields from a DiffFieldReader is identical to reading from
    22  // Source except the diff will be applied to the end result.
    23  //
    24  // The "Exists" field on the result will be set to true if the complete
    25  // field exists whether its from the source, diff, or a combination of both.
    26  // It cannot be determined whether a retrieved value is composed of
    27  // diff elements.
    28  type DiffFieldReader struct {
    29  	Diff   *terraform.InstanceDiff
    30  	Source FieldReader
    31  	Schema map[string]*Schema
    32  }
    33  
    34  func (r *DiffFieldReader) ReadField(address []string) (FieldReadResult, error) {
    35  	schemaList := addrToSchema(address, r.Schema)
    36  	if len(schemaList) == 0 {
    37  		return FieldReadResult{}, nil
    38  	}
    39  
    40  	schema := schemaList[len(schemaList)-1]
    41  	switch schema.Type {
    42  	case TypeBool:
    43  		fallthrough
    44  	case TypeFloat:
    45  		fallthrough
    46  	case TypeInt:
    47  		fallthrough
    48  	case TypeString:
    49  		return r.readPrimitive(address, schema)
    50  	case TypeList:
    51  		return readListField(r, address, schema)
    52  	case TypeMap:
    53  		return r.readMap(address, schema)
    54  	case TypeSet:
    55  		return r.readSet(address, schema)
    56  	case typeObject:
    57  		return readObjectField(r, address, schema.Elem.(map[string]*Schema))
    58  	default:
    59  		panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
    60  	}
    61  }
    62  
    63  func (r *DiffFieldReader) readMap(
    64  	address []string, schema *Schema) (FieldReadResult, error) {
    65  	result := make(map[string]interface{})
    66  	resultSet := false
    67  
    68  	// First read the map from the underlying source
    69  	source, err := r.Source.ReadField(address)
    70  	if err != nil {
    71  		return FieldReadResult{}, err
    72  	}
    73  	if source.Exists {
    74  		result = source.Value.(map[string]interface{})
    75  		resultSet = true
    76  	}
    77  
    78  	// Next, read all the elements we have in our diff, and apply
    79  	// the diff to our result.
    80  	prefix := strings.Join(address, ".") + "."
    81  	for k, v := range r.Diff.Attributes {
    82  		if !strings.HasPrefix(k, prefix) {
    83  			continue
    84  		}
    85  		resultSet = true
    86  
    87  		k = k[len(prefix):]
    88  		if v.NewRemoved {
    89  			delete(result, k)
    90  			continue
    91  		}
    92  
    93  		result[k] = v.New
    94  	}
    95  
    96  	var resultVal interface{}
    97  	if resultSet {
    98  		resultVal = result
    99  	}
   100  
   101  	return FieldReadResult{
   102  		Value:  resultVal,
   103  		Exists: resultSet,
   104  	}, nil
   105  }
   106  
   107  func (r *DiffFieldReader) readPrimitive(
   108  	address []string, schema *Schema) (FieldReadResult, error) {
   109  	result, err := r.Source.ReadField(address)
   110  	if err != nil {
   111  		return FieldReadResult{}, err
   112  	}
   113  
   114  	attrD, ok := r.Diff.Attributes[strings.Join(address, ".")]
   115  	if !ok {
   116  		return result, nil
   117  	}
   118  
   119  	var resultVal string
   120  	if !attrD.NewComputed {
   121  		resultVal = attrD.New
   122  		if attrD.NewExtra != nil {
   123  			result.ValueProcessed = resultVal
   124  			if err := mapstructure.WeakDecode(attrD.NewExtra, &resultVal); err != nil {
   125  				return FieldReadResult{}, err
   126  			}
   127  		}
   128  	}
   129  
   130  	result.Computed = attrD.NewComputed
   131  	result.Exists = true
   132  	result.Value, err = stringToPrimitive(resultVal, false, schema)
   133  	if err != nil {
   134  		return FieldReadResult{}, err
   135  	}
   136  
   137  	return result, nil
   138  }
   139  
   140  func (r *DiffFieldReader) readSet(
   141  	address []string, schema *Schema) (FieldReadResult, error) {
   142  	// Create the set that will be our result
   143  	set := &Set{F: schema.Set}
   144  
   145  	// Go through the map and find all the set items
   146  	prefix := strings.Join(address, ".") + "."
   147  	for k, _ := range r.Diff.Attributes {
   148  		if !strings.HasPrefix(k, prefix) {
   149  			continue
   150  		}
   151  		if strings.HasPrefix(k, prefix+"#") {
   152  			// Ignore the count field
   153  			continue
   154  		}
   155  
   156  		// Split the key, since it might be a sub-object like "idx.field"
   157  		parts := strings.Split(k[len(prefix):], ".")
   158  		idx := parts[0]
   159  
   160  		raw, err := r.ReadField(append(address, idx))
   161  		if err != nil {
   162  			return FieldReadResult{}, err
   163  		}
   164  		if !raw.Exists {
   165  			// This shouldn't happen because we just verified it does exist
   166  			panic("missing field in set: " + k + "." + idx)
   167  		}
   168  
   169  		set.Add(raw.Value)
   170  	}
   171  
   172  	return FieldReadResult{
   173  		Value:  set,
   174  		Exists: set.Len() > 0,
   175  	}, nil
   176  }