github.com/bigkraig/terraform@v0.6.4-0.20151219155159-c90d1b074e31/helper/schema/field_reader_config.go (about) 1 package schema 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 "sync" 8 9 "github.com/hashicorp/terraform/terraform" 10 "github.com/mitchellh/mapstructure" 11 ) 12 13 // ConfigFieldReader reads fields out of an untyped map[string]string to the 14 // best of its ability. It also applies defaults from the Schema. (The other 15 // field readers do not need default handling because they source fully 16 // populated data structures.) 17 type ConfigFieldReader struct { 18 Config *terraform.ResourceConfig 19 Schema map[string]*Schema 20 21 indexMaps map[string]map[string]int 22 once sync.Once 23 } 24 25 func (r *ConfigFieldReader) ReadField(address []string) (FieldReadResult, error) { 26 r.once.Do(func() { r.indexMaps = make(map[string]map[string]int) }) 27 return r.readField(address, false) 28 } 29 30 func (r *ConfigFieldReader) readField( 31 address []string, nested bool) (FieldReadResult, error) { 32 schemaList := addrToSchema(address, r.Schema) 33 if len(schemaList) == 0 { 34 return FieldReadResult{}, nil 35 } 36 37 if !nested { 38 // If we have a set anywhere in the address, then we need to 39 // read that set out in order and actually replace that part of 40 // the address with the real list index. i.e. set.50 might actually 41 // map to set.12 in the config, since it is in list order in the 42 // config, not indexed by set value. 43 for i, v := range schemaList { 44 // Sets are the only thing that cause this issue. 45 if v.Type != TypeSet { 46 continue 47 } 48 49 // If we're at the end of the list, then we don't have to worry 50 // about this because we're just requesting the whole set. 51 if i == len(schemaList)-1 { 52 continue 53 } 54 55 // If we're looking for the count, then ignore... 56 if address[i+1] == "#" { 57 continue 58 } 59 60 indexMap, ok := r.indexMaps[strings.Join(address[:i+1], ".")] 61 if !ok { 62 // Get the set so we can get the index map that tells us the 63 // mapping of the hash code to the list index 64 _, err := r.readSet(address[:i+1], v) 65 if err != nil { 66 return FieldReadResult{}, err 67 } 68 indexMap = r.indexMaps[strings.Join(address[:i+1], ".")] 69 } 70 71 index, ok := indexMap[address[i+1]] 72 if !ok { 73 return FieldReadResult{}, nil 74 } 75 76 address[i+1] = strconv.FormatInt(int64(index), 10) 77 } 78 } 79 80 k := strings.Join(address, ".") 81 schema := schemaList[len(schemaList)-1] 82 switch schema.Type { 83 case TypeBool, TypeFloat, TypeInt, TypeString: 84 return r.readPrimitive(k, schema) 85 case TypeList: 86 return readListField(&nestedConfigFieldReader{r}, address, schema) 87 case TypeMap: 88 return r.readMap(k) 89 case TypeSet: 90 return r.readSet(address, schema) 91 case typeObject: 92 return readObjectField( 93 &nestedConfigFieldReader{r}, 94 address, schema.Elem.(map[string]*Schema)) 95 default: 96 panic(fmt.Sprintf("Unknown type: %s", schema.Type)) 97 } 98 } 99 100 func (r *ConfigFieldReader) readMap(k string) (FieldReadResult, error) { 101 // We want both the raw value and the interpolated. We use the interpolated 102 // to store actual values and we use the raw one to check for 103 // computed keys. 104 mraw, ok := r.Config.GetRaw(k) 105 if !ok { 106 return FieldReadResult{}, nil 107 } 108 109 result := make(map[string]interface{}) 110 computed := false 111 switch m := mraw.(type) { 112 case []interface{}: 113 for i, innerRaw := range m { 114 for ik := range innerRaw.(map[string]interface{}) { 115 key := fmt.Sprintf("%s.%d.%s", k, i, ik) 116 if r.Config.IsComputed(key) { 117 computed = true 118 break 119 } 120 121 v, _ := r.Config.Get(key) 122 result[ik] = v 123 } 124 } 125 case []map[string]interface{}: 126 for i, innerRaw := range m { 127 for ik := range innerRaw { 128 key := fmt.Sprintf("%s.%d.%s", k, i, ik) 129 if r.Config.IsComputed(key) { 130 computed = true 131 break 132 } 133 134 v, _ := r.Config.Get(key) 135 result[ik] = v 136 } 137 } 138 case map[string]interface{}: 139 for ik := range m { 140 key := fmt.Sprintf("%s.%s", k, ik) 141 if r.Config.IsComputed(key) { 142 computed = true 143 break 144 } 145 146 v, _ := r.Config.Get(key) 147 result[ik] = v 148 } 149 default: 150 panic(fmt.Sprintf("unknown type: %#v", mraw)) 151 } 152 153 var value interface{} 154 if !computed { 155 value = result 156 } 157 158 return FieldReadResult{ 159 Value: value, 160 Exists: true, 161 Computed: computed, 162 }, nil 163 } 164 165 func (r *ConfigFieldReader) readPrimitive( 166 k string, schema *Schema) (FieldReadResult, error) { 167 raw, ok := r.Config.Get(k) 168 if !ok { 169 // Nothing in config, but we might still have a default from the schema 170 var err error 171 raw, err = schema.DefaultValue() 172 if err != nil { 173 return FieldReadResult{}, fmt.Errorf("%s, error loading default: %s", k, err) 174 } 175 176 if raw == nil { 177 return FieldReadResult{}, nil 178 } 179 } 180 181 var result string 182 if err := mapstructure.WeakDecode(raw, &result); err != nil { 183 return FieldReadResult{}, err 184 } 185 186 computed := r.Config.IsComputed(k) 187 returnVal, err := stringToPrimitive(result, computed, schema) 188 if err != nil { 189 return FieldReadResult{}, err 190 } 191 192 return FieldReadResult{ 193 Value: returnVal, 194 Exists: true, 195 Computed: computed, 196 }, nil 197 } 198 199 func (r *ConfigFieldReader) readSet( 200 address []string, schema *Schema) (FieldReadResult, error) { 201 indexMap := make(map[string]int) 202 // Create the set that will be our result 203 set := schema.ZeroValue().(*Set) 204 205 raw, err := readListField(&nestedConfigFieldReader{r}, address, schema) 206 if err != nil { 207 return FieldReadResult{}, err 208 } 209 if !raw.Exists { 210 return FieldReadResult{Value: set}, nil 211 } 212 213 // If the list is computed, the set is necessarilly computed 214 if raw.Computed { 215 return FieldReadResult{ 216 Value: set, 217 Exists: true, 218 Computed: raw.Computed, 219 }, nil 220 } 221 222 // Build up the set from the list elements 223 for i, v := range raw.Value.([]interface{}) { 224 // Check if any of the keys in this item are computed 225 computed := r.hasComputedSubKeys( 226 fmt.Sprintf("%s.%d", strings.Join(address, "."), i), schema) 227 228 code := set.add(v, computed) 229 indexMap[code] = i 230 } 231 232 r.indexMaps[strings.Join(address, ".")] = indexMap 233 234 return FieldReadResult{ 235 Value: set, 236 Exists: true, 237 }, nil 238 } 239 240 // hasComputedSubKeys walks through a schema and returns whether or not the 241 // given key contains any subkeys that are computed. 242 func (r *ConfigFieldReader) hasComputedSubKeys(key string, schema *Schema) bool { 243 prefix := key + "." 244 245 switch t := schema.Elem.(type) { 246 case *Resource: 247 for k, schema := range t.Schema { 248 if r.Config.IsComputed(prefix + k) { 249 return true 250 } 251 252 if r.hasComputedSubKeys(prefix+k, schema) { 253 return true 254 } 255 } 256 } 257 258 return false 259 } 260 261 // nestedConfigFieldReader is a funny little thing that just wraps a 262 // ConfigFieldReader to call readField when ReadField is called so that 263 // we don't recalculate the set rewrites in the address, which leads to 264 // an infinite loop. 265 type nestedConfigFieldReader struct { 266 Reader *ConfigFieldReader 267 } 268 269 func (r *nestedConfigFieldReader) ReadField( 270 address []string) (FieldReadResult, error) { 271 return r.Reader.readField(address, true) 272 }