10 "github.com/hashicorp/terraform/terraform"
13 // ResourceData is used to query and set the attributes of a resource.
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.
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
31 multiReader *MultiLevelFieldReader
32 setWriter *MapFieldWriter
33 newState *terraform.InstanceState
35 partialMap map[string]struct{}
40 // getResult is the internal structure that is generated when a Get
41 // is called that contains some extra data that might be used.
42 type getResult struct {
44 ValueProcessed interface{}
50 // UnsafeSetFieldRaw allows setting arbitrary values in state to arbitrary
51 // values, bypassing schema. This MUST NOT be used in normal circumstances -
52 // it exists only to support the remote_state data source.
53 func (d *ResourceData) UnsafeSetFieldRaw(key string, value string) {
56 d.setWriter.unsafeWriteField(key, value)
59 // Get returns the data for the given key, or nil if the key doesn't exist
62 // If the key does exist in the schema but doesn't exist in the configuration,
63 // then the default value for that type will be returned. For strings, this is
64 // "", for numbers it is 0, etc.
66 // If you want to test if something is set at all in the configuration,
68 func (d *ResourceData) Get(key string) interface{} {
73 // GetChange returns the old and new value for a given key.
75 // HasChange should be used to check if a change exists. It is possible
76 // that both the old and new value are the same if the old value was not
77 // set and the new value is. This is common, for example, for boolean
78 // fields which have a zero value of false.
79 func (d *ResourceData) GetChange(key string) (interface{}, interface{}) {
80 o, n := d.getChange(key, getSourceState, getSourceDiff)
81 return o.Value, n.Value
84 // GetOk returns the data for the given key and whether or not the key
85 // has been set to a non-zero value at some point.
87 // The first result will not necessarilly be nil if the value doesn't exist.
88 // The second result should be checked to determine this information.
89 func (d *ResourceData) GetOk(key string) (interface{}, bool) {
90 r := d.getRaw(key, getSourceSet)
91 exists := r.Exists && !r.Computed
93 // If it exists, we also want to verify it is not the zero-value.
95 zero := r.Schema.Type.Zero()
97 if eq, ok := value.(Equal); ok {
98 exists = !eq.Equal(zero)
100 exists = !reflect.DeepEqual(value, zero)
104 return r.Value, exists
107 func (d *ResourceData) getRaw(key string, level getSource) getResult {
110 parts = strings.Split(key, ".")
113 return d.get(parts, level)
116 // HasChange returns whether or not the given key has been changed.
117 func (d *ResourceData) HasChange(key string) bool {
118 o, n := d.GetChange(key)
120 // If the type implements the Equal interface, then call that
121 // instead of just doing a reflect.DeepEqual. An example where this is
123 if eq, ok := o.(Equal); ok {
127 return !reflect.DeepEqual(o, n)
130 // Partial turns partial state mode on/off.
132 // When partial state mode is enabled, then only key prefixes specified
133 // by SetPartial will be in the final state. This allows providers to return
134 // partial states for partially applied resources (when errors occur).
135 func (d *ResourceData) Partial(on bool) {
138 if d.partialMap == nil {
139 d.partialMap = make(map[string]struct{})
146 // Set sets the value for the given key.
148 // If the key is invalid or the value is not a correct type, an error
150 func (d *ResourceData) Set(key string, value interface{}) error {
153 // If the value is a pointer to a non-struct, get its value and
154 // use that. This allows Set to take a pointer to primitives to
155 // simplify the interface.
156 reflectVal := reflect.ValueOf(value)
157 if reflectVal.Kind() == reflect.Ptr {
158 if reflectVal.IsNil() {
159 // If the pointer is nil, then the value is just nil
162 // Otherwise, we dereference the pointer as long as its not
163 // a pointer to a struct, since struct pointers are allowed.
164 reflectVal = reflect.Indirect(reflectVal)
165 if reflectVal.Kind() != reflect.Struct {
166 value = reflectVal.Interface()
171 return d.setWriter.WriteField(strings.Split(key, "."), value)
174 // SetPartial adds the key to the final state output while
175 // in partial state mode. The key must be a root key in the schema (i.e.
176 // it cannot be "list.0").
178 // If partial state mode is disabled, then this has no effect. Additionally,
179 // whenever partial state mode is toggled, the partial data is cleared.
180 func (d *ResourceData) SetPartial(k string) {
182 d.partialMap[k] = struct{}{}
186 func (d *ResourceData) MarkNewResource() {
190 func (d *ResourceData) IsNewResource() bool {
194 // Id returns the ID of the resource.
195 func (d *ResourceData) Id() string {
202 if d.newState != nil {
203 result = d.newState.ID
209 // ConnInfo returns the connection info for this resource.
210 func (d *ResourceData) ConnInfo() map[string]string {
211 if d.newState != nil {
212 return d.newState.Ephemeral.ConnInfo
216 return d.state.Ephemeral.ConnInfo
222 // SetId sets the ID of the resource. If the value is blank, then the
223 // resource is destroyed.
224 func (d *ResourceData) SetId(v string) {
229 // SetConnInfo sets the connection info for a resource.
230 func (d *ResourceData) SetConnInfo(v map[string]string) {
232 d.newState.Ephemeral.ConnInfo = v
235 // SetType sets the ephemeral type for the data. This is only required
237 func (d *ResourceData) SetType(t string) {
239 d.newState.Ephemeral.Type = t
242 // State returns the new InstanceState after the diff and any Set
244 func (d *ResourceData) State() *terraform.InstanceState {
245 var result terraform.InstanceState
249 // If we have no ID, then this resource doesn't exist and we just
255 if d.timeouts != nil {
256 if err := d.timeouts.StateEncode(&result); err != nil {
257 log.Printf("[ERR] Error encoding Timeout meta to Instance State: %s", err)
261 // Look for a magic key in the schema that determines we skip the
262 // integrity check of fields existing in the schema, allowing dynamic
263 // keys to be created.
264 hasDynamicAttributes := false
265 for k, _ := range d.schema {
266 if k == "__has_dynamic_attributes" {
267 hasDynamicAttributes = true
268 log.Printf("[INFO] Resource %s has dynamic attributes", result.ID)
272 // In order to build the final state attributes, we read the full
273 // attribute set as a map[string]interface{}, write it to a MapFieldWriter,
274 // and then use that map.
275 rawMap := make(map[string]interface{})
276 for k := range d.schema {
277 source := getSourceSet
279 source = getSourceState
280 if _, ok := d.partialMap[k]; ok {
281 source = getSourceSet
285 raw := d.get([]string{k}, source)
286 if raw.Exists && !raw.Computed {
287 rawMap[k] = raw.Value
288 if raw.ValueProcessed != nil {
289 rawMap[k] = raw.ValueProcessed
294 mapW := &MapFieldWriter{Schema: d.schema}
295 if err := mapW.WriteField(nil, rawMap); err != nil {
299 result.Attributes = mapW.Map()
301 if hasDynamicAttributes {
302 // If we have dynamic attributes, just copy the attributes map
303 // one for one into the result attributes.
304 for k, v := range d.setWriter.Map() {
305 // Don't clobber schema values. This limits usage of dynamic
306 // attributes to names which _do not_ conflict with schema
308 if _, ok := result.Attributes[k]; !ok {
309 result.Attributes[k] = v
314 if d.newState != nil {
315 result.Ephemeral = d.newState.Ephemeral
318 // TODO: This is hacky and we can remove this when we have a proper
319 // state writer. We should instead have a proper StateFieldWriter
321 for k, schema := range d.schema {
322 if schema.Type != TypeMap {
326 if result.Attributes[k] == "" {
327 delete(result.Attributes, k)
331 if v := d.Id(); v != "" {
332 result.Attributes["id"] = d.Id()
336 result.Tainted = d.state.Tainted
342 // Timeout returns the data for the given timeout key
343 // Returns a duration of 20 minutes for any key not found, or not found and no default.
344 func (d *ResourceData) Timeout(key string) time.Duration {
345 key = strings.ToLower(key)
347 var timeout *time.Duration
350 timeout = d.timeouts.Create
352 timeout = d.timeouts.Read
354 timeout = d.timeouts.Update
356 timeout = d.timeouts.Delete
363 if d.timeouts.Default != nil {
364 return *d.timeouts.Default
367 // Return system default of 20 minutes
368 return 20 * time.Minute
371 func (d *ResourceData) init() {
372 // Initialize the field that will store our new state
373 var copyState terraform.InstanceState
375 copyState = *d.state.DeepCopy()
377 d.newState = ©State
379 // Initialize the map for storing set data
380 d.setWriter = &MapFieldWriter{Schema: d.schema}
382 // Initialize the reader for getting data from the
383 // underlying sources (config, diff, etc.)
384 readers := make(map[string]FieldReader)
385 var stateAttributes map[string]string
387 stateAttributes = d.state.Attributes
388 readers["state"] = &MapFieldReader{
390 Map: BasicMapReader(stateAttributes),
394 readers["config"] = &ConfigFieldReader{
400 readers["diff"] = &DiffFieldReader{
403 Source: &MultiLevelFieldReader{
404 Levels: []string{"state", "config"},
409 readers["set"] = &MapFieldReader{
411 Map: BasicMapReader(d.setWriter.Map()),
413 d.multiReader = &MultiLevelFieldReader{
425 func (d *ResourceData) diffChange(
426 k string) (interface{}, interface{}, bool, bool) {
427 // Get the change between the state and the config.
428 o, n := d.getChange(k, getSourceState, getSourceConfig|getSourceExact)
436 // Return the old, new, and whether there is a change
437 return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value), n.Computed
440 func (d *ResourceData) getChange(
443 newLevel getSource) (getResult, getResult) {
444 var parts, parts2 []string
446 parts = strings.Split(k, ".")
447 parts2 = strings.Split(k, ".")
450 o := d.get(parts, oldLevel)
451 n := d.get(parts2, newLevel)
455 func (d *ResourceData) get(addr []string, source getSource) getResult {
459 flags := source & ^getSourceLevelMask
460 exact := flags&getSourceExact != 0
461 source = source & getSourceLevelMask
462 if source >= getSourceSet {
464 } else if source >= getSourceDiff {
466 } else if source >= getSourceConfig {
472 var result FieldReadResult
475 result, err = d.multiReader.ReadFieldExact(addr, level)
477 result, err = d.multiReader.ReadFieldMerge(addr, level)
483 // If the result doesn't exist, then we set the value to the zero value
485 if schemaL := addrToSchema(addr, d.schema); len(schemaL) > 0 {
486 schema = schemaL[len(schemaL)-1]
489 if result.Value == nil && schema != nil {
490 result.Value = result.ValueOrZero(schema)
493 // Transform the FieldReadResult into a getResult. It might be worth
494 // merging these two structures one day.
497 ValueProcessed: result.ValueProcessed,
498 Computed: result.Computed,
499 Exists: result.Exists,