Attribute Schema
Attributes define how users can configure values for your Terraform provider, resources, and data sources. Refer to Schemas - Attributes in the Framework documentation for details.
This page explains how to migrate an attribute from SDKv2 to the plugin Framework.
SDKv2
In SDKv2, attributes are defined by the Schema
field in the provider, resource, or data source schema. The Schema
field maps each attribute name (string) to the attribute's schema.Schema
struct. Both resources and data sources are
defined using the schema.Resource
struct.
The following code shows a basic implementation of attribute schema for a provider in SDKv2.
func ProviderExample() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
/* ... */
},
In SDKv2, resource and data source attributes are defined the same way on their respective types.
func resourceExample() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
/* ... */
Framework
In the Framework, you define attributes by setting the Attributes
field on your provider, resource, or data type's
schema, as returned by the GetSchema
function. The tfsdk.Schema
type returned by GetSchema
includes an
Attributes
field that maps each attribute name (string) to the attribute's tfsdk.Attribute
struct.
The following code shows how to define an attribute for a resource with the Framework.
func (d *resourceTypeExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"example": {
/* ... */
The Framework defines the tfsdk.Attribute
struct as follows.
type Attribute struct {
Type attr.Type
Attributes NestedAttributes
Description string
MarkdownDescription string
Required bool
Optional bool
Computed bool
Sensitive bool
DeprecationMessage string
Validators []AttributeValidator
PlanModifiers AttributePlanModifiers
}
Migration Notes
Remember the following differences between SDKv2 and the Framework when completing the migration.
- In SDKv2, attributes are defined by a map from attribute names to
schema.Schema
structs in theSchema
field of your resource's schema. In the Framework, attributes are defined by a map from attribute names totfsdk.Attribute
structs in your resource's schema, which is returned by theGetSchema
function. - There are several differences between the implementation of attributes in SDKv2 and the Framework. Refer to the other pages in the Attributes & Blocks section of this migration guide for more details.
Example
The following examples show how to migrate portions of the http provider.
For a complete example, clone the
terraform-provider-http
repository and compare the data_source.go
file in
v2.2.0
with the data_source_http.go
file in
v3.0.1.
SDKv2
The following example from the data_source.go
file shows the implementation of the url
attribute for the http
data source.
func dataSource() *schema.Resource {
return &schema.Resource{
/* ... */
Schema: map[string]*schema.Schema{
"url": {
Description: "The URL for the request. Supported schemes are `http` and `https`.",
Type: schema.TypeString,
Required: true,
},
Framework
The following shows the same section of provider code after the migration.
This code implements the url
attribute for the http
data source with the Framework.
func (d *httpDataSourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
/* ... */
Attributes: map[string]tfsdk.Attribute{
"url": {
Description: "The URL for the request. Supported schemes are `http` and `https`.",
Type: types.StringType,
Required: true,
},
/* ... */