github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/helper/schema/resource_data.go (about)

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