github.com/xsb/terraform@v0.6.13-0.20160314145438-fe415c2f09d7/helper/schema/resource_data.go (about)

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