github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/resource_data.go (about) 1 package schema 2 3 import ( 4 "log" 5 "reflect" 6 "strings" 7 "sync" 8 "time" 9 10 "github.com/hashicorp/terraform-plugin-sdk/terraform" 11 ) 12 13 // ResourceData is used to query and set the attributes of a resource. 14 // 15 // ResourceData is the primary argument received for CRUD operations on 16 // a resource as well as configuration of a provider. It is a powerful 17 // structure that can be used to not only query data, but check for changes, 18 // define partial state updates, etc. 19 // 20 // The most relevant methods to take a look at are Get, Set, and Partial. 21 type ResourceData struct { 22 // Settable (internally) 23 schema map[string]*Schema 24 config *terraform.ResourceConfig 25 state *terraform.InstanceState 26 diff *terraform.InstanceDiff 27 meta map[string]interface{} 28 timeouts *ResourceTimeout 29 30 // Don't set 31 multiReader *MultiLevelFieldReader 32 setWriter *MapFieldWriter 33 newState *terraform.InstanceState 34 partial bool 35 partialMap map[string]struct{} 36 once sync.Once 37 isNew bool 38 39 panicOnError bool 40 } 41 42 // getResult is the internal structure that is generated when a Get 43 // is called that contains some extra data that might be used. 44 type getResult struct { 45 Value interface{} 46 ValueProcessed interface{} 47 Computed bool 48 Exists bool 49 Schema *Schema 50 } 51 52 // UnsafeSetFieldRaw allows setting arbitrary values in state to arbitrary 53 // values, bypassing schema. This MUST NOT be used in normal circumstances - 54 // it exists only to support the remote_state data source. 55 // 56 // Deprecated: Fully define schema attributes and use Set() instead. 57 func (d *ResourceData) UnsafeSetFieldRaw(key string, value string) { 58 d.once.Do(d.init) 59 60 d.setWriter.unsafeWriteField(key, value) 61 } 62 63 // Get returns the data for the given key, or nil if the key doesn't exist 64 // in the schema. 65 // 66 // If the key does exist in the schema but doesn't exist in the configuration, 67 // then the default value for that type will be returned. For strings, this is 68 // "", for numbers it is 0, etc. 69 // 70 // If you want to test if something is set at all in the configuration, 71 // use GetOk. 72 func (d *ResourceData) Get(key string) interface{} { 73 v, _ := d.GetOk(key) 74 return v 75 } 76 77 // GetChange returns the old and new value for a given key. 78 // 79 // HasChange should be used to check if a change exists. It is possible 80 // that both the old and new value are the same if the old value was not 81 // set and the new value is. This is common, for example, for boolean 82 // fields which have a zero value of false. 83 func (d *ResourceData) GetChange(key string) (interface{}, interface{}) { 84 o, n := d.getChange(key, getSourceState, getSourceDiff) 85 return o.Value, n.Value 86 } 87 88 // GetOk returns the data for the given key and whether or not the key 89 // has been set to a non-zero value at some point. 90 // 91 // The first result will not necessarilly be nil if the value doesn't exist. 92 // The second result should be checked to determine this information. 93 func (d *ResourceData) GetOk(key string) (interface{}, bool) { 94 r := d.getRaw(key, getSourceSet) 95 exists := r.Exists && !r.Computed 96 if exists { 97 // If it exists, we also want to verify it is not the zero-value. 98 value := r.Value 99 zero := r.Schema.Type.Zero() 100 101 if eq, ok := value.(Equal); ok { 102 exists = !eq.Equal(zero) 103 } else { 104 exists = !reflect.DeepEqual(value, zero) 105 } 106 } 107 108 return r.Value, exists 109 } 110 111 // GetOkExists can check if TypeBool attributes that are Optional with 112 // no Default value have been set. 113 // 114 // Deprecated: usage is discouraged due to undefined behaviors and may be 115 // removed in a future version of the SDK 116 func (d *ResourceData) GetOkExists(key string) (interface{}, bool) { 117 r := d.getRaw(key, getSourceSet) 118 exists := r.Exists && !r.Computed 119 return r.Value, exists 120 } 121 122 func (d *ResourceData) getRaw(key string, level getSource) getResult { 123 var parts []string 124 if key != "" { 125 parts = strings.Split(key, ".") 126 } 127 128 return d.get(parts, level) 129 } 130 131 // HasChanges returns whether or not any of the given keys has been changed. 132 func (d *ResourceData) HasChanges(keys ...string) bool { 133 for _, key := range keys { 134 if d.HasChange(key) { 135 return true 136 } 137 } 138 return false 139 } 140 141 // HasChange returns whether or not the given key has been changed. 142 func (d *ResourceData) HasChange(key string) bool { 143 o, n := d.GetChange(key) 144 145 // If the type implements the Equal interface, then call that 146 // instead of just doing a reflect.DeepEqual. An example where this is 147 // needed is *Set 148 if eq, ok := o.(Equal); ok { 149 return !eq.Equal(n) 150 } 151 152 return !reflect.DeepEqual(o, n) 153 } 154 155 // Partial turns partial state mode on/off. 156 // 157 // When partial state mode is enabled, then only key prefixes specified 158 // by SetPartial will be in the final state. This allows providers to return 159 // partial states for partially applied resources (when errors occur). 160 func (d *ResourceData) Partial(on bool) { 161 d.partial = on 162 if on { 163 if d.partialMap == nil { 164 d.partialMap = make(map[string]struct{}) 165 } 166 } else { 167 d.partialMap = nil 168 } 169 } 170 171 // Set sets the value for the given key. 172 // 173 // If the key is invalid or the value is not a correct type, an error 174 // will be returned. 175 func (d *ResourceData) Set(key string, value interface{}) error { 176 d.once.Do(d.init) 177 178 // If the value is a pointer to a non-struct, get its value and 179 // use that. This allows Set to take a pointer to primitives to 180 // simplify the interface. 181 reflectVal := reflect.ValueOf(value) 182 if reflectVal.Kind() == reflect.Ptr { 183 if reflectVal.IsNil() { 184 // If the pointer is nil, then the value is just nil 185 value = nil 186 } else { 187 // Otherwise, we dereference the pointer as long as its not 188 // a pointer to a struct, since struct pointers are allowed. 189 reflectVal = reflect.Indirect(reflectVal) 190 if reflectVal.Kind() != reflect.Struct { 191 value = reflectVal.Interface() 192 } 193 } 194 } 195 196 err := d.setWriter.WriteField(strings.Split(key, "."), value) 197 if err != nil && d.panicOnError { 198 panic(err) 199 } 200 return err 201 } 202 203 // SetPartial adds the key to the final state output while 204 // in partial state mode. The key must be a root key in the schema (i.e. 205 // it cannot be "list.0"). 206 // 207 // If partial state mode is disabled, then this has no effect. Additionally, 208 // whenever partial state mode is toggled, the partial data is cleared. 209 // 210 // Deprecated: Partial state has very limited benefit given Terraform refreshes 211 // before operations by default. 212 func (d *ResourceData) SetPartial(k string) { 213 if d.partial { 214 d.partialMap[k] = struct{}{} 215 } 216 } 217 218 func (d *ResourceData) MarkNewResource() { 219 d.isNew = true 220 } 221 222 func (d *ResourceData) IsNewResource() bool { 223 return d.isNew 224 } 225 226 // Id returns the ID of the resource. 227 func (d *ResourceData) Id() string { 228 var result string 229 230 if d.state != nil { 231 result = d.state.ID 232 if result == "" { 233 result = d.state.Attributes["id"] 234 } 235 } 236 237 if d.newState != nil { 238 result = d.newState.ID 239 if result == "" { 240 result = d.newState.Attributes["id"] 241 } 242 } 243 244 return result 245 } 246 247 // ConnInfo returns the connection info for this resource. 248 func (d *ResourceData) ConnInfo() map[string]string { 249 if d.newState != nil { 250 return d.newState.Ephemeral.ConnInfo 251 } 252 253 if d.state != nil { 254 return d.state.Ephemeral.ConnInfo 255 } 256 257 return nil 258 } 259 260 // SetId sets the ID of the resource. If the value is blank, then the 261 // resource is destroyed. 262 func (d *ResourceData) SetId(v string) { 263 d.once.Do(d.init) 264 d.newState.ID = v 265 266 // once we transition away from the legacy state types, "id" will no longer 267 // be a special field, and will become a normal attribute. 268 // set the attribute normally 269 d.setWriter.unsafeWriteField("id", v) 270 271 // Make sure the newState is also set, otherwise the old value 272 // may get precedence. 273 if d.newState.Attributes == nil { 274 d.newState.Attributes = map[string]string{} 275 } 276 d.newState.Attributes["id"] = v 277 } 278 279 // SetConnInfo sets the connection info for a resource. 280 func (d *ResourceData) SetConnInfo(v map[string]string) { 281 d.once.Do(d.init) 282 d.newState.Ephemeral.ConnInfo = v 283 } 284 285 // SetType sets the ephemeral type for the data. This is only required 286 // for importing. 287 func (d *ResourceData) SetType(t string) { 288 d.once.Do(d.init) 289 d.newState.Ephemeral.Type = t 290 } 291 292 // State returns the new InstanceState after the diff and any Set 293 // calls. 294 func (d *ResourceData) State() *terraform.InstanceState { 295 var result terraform.InstanceState 296 result.ID = d.Id() 297 result.Meta = d.meta 298 299 // If we have no ID, then this resource doesn't exist and we just 300 // return nil. 301 if result.ID == "" { 302 return nil 303 } 304 305 if d.timeouts != nil { 306 if err := d.timeouts.StateEncode(&result); err != nil { 307 log.Printf("[ERR] Error encoding Timeout meta to Instance State: %s", err) 308 } 309 } 310 311 // Look for a magic key in the schema that determines we skip the 312 // integrity check of fields existing in the schema, allowing dynamic 313 // keys to be created. 314 hasDynamicAttributes := false 315 for k := range d.schema { 316 if k == "__has_dynamic_attributes" { 317 hasDynamicAttributes = true 318 log.Printf("[INFO] Resource %s has dynamic attributes", result.ID) 319 } 320 } 321 322 // In order to build the final state attributes, we read the full 323 // attribute set as a map[string]interface{}, write it to a MapFieldWriter, 324 // and then use that map. 325 rawMap := make(map[string]interface{}) 326 for k := range d.schema { 327 source := getSourceSet 328 if d.partial { 329 source = getSourceState 330 if _, ok := d.partialMap[k]; ok { 331 source = getSourceSet 332 } 333 } 334 335 raw := d.get([]string{k}, source) 336 if raw.Exists && !raw.Computed { 337 rawMap[k] = raw.Value 338 if raw.ValueProcessed != nil { 339 rawMap[k] = raw.ValueProcessed 340 } 341 } 342 } 343 344 mapW := &MapFieldWriter{Schema: d.schema} 345 if err := mapW.WriteField(nil, rawMap); err != nil { 346 log.Printf("[ERR] Error writing fields: %s", err) 347 return nil 348 } 349 350 result.Attributes = mapW.Map() 351 352 if hasDynamicAttributes { 353 // If we have dynamic attributes, just copy the attributes map 354 // one for one into the result attributes. 355 for k, v := range d.setWriter.Map() { 356 // Don't clobber schema values. This limits usage of dynamic 357 // attributes to names which _do not_ conflict with schema 358 // keys! 359 if _, ok := result.Attributes[k]; !ok { 360 result.Attributes[k] = v 361 } 362 } 363 } 364 365 if d.newState != nil { 366 result.Ephemeral = d.newState.Ephemeral 367 } 368 369 // TODO: This is hacky and we can remove this when we have a proper 370 // state writer. We should instead have a proper StateFieldWriter 371 // and use that. 372 for k, schema := range d.schema { 373 if schema.Type != TypeMap { 374 continue 375 } 376 377 if result.Attributes[k] == "" { 378 delete(result.Attributes, k) 379 } 380 } 381 382 if v := d.Id(); v != "" { 383 result.Attributes["id"] = d.Id() 384 } 385 386 if d.state != nil { 387 result.Tainted = d.state.Tainted 388 } 389 390 return &result 391 } 392 393 // Timeout returns the data for the given timeout key 394 // Returns a duration of 20 minutes for any key not found, or not found and no default. 395 func (d *ResourceData) Timeout(key string) time.Duration { 396 key = strings.ToLower(key) 397 398 // System default of 20 minutes 399 defaultTimeout := 20 * time.Minute 400 401 if d.timeouts == nil { 402 return defaultTimeout 403 } 404 405 var timeout *time.Duration 406 switch key { 407 case TimeoutCreate: 408 timeout = d.timeouts.Create 409 case TimeoutRead: 410 timeout = d.timeouts.Read 411 case TimeoutUpdate: 412 timeout = d.timeouts.Update 413 case TimeoutDelete: 414 timeout = d.timeouts.Delete 415 } 416 417 if timeout != nil { 418 return *timeout 419 } 420 421 if d.timeouts.Default != nil { 422 return *d.timeouts.Default 423 } 424 425 return defaultTimeout 426 } 427 428 func (d *ResourceData) init() { 429 // Initialize the field that will store our new state 430 var copyState terraform.InstanceState 431 if d.state != nil { 432 copyState = *d.state.DeepCopy() 433 } 434 d.newState = ©State 435 436 // Initialize the map for storing set data 437 d.setWriter = &MapFieldWriter{Schema: d.schema} 438 439 // Initialize the reader for getting data from the 440 // underlying sources (config, diff, etc.) 441 readers := make(map[string]FieldReader) 442 var stateAttributes map[string]string 443 if d.state != nil { 444 stateAttributes = d.state.Attributes 445 readers["state"] = &MapFieldReader{ 446 Schema: d.schema, 447 Map: BasicMapReader(stateAttributes), 448 } 449 } 450 if d.config != nil { 451 readers["config"] = &ConfigFieldReader{ 452 Schema: d.schema, 453 Config: d.config, 454 } 455 } 456 if d.diff != nil { 457 readers["diff"] = &DiffFieldReader{ 458 Schema: d.schema, 459 Diff: d.diff, 460 Source: &MultiLevelFieldReader{ 461 Levels: []string{"state", "config"}, 462 Readers: readers, 463 }, 464 } 465 } 466 readers["set"] = &MapFieldReader{ 467 Schema: d.schema, 468 Map: BasicMapReader(d.setWriter.Map()), 469 } 470 d.multiReader = &MultiLevelFieldReader{ 471 Levels: []string{ 472 "state", 473 "config", 474 "diff", 475 "set", 476 }, 477 478 Readers: readers, 479 } 480 } 481 482 func (d *ResourceData) diffChange( 483 k string) (interface{}, interface{}, bool, bool, bool) { 484 // Get the change between the state and the config. 485 o, n := d.getChange(k, getSourceState, getSourceConfig|getSourceExact) 486 if !o.Exists { 487 o.Value = nil 488 } 489 if !n.Exists { 490 n.Value = nil 491 } 492 493 // Return the old, new, and whether there is a change 494 return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value), n.Computed, false 495 } 496 497 func (d *ResourceData) getChange( 498 k string, 499 oldLevel getSource, 500 newLevel getSource) (getResult, getResult) { 501 var parts, parts2 []string 502 if k != "" { 503 parts = strings.Split(k, ".") 504 parts2 = strings.Split(k, ".") 505 } 506 507 o := d.get(parts, oldLevel) 508 n := d.get(parts2, newLevel) 509 return o, n 510 } 511 512 func (d *ResourceData) get(addr []string, source getSource) getResult { 513 d.once.Do(d.init) 514 515 level := "set" 516 flags := source & ^getSourceLevelMask 517 exact := flags&getSourceExact != 0 518 source = source & getSourceLevelMask 519 if source >= getSourceSet { 520 level = "set" 521 } else if source >= getSourceDiff { 522 level = "diff" 523 } else if source >= getSourceConfig { 524 level = "config" 525 } else { 526 level = "state" 527 } 528 529 var result FieldReadResult 530 var err error 531 if exact { 532 result, err = d.multiReader.ReadFieldExact(addr, level) 533 } else { 534 result, err = d.multiReader.ReadFieldMerge(addr, level) 535 } 536 if err != nil { 537 panic(err) 538 } 539 540 // If the result doesn't exist, then we set the value to the zero value 541 var schema *Schema 542 if schemaL := addrToSchema(addr, d.schema); len(schemaL) > 0 { 543 schema = schemaL[len(schemaL)-1] 544 } 545 546 if result.Value == nil && schema != nil { 547 result.Value = result.ValueOrZero(schema) 548 } 549 550 // Transform the FieldReadResult into a getResult. It might be worth 551 // merging these two structures one day. 552 return getResult{ 553 Value: result.Value, 554 ValueProcessed: result.ValueProcessed, 555 Computed: result.Computed, 556 Exists: result.Exists, 557 Schema: schema, 558 } 559 }