github.com/bigkraig/terraform@v0.6.4-0.20151219155159-c90d1b074e31/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, TypeInt, TypeFloat, TypeString:
    43  		return r.readPrimitive(address, schema)
    44  	case TypeList:
    45  		return readListField(r, address, schema)
    46  	case TypeMap:
    47  		return r.readMap(address, schema)
    48  	case TypeSet:
    49  		return r.readSet(address, schema)
    50  	case typeObject:
    51  		return readObjectField(r, address, schema.Elem.(map[string]*Schema))
    52  	default:
    53  		panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
    54  	}
    55  }
    56  
    57  func (r *DiffFieldReader) readMap(
    58  	address []string, schema *Schema) (FieldReadResult, error) {
    59  	result := make(map[string]interface{})
    60  	resultSet := false
    61  
    62  	// First read the map from the underlying source
    63  	source, err := r.Source.ReadField(address)
    64  	if err != nil {
    65  		return FieldReadResult{}, err
    66  	}
    67  	if source.Exists {
    68  		result = source.Value.(map[string]interface{})
    69  		resultSet = true
    70  	}
    71  
    72  	// Next, read all the elements we have in our diff, and apply
    73  	// the diff to our result.
    74  	prefix := strings.Join(address, ".") + "."
    75  	for k, v := range r.Diff.Attributes {
    76  		if !strings.HasPrefix(k, prefix) {
    77  			continue
    78  		}
    79  		if strings.HasPrefix(k, prefix+"#") {
    80  			// Ignore the count field
    81  			continue
    82  		}
    83  
    84  		resultSet = true
    85  
    86  		k = k[len(prefix):]
    87  		if v.NewRemoved {
    88  			delete(result, k)
    89  			continue
    90  		}
    91  
    92  		result[k] = v.New
    93  	}
    94  
    95  	var resultVal interface{}
    96  	if resultSet {
    97  		resultVal = result
    98  	}
    99  
   100  	return FieldReadResult{
   101  		Value:  resultVal,
   102  		Exists: resultSet,
   103  	}, nil
   104  }
   105  
   106  func (r *DiffFieldReader) readPrimitive(
   107  	address []string, schema *Schema) (FieldReadResult, error) {
   108  	result, err := r.Source.ReadField(address)
   109  	if err != nil {
   110  		return FieldReadResult{}, err
   111  	}
   112  
   113  	attrD, ok := r.Diff.Attributes[strings.Join(address, ".")]
   114  	if !ok {
   115  		return result, nil
   116  	}
   117  
   118  	var resultVal string
   119  	if !attrD.NewComputed {
   120  		resultVal = attrD.New
   121  		if attrD.NewExtra != nil {
   122  			result.ValueProcessed = resultVal
   123  			if err := mapstructure.WeakDecode(attrD.NewExtra, &resultVal); err != nil {
   124  				return FieldReadResult{}, err
   125  			}
   126  		}
   127  	}
   128  
   129  	result.Computed = attrD.NewComputed
   130  	result.Exists = true
   131  	result.Value, err = stringToPrimitive(resultVal, false, schema)
   132  	if err != nil {
   133  		return FieldReadResult{}, err
   134  	}
   135  
   136  	return result, nil
   137  }
   138  
   139  func (r *DiffFieldReader) readSet(
   140  	address []string, schema *Schema) (FieldReadResult, error) {
   141  	prefix := strings.Join(address, ".") + "."
   142  
   143  	// Create the set that will be our result
   144  	set := schema.ZeroValue().(*Set)
   145  
   146  	// Go through the map and find all the set items
   147  	for k, d := range r.Diff.Attributes {
   148  		if d.NewRemoved {
   149  			// If the field is removed, we always ignore it
   150  			continue
   151  		}
   152  		if !strings.HasPrefix(k, prefix) {
   153  			continue
   154  		}
   155  		if strings.HasSuffix(k, "#") {
   156  			// Ignore any count field
   157  			continue
   158  		}
   159  
   160  		// Split the key, since it might be a sub-object like "idx.field"
   161  		parts := strings.Split(k[len(prefix):], ".")
   162  		idx := parts[0]
   163  
   164  		raw, err := r.ReadField(append(address, idx))
   165  		if err != nil {
   166  			return FieldReadResult{}, err
   167  		}
   168  		if !raw.Exists {
   169  			// This shouldn't happen because we just verified it does exist
   170  			panic("missing field in set: " + k + "." + idx)
   171  		}
   172  
   173  		set.Add(raw.Value)
   174  	}
   175  
   176  	// Determine if the set "exists". It exists if there are items or if
   177  	// the diff explicitly wanted it empty.
   178  	exists := set.Len() > 0
   179  	if !exists {
   180  		// We could check if the diff value is "0" here but I think the
   181  		// existence of "#" on its own is enough to show it existed. This
   182  		// protects us in the future from the zero value changing from
   183  		// "0" to "" breaking us (if that were to happen).
   184  		if _, ok := r.Diff.Attributes[prefix+"#"]; ok {
   185  			exists = true
   186  		}
   187  	}
   188  
   189  	return FieldReadResult{
   190  		Value:  set,
   191  		Exists: exists,
   192  	}, nil
   193  }