github.com/opentofu/opentofu@v1.7.1/internal/legacy/helper/schema/resource_data.go (about)

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