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