github.com/posener/terraform@v0.11.0-beta1.0.20171103235147-645df36af025/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  	// cache for memoizing ReadField calls.
    34  	cache map[string]cachedFieldReadResult
    35  }
    36  
    37  type cachedFieldReadResult struct {
    38  	val FieldReadResult
    39  	err error
    40  }
    41  
    42  func (r *DiffFieldReader) ReadField(address []string) (FieldReadResult, error) {
    43  	if r.cache == nil {
    44  		r.cache = make(map[string]cachedFieldReadResult)
    45  	}
    46  
    47  	// Create the cache key by joining around a value that isn't a valid part
    48  	// of an address. This assumes that the Source and Schema are not changed
    49  	// for the life of this DiffFieldReader.
    50  	cacheKey := strings.Join(address, "|")
    51  	if cached, ok := r.cache[cacheKey]; ok {
    52  		return cached.val, cached.err
    53  	}
    54  
    55  	schemaList := addrToSchema(address, r.Schema)
    56  	if len(schemaList) == 0 {
    57  		r.cache[cacheKey] = cachedFieldReadResult{}
    58  		return FieldReadResult{}, nil
    59  	}
    60  
    61  	var res FieldReadResult
    62  	var err error
    63  
    64  	schema := schemaList[len(schemaList)-1]
    65  	switch schema.Type {
    66  	case TypeBool, TypeInt, TypeFloat, TypeString:
    67  		res, err = r.readPrimitive(address, schema)
    68  	case TypeList:
    69  		res, err = readListField(r, address, schema)
    70  	case TypeMap:
    71  		res, err = r.readMap(address, schema)
    72  	case TypeSet:
    73  		res, err = r.readSet(address, schema)
    74  	case typeObject:
    75  		res, err = readObjectField(r, address, schema.Elem.(map[string]*Schema))
    76  	default:
    77  		panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
    78  	}
    79  
    80  	r.cache[cacheKey] = cachedFieldReadResult{
    81  		val: res,
    82  		err: err,
    83  	}
    84  	return res, err
    85  }
    86  
    87  func (r *DiffFieldReader) readMap(
    88  	address []string, schema *Schema) (FieldReadResult, error) {
    89  	result := make(map[string]interface{})
    90  	resultSet := false
    91  
    92  	// First read the map from the underlying source
    93  	source, err := r.Source.ReadField(address)
    94  	if err != nil {
    95  		return FieldReadResult{}, err
    96  	}
    97  	if source.Exists {
    98  		result = source.Value.(map[string]interface{})
    99  		resultSet = true
   100  	}
   101  
   102  	// Next, read all the elements we have in our diff, and apply
   103  	// the diff to our result.
   104  	prefix := strings.Join(address, ".") + "."
   105  	for k, v := range r.Diff.Attributes {
   106  		if !strings.HasPrefix(k, prefix) {
   107  			continue
   108  		}
   109  		if strings.HasPrefix(k, prefix+"%") {
   110  			// Ignore the count field
   111  			continue
   112  		}
   113  
   114  		resultSet = true
   115  
   116  		k = k[len(prefix):]
   117  		if v.NewRemoved {
   118  			delete(result, k)
   119  			continue
   120  		}
   121  
   122  		result[k] = v.New
   123  	}
   124  
   125  	err = mapValuesToPrimitive(result, schema)
   126  	if err != nil {
   127  		return FieldReadResult{}, nil
   128  	}
   129  
   130  	var resultVal interface{}
   131  	if resultSet {
   132  		resultVal = result
   133  	}
   134  
   135  	return FieldReadResult{
   136  		Value:  resultVal,
   137  		Exists: resultSet,
   138  	}, nil
   139  }
   140  
   141  func (r *DiffFieldReader) readPrimitive(
   142  	address []string, schema *Schema) (FieldReadResult, error) {
   143  	result, err := r.Source.ReadField(address)
   144  	if err != nil {
   145  		return FieldReadResult{}, err
   146  	}
   147  
   148  	attrD, ok := r.Diff.Attributes[strings.Join(address, ".")]
   149  	if !ok {
   150  		return result, nil
   151  	}
   152  
   153  	var resultVal string
   154  	if !attrD.NewComputed {
   155  		resultVal = attrD.New
   156  		if attrD.NewExtra != nil {
   157  			result.ValueProcessed = resultVal
   158  			if err := mapstructure.WeakDecode(attrD.NewExtra, &resultVal); err != nil {
   159  				return FieldReadResult{}, err
   160  			}
   161  		}
   162  	}
   163  
   164  	result.Computed = attrD.NewComputed
   165  	result.Exists = true
   166  	result.Value, err = stringToPrimitive(resultVal, false, schema)
   167  	if err != nil {
   168  		return FieldReadResult{}, err
   169  	}
   170  
   171  	return result, nil
   172  }
   173  
   174  func (r *DiffFieldReader) readSet(
   175  	address []string, schema *Schema) (FieldReadResult, error) {
   176  	prefix := strings.Join(address, ".") + "."
   177  
   178  	// Create the set that will be our result
   179  	set := schema.ZeroValue().(*Set)
   180  
   181  	// Go through the map and find all the set items
   182  	for k, d := range r.Diff.Attributes {
   183  		if d.NewRemoved {
   184  			// If the field is removed, we always ignore it
   185  			continue
   186  		}
   187  		if !strings.HasPrefix(k, prefix) {
   188  			continue
   189  		}
   190  		if strings.HasSuffix(k, "#") {
   191  			// Ignore any count field
   192  			continue
   193  		}
   194  
   195  		// Split the key, since it might be a sub-object like "idx.field"
   196  		parts := strings.Split(k[len(prefix):], ".")
   197  		idx := parts[0]
   198  
   199  		raw, err := r.ReadField(append(address, idx))
   200  		if err != nil {
   201  			return FieldReadResult{}, err
   202  		}
   203  		if !raw.Exists {
   204  			// This shouldn't happen because we just verified it does exist
   205  			panic("missing field in set: " + k + "." + idx)
   206  		}
   207  
   208  		set.Add(raw.Value)
   209  	}
   210  
   211  	// Determine if the set "exists". It exists if there are items or if
   212  	// the diff explicitly wanted it empty.
   213  	exists := set.Len() > 0
   214  	if !exists {
   215  		// We could check if the diff value is "0" here but I think the
   216  		// existence of "#" on its own is enough to show it existed. This
   217  		// protects us in the future from the zero value changing from
   218  		// "0" to "" breaking us (if that were to happen).
   219  		if _, ok := r.Diff.Attributes[prefix+"#"]; ok {
   220  			exists = true
   221  		}
   222  	}
   223  
   224  	if !exists {
   225  		result, err := r.Source.ReadField(address)
   226  		if err != nil {
   227  			return FieldReadResult{}, err
   228  		}
   229  		if result.Exists {
   230  			return result, nil
   231  		}
   232  	}
   233  
   234  	return FieldReadResult{
   235  		Value:  set,
   236  		Exists: exists,
   237  	}, nil
   238  }