github.com/rmenn/terraform@v0.3.8-0.20150225065417-fc84b3a78802/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  	return d.setWriter.WriteField(strings.Split(key, "."), value)
   141  }
   142  
   143  // SetPartial adds the key to the final state output while
   144  // in partial state mode. The key must be a root key in the schema (i.e.
   145  // it cannot be "list.0").
   146  //
   147  // If partial state mode is disabled, then this has no effect. Additionally,
   148  // whenever partial state mode is toggled, the partial data is cleared.
   149  func (d *ResourceData) SetPartial(k string) {
   150  	if d.partial {
   151  		d.partialMap[k] = struct{}{}
   152  	}
   153  }
   154  
   155  // Id returns the ID of the resource.
   156  func (d *ResourceData) Id() string {
   157  	var result string
   158  
   159  	if d.state != nil {
   160  		result = d.state.ID
   161  	}
   162  
   163  	if d.newState != nil {
   164  		result = d.newState.ID
   165  	}
   166  
   167  	return result
   168  }
   169  
   170  // ConnInfo returns the connection info for this resource.
   171  func (d *ResourceData) ConnInfo() map[string]string {
   172  	if d.newState != nil {
   173  		return d.newState.Ephemeral.ConnInfo
   174  	}
   175  
   176  	if d.state != nil {
   177  		return d.state.Ephemeral.ConnInfo
   178  	}
   179  
   180  	return nil
   181  }
   182  
   183  // SetId sets the ID of the resource. If the value is blank, then the
   184  // resource is destroyed.
   185  func (d *ResourceData) SetId(v string) {
   186  	d.once.Do(d.init)
   187  	d.newState.ID = v
   188  }
   189  
   190  // SetConnInfo sets the connection info for a resource.
   191  func (d *ResourceData) SetConnInfo(v map[string]string) {
   192  	d.once.Do(d.init)
   193  	d.newState.Ephemeral.ConnInfo = v
   194  }
   195  
   196  // State returns the new InstanceState after the diff and any Set
   197  // calls.
   198  func (d *ResourceData) State() *terraform.InstanceState {
   199  	var result terraform.InstanceState
   200  	result.ID = d.Id()
   201  
   202  	// If we have no ID, then this resource doesn't exist and we just
   203  	// return nil.
   204  	if result.ID == "" {
   205  		return nil
   206  	}
   207  
   208  	// In order to build the final state attributes, we read the full
   209  	// attribute set as a map[string]interface{}, write it to a MapFieldWriter,
   210  	// and then use that map.
   211  	rawMap := make(map[string]interface{})
   212  	for k, _ := range d.schema {
   213  		source := getSourceSet
   214  		if d.partial {
   215  			source = getSourceState
   216  			if _, ok := d.partialMap[k]; ok {
   217  				source = getSourceSet
   218  			}
   219  		}
   220  
   221  		raw := d.get([]string{k}, source)
   222  		if raw.Exists && !raw.Computed {
   223  			rawMap[k] = raw.Value
   224  			if raw.ValueProcessed != nil {
   225  				rawMap[k] = raw.ValueProcessed
   226  			}
   227  		}
   228  	}
   229  	mapW := &MapFieldWriter{Schema: d.schema}
   230  	if err := mapW.WriteField(nil, rawMap); err != nil {
   231  		return nil
   232  	}
   233  
   234  	result.Attributes = mapW.Map()
   235  	result.Ephemeral.ConnInfo = d.ConnInfo()
   236  
   237  	// TODO: This is hacky and we can remove this when we have a proper
   238  	// state writer. We should instead have a proper StateFieldWriter
   239  	// and use that.
   240  	for k, schema := range d.schema {
   241  		if schema.Type != TypeMap {
   242  			continue
   243  		}
   244  
   245  		if result.Attributes[k] == "" {
   246  			delete(result.Attributes, k)
   247  		}
   248  	}
   249  
   250  	if v := d.Id(); v != "" {
   251  		result.Attributes["id"] = d.Id()
   252  	}
   253  
   254  	return &result
   255  }
   256  
   257  func (d *ResourceData) init() {
   258  	// Initialize the field that will store our new state
   259  	var copyState terraform.InstanceState
   260  	if d.state != nil {
   261  		copyState = *d.state
   262  	}
   263  	d.newState = &copyState
   264  
   265  	// Initialize the map for storing set data
   266  	d.setWriter = &MapFieldWriter{Schema: d.schema}
   267  
   268  	// Initialize the reader for getting data from the
   269  	// underlying sources (config, diff, etc.)
   270  	readers := make(map[string]FieldReader)
   271  	var stateAttributes map[string]string
   272  	if d.state != nil {
   273  		stateAttributes = d.state.Attributes
   274  		readers["state"] = &MapFieldReader{
   275  			Schema: d.schema,
   276  			Map:    BasicMapReader(stateAttributes),
   277  		}
   278  	}
   279  	if d.config != nil {
   280  		readers["config"] = &ConfigFieldReader{
   281  			Schema: d.schema,
   282  			Config: d.config,
   283  		}
   284  	}
   285  	if d.diff != nil {
   286  		readers["diff"] = &DiffFieldReader{
   287  			Schema: d.schema,
   288  			Diff:   d.diff,
   289  			Source: &MultiLevelFieldReader{
   290  				Levels:  []string{"state", "config"},
   291  				Readers: readers,
   292  			},
   293  		}
   294  	}
   295  	readers["set"] = &MapFieldReader{
   296  		Schema: d.schema,
   297  		Map:    BasicMapReader(d.setWriter.Map()),
   298  	}
   299  	d.multiReader = &MultiLevelFieldReader{
   300  		Levels: []string{
   301  			"state",
   302  			"config",
   303  			"diff",
   304  			"set",
   305  		},
   306  
   307  		Readers: readers,
   308  	}
   309  }
   310  
   311  func (d *ResourceData) diffChange(
   312  	k string) (interface{}, interface{}, bool, bool) {
   313  	// Get the change between the state and the config.
   314  	o, n := d.getChange(k, getSourceState, getSourceConfig|getSourceExact)
   315  	if !o.Exists {
   316  		o.Value = nil
   317  	}
   318  	if !n.Exists {
   319  		n.Value = nil
   320  	}
   321  
   322  	// Return the old, new, and whether there is a change
   323  	return o.Value, n.Value, !reflect.DeepEqual(o.Value, n.Value), n.Computed
   324  }
   325  
   326  func (d *ResourceData) getChange(
   327  	key string,
   328  	oldLevel getSource,
   329  	newLevel getSource) (getResult, getResult) {
   330  	var parts, parts2 []string
   331  	if key != "" {
   332  		parts = strings.Split(key, ".")
   333  		parts2 = strings.Split(key, ".")
   334  	}
   335  
   336  	o := d.get(parts, oldLevel)
   337  	n := d.get(parts2, newLevel)
   338  	return o, n
   339  }
   340  
   341  func (d *ResourceData) get(addr []string, source getSource) getResult {
   342  	d.once.Do(d.init)
   343  
   344  	level := "set"
   345  	flags := source & ^getSourceLevelMask
   346  	exact := flags&getSourceExact != 0
   347  	source = source & getSourceLevelMask
   348  	if source >= getSourceSet {
   349  		level = "set"
   350  	} else if source >= getSourceDiff {
   351  		level = "diff"
   352  	} else if source >= getSourceConfig {
   353  		level = "config"
   354  	} else {
   355  		level = "state"
   356  	}
   357  
   358  	// Build the address of the key we're looking for and ask the FieldReader
   359  	for i, v := range addr {
   360  		if v[0] == '~' {
   361  			addr[i] = v[1:]
   362  		}
   363  	}
   364  
   365  	var result FieldReadResult
   366  	var err error
   367  	if exact {
   368  		result, err = d.multiReader.ReadFieldExact(addr, level)
   369  	} else {
   370  		result, err = d.multiReader.ReadFieldMerge(addr, level)
   371  	}
   372  	if err != nil {
   373  		panic(err)
   374  	}
   375  
   376  	// If the result doesn't exist, then we set the value to the zero value
   377  	var schema *Schema
   378  	if schemaL := addrToSchema(addr, d.schema); len(schemaL) > 0 {
   379  		schema = schemaL[len(schemaL)-1]
   380  	}
   381  
   382  	if result.Value == nil && schema != nil {
   383  		result.Value = result.ValueOrZero(schema)
   384  	}
   385  
   386  	// Transform the FieldReadResult into a getResult. It might be worth
   387  	// merging these two structures one day.
   388  	return getResult{
   389  		Value:          result.Value,
   390  		ValueProcessed: result.ValueProcessed,
   391  		Computed:       result.Computed,
   392  		Exists:         result.Exists,
   393  		Schema:         schema,
   394  	}
   395  }