7 "github.com/hashicorp/terraform/terraform"
8 "github.com/mitchellh/mapstructure"
11 // DiffFieldReader reads fields out of a diff structures.
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
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.
21 // Reading fields from a DiffFieldReader is identical to reading from
22 // Source except the diff will be applied to the end result.
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
28 type DiffFieldReader struct {
29 Diff *terraform.InstanceDiff
31 Schema map[string]*Schema
34 func (r *DiffFieldReader) ReadField(address []string) (FieldReadResult, error) {
35 schemaList := addrToSchema(address, r.Schema)
36 if len(schemaList) == 0 {
37 return FieldReadResult{}, nil
40 schema := schemaList[len(schemaList)-1]
42 case TypeBool, TypeInt, TypeFloat, TypeString:
43 return r.readPrimitive(address, schema)
45 return readListField(r, address, schema)
47 return r.readMap(address, schema)
49 return r.readSet(address, schema)
51 return readObjectField(r, address, schema.Elem.(map[string]*Schema))
53 panic(fmt.Sprintf("Unknown type: %#v", schema.Type))
57 func (r *DiffFieldReader) readMap(
58 address []string, schema *Schema) (FieldReadResult, error) {
59 result := make(map[string]interface{})
62 // First read the map from the underlying source
63 source, err := r.Source.ReadField(address)
65 return FieldReadResult{}, err
68 result = source.Value.(map[string]interface{})
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) {
79 if strings.HasPrefix(k, prefix+"%") {
80 // Ignore the count field
95 err = mapValuesToPrimitive(result, schema)
97 return FieldReadResult{}, nil
100 var resultVal interface{}
105 return FieldReadResult{
111 func (r *DiffFieldReader) readPrimitive(
112 address []string, schema *Schema) (FieldReadResult, error) {
113 result, err := r.Source.ReadField(address)
115 return FieldReadResult{}, err
118 attrD, ok := r.Diff.Attributes[strings.Join(address, ".")]
124 if !attrD.NewComputed {
125 resultVal = attrD.New
126 if attrD.NewExtra != nil {
127 result.ValueProcessed = resultVal
128 if err := mapstructure.WeakDecode(attrD.NewExtra, &resultVal); err != nil {
129 return FieldReadResult{}, err
134 result.Computed = attrD.NewComputed
136 result.Value, err = stringToPrimitive(resultVal, false, schema)
138 return FieldReadResult{}, err
144 func (r *DiffFieldReader) readSet(
145 address []string, schema *Schema) (FieldReadResult, error) {
146 prefix := strings.Join(address, ".") + "."
148 // Create the set that will be our result
149 set := schema.ZeroValue().(*Set)
151 // Go through the map and find all the set items
152 for k, d := range r.Diff.Attributes {
154 // If the field is removed, we always ignore it
157 if !strings.HasPrefix(k, prefix) {
160 if strings.HasSuffix(k, "#") {
161 // Ignore any count field
165 // Split the key, since it might be a sub-object like "idx.field"
166 parts := strings.Split(k[len(prefix):], ".")
169 raw, err := r.ReadField(append(address, idx))
171 return FieldReadResult{}, err
174 // This shouldn't happen because we just verified it does exist
175 panic("missing field in set: " + k + "." + idx)
181 // Determine if the set "exists". It exists if there are items or if
182 // the diff explicitly wanted it empty.
183 exists := set.Len() > 0
185 // We could check if the diff value is "0" here but I think the
186 // existence of "#" on its own is enough to show it existed. This
187 // protects us in the future from the zero value changing from
188 // "0" to "" breaking us (if that were to happen).
189 if _, ok := r.Diff.Attributes[prefix+"#"]; ok {
195 result, err := r.Source.ReadField(address)
197 return FieldReadResult{}, err
204 return FieldReadResult{