Resources are an abstraction that allow Terraform to manage infrastructure objects by defining create, read, update,
and delete functionality that maps onto API operations. Resource schemas define what fields a resource has, give
Terraform metadata about those fields, and define how the resource behaves. Refer to
Resources in the Framework documentation for details.
This page explains how to migrate a resource's schema from SDKv2 to the plugin Framework. We also recommend reviewing these additional guides for resources throughout the migration:
In SDKv2, resources are defined by the ResourcesMap field in the schema.Provider struct, which maps resource names
(strings) to their schema. Each schema is a schema.Resource struct that includes:
A Schema field, which defines resource attributes
Fields for resource lifecycle functions such as Create and CreateContext
Fields for functions to implement state upgrade (StateUpgraders), import (Importer), and customize diff
(CustomizeDiff)
The following code shows a basic implementation of resource schema with SDKv2.
In the Framework, you define your provider's resources by adding them to your provider's Resources method.
The Resources method on your provider.Provider returns a slice of functions that return types that
implement the resource.Resource interface for each resource your provider supports.
The following code shows how you add a resource to your provider with the Framework.
The resource.Resource interface requires Metadata, GetSchema, Create, Read, Update, and Delete methods.
The GetSchema method returns a tfsdk.Schema struct which defines your resource's attributes. This is the same
struct you use to define provider and data source attributes.
The Metadata method returns a type name that you define.
The following code shows how you define a resource.Resource which implements these methods with the
Framework.
Remember the following differences between SDKv2 and the Framework when completing the migration.
SDKv2 uses schema.Resource structs to define resources. These structs have a Schema field which holds a
schema.Schema to define the resource's attributes and behavior. In the Framework, you define a type that implements
the resource.Resource interface, which includes a GetSchema method that returns your resource's schema.
SDKv2 implements a resource's CRUD operations as functions on the schema.Resource. In the Framework, you define a
type that implements the resource.Resource interface. The resource interface contains the functions that define your resource's
CRUD operations.
The following examples show how to migrate portions of the
tls provider.
For a complete example,
clone the terraform-provider-tls repository and compare the resource_private_key.go file in
v3.4.0 with the file
after the migration.
In SDKv2, the ResourcesMap field on the schema.Provider struct holds a map[string]*schemaResource. A typical
pattern is to implement a function that returns schema.Resource.
The following example from the provider.go file defines a tls_private_key resource within the provider schema.
The following example from the resource_private_key.go file defines the resource schema.
funcresourcePrivateKey()*schema.Resource {return&schema.Resource{
CreateContext: createResourcePrivateKey,
DeleteContext: deleteResourcePrivateKey,
ReadContext: readResourcePrivateKey,
Description:"Creates a PEM /* ... */",
Schema:map[string]*schema.Schema{"algorithm":{
Type: schema.TypeString,
Required:true,
ForceNew:true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(SupportedAlgorithmsStr(),false)),
Description:"Name of the algorithm to use when generating the private key. "+"Currently-supported values are `RSA`, `ECDSA` and `ED25519`.",},/* ... */
funcresourcePrivateKey()*schema.Resource {return&schema.Resource{ CreateContext: createResourcePrivateKey, DeleteContext: deleteResourcePrivateKey, ReadContext: readResourcePrivateKey, Description:"Creates a PEM /* ... */", Schema:map[string]*schema.Schema{"algorithm":{ Type: schema.TypeString, Required:true, ForceNew:true, ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(SupportedAlgorithmsStr(),false)), Description:"Name of the algorithm to use when generating the private key. "+"Currently-supported values are `RSA`, `ECDSA` and `ED25519`.",},/* ... */