Attribute Types
An attribute either contains a primitive type, such as an integer or a string, or contains other attributes. Attributes that contain other attributes are referred to as nested attributes. Refer to Schemas - Attributes in the Framework documentation for details.
This page explains how to migrate a primitive attribute from SDKv2 to the plugin Framework. For an example of migrating a nested block to a nested attribute, refer to Providers in this guide.
SDKv2
In SDKv2, attribute types are defined by the Type
field on the attribute's schema.Schema
struct.
func resourceExample() *schema.Resource { return &schema.Resource{ /* ... */ Schema: map[string]*schema.Schema{ "bool_example": { Type: schema.TypeBool, /* ... */ }, "float64_example": { Type: schema.TypeFloat, /* ... */ }, "int64_example": { Type: schema.TypeInt, /* ... */ }, "list_example": { Type: schema.TypeList, Elem: &schema.Schema{ Type: schema.TypeBool, }, /* ... */ }, "map_example": { Type: schema.TypeMap, Elem: &schema.Schema{ Type: schema.TypeFloat, }, /* ... */ }, "set_example": { Type: schema.TypeSet, Elem: &schema.Schema{ Type: schema.TypeInt, }, /* ... */ }, "string_example": { Type: schema.TypeString, /* ... */ }, /* ... */
Framework
In the Framework, you set your attribute's type with the attribute's schema.Attribute
implementation.
func (r *ThingResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schea.Schema{ /* ... */ Attributes: map[string]schema.Attribute{ "bool_example": schema.BoolAttribute{ /* ... */ }, "float64_example": schema.Float64Attribute{ /* ... */ }, "int64_example": schema.Int64Attribute{ /* ... */ }, "list_example": schema.ListAttribute{ ElementType: types.BoolType, /* ... */ }, "map_example": schema.MapAttribute{ ElementType: types.Float64Type, /* ... */ }, "set_example": schema.SetAttribute{ ElementType: types.Int64Type, /* ... */ }, "string_example": schema.StringAttribute{ /* ... */ }, /* ... */
Migration Notes
Remember the following differences between SDKv2 and the Framework when completing the migration.
- In the Framework, the
schema.Attribute
implementation determines the required details.
Examples
SDKv2
The following example shows the implementation of the type field of the example_string_attribute
attribute
for the exampleDataSource
data source with SDKv2.
func exampleDataSource() *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ "example_string_attribute": { Type: schema.TypeString, /* ... */ }, /* ... */
Framework
The following example shows how the type of the example_string_attribute
attribute for the exampleDataSource
data
source is defined with the Framework after the migration.
func (d *exampleDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { resp.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ "example_string_attribute": schema.StringAttribute{ /* ... */ }, /* ... */
SDKv2
The following example shows the implementation of the type field of the example_list_attribute
attribute with SDKv2.
func exampleResource() *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ "example_list_attribute": { Type: schema.TypeList, Elem: &schema.Schema{ Type: schema.TypeString, }, /* ... */ }, /* ... */
Framework
The following example shows how the type of the example_list_attribute
attribute for the exampleResource
resource
is defined with the Framework after the migration.
func (r *exampleResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) { resp.Schema = schema.Schema{ Attributes: map[string]schema.Attribute{ "example_list_attribute": schema.ListAttribute{ ElementType: types.StringType, /* ... */ }, /* ... */
Refer to Terraform Concepts - Attributes for further examples of different types of schema attributes in the Framework.