github.com/posener/terraform@v0.11.0-beta1.0.20171103235147-645df36af025/helper/schema/resource.go (about)

     1  package schema
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"log"
     7  	"strconv"
     8  
     9  	"github.com/hashicorp/terraform/config"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  // Resource represents a thing in Terraform that has a set of configurable
    14  // attributes and a lifecycle (create, read, update, delete).
    15  //
    16  // The Resource schema is an abstraction that allows provider writers to
    17  // worry only about CRUD operations while off-loading validation, diff
    18  // generation, etc. to this higher level library.
    19  //
    20  // In spite of the name, this struct is not used only for terraform resources,
    21  // but also for data sources. In the case of data sources, the Create,
    22  // Update and Delete functions must not be provided.
    23  type Resource struct {
    24  	// Schema is the schema for the configuration of this resource.
    25  	//
    26  	// The keys of this map are the configuration keys, and the values
    27  	// describe the schema of the configuration value.
    28  	//
    29  	// The schema is used to represent both configurable data as well
    30  	// as data that might be computed in the process of creating this
    31  	// resource.
    32  	Schema map[string]*Schema
    33  
    34  	// SchemaVersion is the version number for this resource's Schema
    35  	// definition. The current SchemaVersion stored in the state for each
    36  	// resource. Provider authors can increment this version number
    37  	// when Schema semantics change. If the State's SchemaVersion is less than
    38  	// the current SchemaVersion, the InstanceState is yielded to the
    39  	// MigrateState callback, where the provider can make whatever changes it
    40  	// needs to update the state to be compatible to the latest version of the
    41  	// Schema.
    42  	//
    43  	// When unset, SchemaVersion defaults to 0, so provider authors can start
    44  	// their Versioning at any integer >= 1
    45  	SchemaVersion int
    46  
    47  	// MigrateState is responsible for updating an InstanceState with an old
    48  	// version to the format expected by the current version of the Schema.
    49  	//
    50  	// It is called during Refresh if the State's stored SchemaVersion is less
    51  	// than the current SchemaVersion of the Resource.
    52  	//
    53  	// The function is yielded the state's stored SchemaVersion and a pointer to
    54  	// the InstanceState that needs updating, as well as the configured
    55  	// provider's configured meta interface{}, in case the migration process
    56  	// needs to make any remote API calls.
    57  	MigrateState StateMigrateFunc
    58  
    59  	// The functions below are the CRUD operations for this resource.
    60  	//
    61  	// The only optional operation is Update. If Update is not implemented,
    62  	// then updates will not be supported for this resource.
    63  	//
    64  	// The ResourceData parameter in the functions below are used to
    65  	// query configuration and changes for the resource as well as to set
    66  	// the ID, computed data, etc.
    67  	//
    68  	// The interface{} parameter is the result of the ConfigureFunc in
    69  	// the provider for this resource. If the provider does not define
    70  	// a ConfigureFunc, this will be nil. This parameter should be used
    71  	// to store API clients, configuration structures, etc.
    72  	//
    73  	// If any errors occur during each of the operation, an error should be
    74  	// returned. If a resource was partially updated, be careful to enable
    75  	// partial state mode for ResourceData and use it accordingly.
    76  	//
    77  	// Exists is a function that is called to check if a resource still
    78  	// exists. If this returns false, then this will affect the diff
    79  	// accordingly. If this function isn't set, it will not be called. It
    80  	// is highly recommended to set it. The *ResourceData passed to Exists
    81  	// should _not_ be modified.
    82  	Create CreateFunc
    83  	Read   ReadFunc
    84  	Update UpdateFunc
    85  	Delete DeleteFunc
    86  	Exists ExistsFunc
    87  
    88  	// CustomizeDiff is a custom function for working with the diff that
    89  	// Terraform has created for this resource - it can be used to customize the
    90  	// diff that has been created, diff values not controlled by configuration,
    91  	// or even veto the diff altogether and abort the plan. It is passed a
    92  	// *ResourceDiff, a structure similar to ResourceData but lacking most write
    93  	// functions like Set, while introducing new functions that work with the
    94  	// diff such as SetNew, SetNewComputed, and ForceNew.
    95  	//
    96  	// The phases Terraform runs this in, and the state available via functions
    97  	// like Get and GetChange, are as follows:
    98  	//
    99  	//  * New resource: One run with no state
   100  	//  * Existing resource: One run with state
   101  	//   * Existing resource, forced new: One run with state (before ForceNew),
   102  	//     then one run without state (as if new resource)
   103  	//  * Tainted resource: No runs (custom diff logic is skipped)
   104  	//  * Destroy: No runs (standard diff logic is skipped on destroy diffs)
   105  	//
   106  	// This function needs to be resilient to support all scenarios.
   107  	//
   108  	// If this function needs to access external API resources, remember to flag
   109  	// the RequiresRefresh attribute mentioned below to ensure that
   110  	// -refresh=false is blocked when running plan or apply, as this means that
   111  	// this resource requires refresh-like behaviour to work effectively.
   112  	//
   113  	// For the most part, only computed fields can be customized by this
   114  	// function.
   115  	//
   116  	// This function is only allowed on regular resources (not data sources).
   117  	CustomizeDiff CustomizeDiffFunc
   118  
   119  	// Importer is the ResourceImporter implementation for this resource.
   120  	// If this is nil, then this resource does not support importing. If
   121  	// this is non-nil, then it supports importing and ResourceImporter
   122  	// must be validated. The validity of ResourceImporter is verified
   123  	// by InternalValidate on Resource.
   124  	Importer *ResourceImporter
   125  
   126  	// If non-empty, this string is emitted as a warning during Validate.
   127  	// This is a private interface for now, for use by DataSourceResourceShim,
   128  	// and not for general use. (But maybe later...)
   129  	deprecationMessage string
   130  
   131  	// Timeouts allow users to specify specific time durations in which an
   132  	// operation should time out, to allow them to extend an action to suit their
   133  	// usage. For example, a user may specify a large Creation timeout for their
   134  	// AWS RDS Instance due to it's size, or restoring from a snapshot.
   135  	// Resource implementors must enable Timeout support by adding the allowed
   136  	// actions (Create, Read, Update, Delete, Default) to the Resource struct, and
   137  	// accessing them in the matching methods.
   138  	Timeouts *ResourceTimeout
   139  }
   140  
   141  // See Resource documentation.
   142  type CreateFunc func(*ResourceData, interface{}) error
   143  
   144  // See Resource documentation.
   145  type ReadFunc func(*ResourceData, interface{}) error
   146  
   147  // See Resource documentation.
   148  type UpdateFunc func(*ResourceData, interface{}) error
   149  
   150  // See Resource documentation.
   151  type DeleteFunc func(*ResourceData, interface{}) error
   152  
   153  // See Resource documentation.
   154  type ExistsFunc func(*ResourceData, interface{}) (bool, error)
   155  
   156  // See Resource documentation.
   157  type StateMigrateFunc func(
   158  	int, *terraform.InstanceState, interface{}) (*terraform.InstanceState, error)
   159  
   160  // See Resource documentation.
   161  type CustomizeDiffFunc func(*ResourceDiff, interface{}) error
   162  
   163  // Apply creates, updates, and/or deletes a resource.
   164  func (r *Resource) Apply(
   165  	s *terraform.InstanceState,
   166  	d *terraform.InstanceDiff,
   167  	meta interface{}) (*terraform.InstanceState, error) {
   168  	data, err := schemaMap(r.Schema).Data(s, d)
   169  	if err != nil {
   170  		return s, err
   171  	}
   172  
   173  	// Instance Diff shoould have the timeout info, need to copy it over to the
   174  	// ResourceData meta
   175  	rt := ResourceTimeout{}
   176  	if _, ok := d.Meta[TimeoutKey]; ok {
   177  		if err := rt.DiffDecode(d); err != nil {
   178  			log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
   179  		}
   180  	} else if s != nil {
   181  		if _, ok := s.Meta[TimeoutKey]; ok {
   182  			if err := rt.StateDecode(s); err != nil {
   183  				log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
   184  			}
   185  		}
   186  	} else {
   187  		log.Printf("[DEBUG] No meta timeoutkey found in Apply()")
   188  	}
   189  	data.timeouts = &rt
   190  
   191  	if s == nil {
   192  		// The Terraform API dictates that this should never happen, but
   193  		// it doesn't hurt to be safe in this case.
   194  		s = new(terraform.InstanceState)
   195  	}
   196  
   197  	if d.Destroy || d.RequiresNew() {
   198  		if s.ID != "" {
   199  			// Destroy the resource since it is created
   200  			if err := r.Delete(data, meta); err != nil {
   201  				return r.recordCurrentSchemaVersion(data.State()), err
   202  			}
   203  
   204  			// Make sure the ID is gone.
   205  			data.SetId("")
   206  		}
   207  
   208  		// If we're only destroying, and not creating, then return
   209  		// now since we're done!
   210  		if !d.RequiresNew() {
   211  			return nil, nil
   212  		}
   213  
   214  		// Reset the data to be stateless since we just destroyed
   215  		data, err = schemaMap(r.Schema).Data(nil, d)
   216  		// data was reset, need to re-apply the parsed timeouts
   217  		data.timeouts = &rt
   218  		if err != nil {
   219  			return nil, err
   220  		}
   221  	}
   222  
   223  	err = nil
   224  	if data.Id() == "" {
   225  		// We're creating, it is a new resource.
   226  		data.MarkNewResource()
   227  		err = r.Create(data, meta)
   228  	} else {
   229  		if r.Update == nil {
   230  			return s, fmt.Errorf("doesn't support update")
   231  		}
   232  
   233  		err = r.Update(data, meta)
   234  	}
   235  
   236  	return r.recordCurrentSchemaVersion(data.State()), err
   237  }
   238  
   239  // Diff returns a diff of this resource.
   240  func (r *Resource) Diff(
   241  	s *terraform.InstanceState,
   242  	c *terraform.ResourceConfig,
   243  	meta interface{}) (*terraform.InstanceDiff, error) {
   244  
   245  	t := &ResourceTimeout{}
   246  	err := t.ConfigDecode(r, c)
   247  
   248  	if err != nil {
   249  		return nil, fmt.Errorf("[ERR] Error decoding timeout: %s", err)
   250  	}
   251  
   252  	instanceDiff, err := schemaMap(r.Schema).Diff(s, c, r.CustomizeDiff, meta)
   253  	if err != nil {
   254  		return instanceDiff, err
   255  	}
   256  
   257  	if instanceDiff != nil {
   258  		if err := t.DiffEncode(instanceDiff); err != nil {
   259  			log.Printf("[ERR] Error encoding timeout to instance diff: %s", err)
   260  		}
   261  	} else {
   262  		log.Printf("[DEBUG] Instance Diff is nil in Diff()")
   263  	}
   264  
   265  	return instanceDiff, err
   266  }
   267  
   268  // Validate validates the resource configuration against the schema.
   269  func (r *Resource) Validate(c *terraform.ResourceConfig) ([]string, []error) {
   270  	warns, errs := schemaMap(r.Schema).Validate(c)
   271  
   272  	if r.deprecationMessage != "" {
   273  		warns = append(warns, r.deprecationMessage)
   274  	}
   275  
   276  	return warns, errs
   277  }
   278  
   279  // ReadDataApply loads the data for a data source, given a diff that
   280  // describes the configuration arguments and desired computed attributes.
   281  func (r *Resource) ReadDataApply(
   282  	d *terraform.InstanceDiff,
   283  	meta interface{},
   284  ) (*terraform.InstanceState, error) {
   285  
   286  	// Data sources are always built completely from scratch
   287  	// on each read, so the source state is always nil.
   288  	data, err := schemaMap(r.Schema).Data(nil, d)
   289  	if err != nil {
   290  		return nil, err
   291  	}
   292  
   293  	err = r.Read(data, meta)
   294  	state := data.State()
   295  	if state != nil && state.ID == "" {
   296  		// Data sources can set an ID if they want, but they aren't
   297  		// required to; we'll provide a placeholder if they don't,
   298  		// to preserve the invariant that all resources have non-empty
   299  		// ids.
   300  		state.ID = "-"
   301  	}
   302  
   303  	return r.recordCurrentSchemaVersion(state), err
   304  }
   305  
   306  // Refresh refreshes the state of the resource.
   307  func (r *Resource) Refresh(
   308  	s *terraform.InstanceState,
   309  	meta interface{}) (*terraform.InstanceState, error) {
   310  	// If the ID is already somehow blank, it doesn't exist
   311  	if s.ID == "" {
   312  		return nil, nil
   313  	}
   314  
   315  	rt := ResourceTimeout{}
   316  	if _, ok := s.Meta[TimeoutKey]; ok {
   317  		if err := rt.StateDecode(s); err != nil {
   318  			log.Printf("[ERR] Error decoding ResourceTimeout: %s", err)
   319  		}
   320  	}
   321  
   322  	if r.Exists != nil {
   323  		// Make a copy of data so that if it is modified it doesn't
   324  		// affect our Read later.
   325  		data, err := schemaMap(r.Schema).Data(s, nil)
   326  		data.timeouts = &rt
   327  
   328  		if err != nil {
   329  			return s, err
   330  		}
   331  
   332  		exists, err := r.Exists(data, meta)
   333  		if err != nil {
   334  			return s, err
   335  		}
   336  		if !exists {
   337  			return nil, nil
   338  		}
   339  	}
   340  
   341  	needsMigration, stateSchemaVersion := r.checkSchemaVersion(s)
   342  	if needsMigration && r.MigrateState != nil {
   343  		s, err := r.MigrateState(stateSchemaVersion, s, meta)
   344  		if err != nil {
   345  			return s, err
   346  		}
   347  	}
   348  
   349  	data, err := schemaMap(r.Schema).Data(s, nil)
   350  	data.timeouts = &rt
   351  	if err != nil {
   352  		return s, err
   353  	}
   354  
   355  	err = r.Read(data, meta)
   356  	state := data.State()
   357  	if state != nil && state.ID == "" {
   358  		state = nil
   359  	}
   360  
   361  	return r.recordCurrentSchemaVersion(state), err
   362  }
   363  
   364  // InternalValidate should be called to validate the structure
   365  // of the resource.
   366  //
   367  // This should be called in a unit test for any resource to verify
   368  // before release that a resource is properly configured for use with
   369  // this library.
   370  //
   371  // Provider.InternalValidate() will automatically call this for all of
   372  // the resources it manages, so you don't need to call this manually if it
   373  // is part of a Provider.
   374  func (r *Resource) InternalValidate(topSchemaMap schemaMap, writable bool) error {
   375  	if r == nil {
   376  		return errors.New("resource is nil")
   377  	}
   378  
   379  	if !writable {
   380  		if r.Create != nil || r.Update != nil || r.Delete != nil {
   381  			return fmt.Errorf("must not implement Create, Update or Delete")
   382  		}
   383  
   384  		// CustomizeDiff cannot be defined for read-only resources
   385  		if r.CustomizeDiff != nil {
   386  			return fmt.Errorf("cannot implement CustomizeDiff")
   387  		}
   388  	}
   389  
   390  	tsm := topSchemaMap
   391  
   392  	if r.isTopLevel() && writable {
   393  		// All non-Computed attributes must be ForceNew if Update is not defined
   394  		if r.Update == nil {
   395  			nonForceNewAttrs := make([]string, 0)
   396  			for k, v := range r.Schema {
   397  				if !v.ForceNew && !v.Computed {
   398  					nonForceNewAttrs = append(nonForceNewAttrs, k)
   399  				}
   400  			}
   401  			if len(nonForceNewAttrs) > 0 {
   402  				return fmt.Errorf(
   403  					"No Update defined, must set ForceNew on: %#v", nonForceNewAttrs)
   404  			}
   405  		} else {
   406  			nonUpdateableAttrs := make([]string, 0)
   407  			for k, v := range r.Schema {
   408  				if v.ForceNew || v.Computed && !v.Optional {
   409  					nonUpdateableAttrs = append(nonUpdateableAttrs, k)
   410  				}
   411  			}
   412  			updateableAttrs := len(r.Schema) - len(nonUpdateableAttrs)
   413  			if updateableAttrs == 0 {
   414  				return fmt.Errorf(
   415  					"All fields are ForceNew or Computed w/out Optional, Update is superfluous")
   416  			}
   417  		}
   418  
   419  		tsm = schemaMap(r.Schema)
   420  
   421  		// Destroy, and Read are required
   422  		if r.Read == nil {
   423  			return fmt.Errorf("Read must be implemented")
   424  		}
   425  		if r.Delete == nil {
   426  			return fmt.Errorf("Delete must be implemented")
   427  		}
   428  
   429  		// If we have an importer, we need to verify the importer.
   430  		if r.Importer != nil {
   431  			if err := r.Importer.InternalValidate(); err != nil {
   432  				return err
   433  			}
   434  		}
   435  
   436  		for k, f := range tsm {
   437  			if isReservedResourceFieldName(k, f) {
   438  				return fmt.Errorf("%s is a reserved field name", k)
   439  			}
   440  		}
   441  	}
   442  
   443  	// Data source
   444  	if r.isTopLevel() && !writable {
   445  		tsm = schemaMap(r.Schema)
   446  		for k, _ := range tsm {
   447  			if isReservedDataSourceFieldName(k) {
   448  				return fmt.Errorf("%s is a reserved field name", k)
   449  			}
   450  		}
   451  	}
   452  
   453  	return schemaMap(r.Schema).InternalValidate(tsm)
   454  }
   455  
   456  func isReservedDataSourceFieldName(name string) bool {
   457  	for _, reservedName := range config.ReservedDataSourceFields {
   458  		if name == reservedName {
   459  			return true
   460  		}
   461  	}
   462  	return false
   463  }
   464  
   465  func isReservedResourceFieldName(name string, s *Schema) bool {
   466  	// Allow phasing out "id"
   467  	// See https://github.com/terraform-providers/terraform-provider-aws/pull/1626#issuecomment-328881415
   468  	if name == "id" && (s.Deprecated != "" || s.Removed != "") {
   469  		return false
   470  	}
   471  
   472  	for _, reservedName := range config.ReservedResourceFields {
   473  		if name == reservedName {
   474  			return true
   475  		}
   476  	}
   477  	return false
   478  }
   479  
   480  // Data returns a ResourceData struct for this Resource. Each return value
   481  // is a separate copy and can be safely modified differently.
   482  //
   483  // The data returned from this function has no actual affect on the Resource
   484  // itself (including the state given to this function).
   485  //
   486  // This function is useful for unit tests and ResourceImporter functions.
   487  func (r *Resource) Data(s *terraform.InstanceState) *ResourceData {
   488  	result, err := schemaMap(r.Schema).Data(s, nil)
   489  	if err != nil {
   490  		// At the time of writing, this isn't possible (Data never returns
   491  		// non-nil errors). We panic to find this in the future if we have to.
   492  		// I don't see a reason for Data to ever return an error.
   493  		panic(err)
   494  	}
   495  
   496  	// Set the schema version to latest by default
   497  	result.meta = map[string]interface{}{
   498  		"schema_version": strconv.Itoa(r.SchemaVersion),
   499  	}
   500  
   501  	return result
   502  }
   503  
   504  // TestResourceData Yields a ResourceData filled with this resource's schema for use in unit testing
   505  //
   506  // TODO: May be able to be removed with the above ResourceData function.
   507  func (r *Resource) TestResourceData() *ResourceData {
   508  	return &ResourceData{
   509  		schema: r.Schema,
   510  	}
   511  }
   512  
   513  // Returns true if the resource is "top level" i.e. not a sub-resource.
   514  func (r *Resource) isTopLevel() bool {
   515  	// TODO: This is a heuristic; replace with a definitive attribute?
   516  	return (r.Create != nil || r.Read != nil)
   517  }
   518  
   519  // Determines if a given InstanceState needs to be migrated by checking the
   520  // stored version number with the current SchemaVersion
   521  func (r *Resource) checkSchemaVersion(is *terraform.InstanceState) (bool, int) {
   522  	// Get the raw interface{} value for the schema version. If it doesn't
   523  	// exist or is nil then set it to zero.
   524  	raw := is.Meta["schema_version"]
   525  	if raw == nil {
   526  		raw = "0"
   527  	}
   528  
   529  	// Try to convert it to a string. If it isn't a string then we pretend
   530  	// that it isn't set at all. It should never not be a string unless it
   531  	// was manually tampered with.
   532  	rawString, ok := raw.(string)
   533  	if !ok {
   534  		rawString = "0"
   535  	}
   536  
   537  	stateSchemaVersion, _ := strconv.Atoi(rawString)
   538  	return stateSchemaVersion < r.SchemaVersion, stateSchemaVersion
   539  }
   540  
   541  func (r *Resource) recordCurrentSchemaVersion(
   542  	state *terraform.InstanceState) *terraform.InstanceState {
   543  	if state != nil && r.SchemaVersion > 0 {
   544  		if state.Meta == nil {
   545  			state.Meta = make(map[string]interface{})
   546  		}
   547  		state.Meta["schema_version"] = strconv.Itoa(r.SchemaVersion)
   548  	}
   549  	return state
   550  }
   551  
   552  // Noop is a convenience implementation of resource function which takes
   553  // no action and returns no error.
   554  func Noop(*ResourceData, interface{}) error {
   555  	return nil
   556  }
   557  
   558  // RemoveFromState is a convenience implementation of a resource function
   559  // which sets the resource ID to empty string (to remove it from state)
   560  // and returns no error.
   561  func RemoveFromState(d *ResourceData, _ interface{}) error {
   562  	d.SetId("")
   563  	return nil
   564  }