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