Terraform
Migrating attribute types
This page explains how to migrate an attribute from SDKv2 to the plugin framework.
Background
An attribute either contains a primitive type, such as an integer or a string, or a complex type that contains other attributes.
Migrating primitive types
In SDKv2, attribute types are defined by the Type
field on the attribute's
schema.Schema
struct.
In the framework, set your attribute's type by selecting a type that implements
the
schema.Attribute
interface.
The following code block shows implementations of various resource attribute
types defined in schema.Schema
with SDKv2.
SDKv2
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,
/* ... */
},
/* ... */
The following shows the same section of provider code after the migration.
Framework
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 SDKv2, an attribute's
Type
field sets its type. In the framework, attributes inherit a type that implements theschema.Attribute
interface.
Examples
The following examples demonstrate migration of various attribute types to the framework.
String attribute
The following example shows the implementation of the type field of the
example_string_attribute
attribute for the exampleDataSource
data source
with SDKv2.
SDKv2
func exampleDataSource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"example_string_attribute": {
Type: schema.TypeString,
/* ... */
},
/* ... */
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.
Framework
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{
/* ... */
},
/* ... */
List attribute
The following example shows the implementation of the type field of the
example_list_attribute
attribute with SDKv2.
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,
},
/* ... */
},
/* ... */
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.
Framework
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.
Migrating complex types
Refer to Attributes - Available attribute types in the framework documentation for details about implementing complex types in the framework.
Migrating custom types
Refer to Types - Custom types in the framework documentation for details about implementing custom types in the framework.