Terraform
Migrating attribute fields
This page explains how to migrate attribute fields to the framework. In SDKv2, attribute fields define an attribute's behavior, such as indicating that an attribute is required or optional. These fields are implemented in various ways in the framework.
Background
A subset of attribute fields, such as required, optional, computed, or sensitive, define attribute behavior as boolean flags. Refer to Schemas - Attributes in the framework documentation for details.
Migrating schema fields
The following table describes the mapping between SDK Schema Fields and the framework.
SDK Schema Field | Framework |
---|---|
Type | Attribute Types |
ConfigMode | Schema must be explictly defined using Attributes and Blocks |
Required | Required boolean field on attribute |
Optional | Optional boolean field on attribute |
Computed | Computed boolean field on attribute |
ForceNew | RequiresReplace on PlanModifiers field on attribute or implementation of ResourceWithModifyPlan interface |
DiffSuppressFunc | Custom Types, PlanModifiers field on attribute, or implementation of ResourceWithModifyPlan interface |
DiffSuppressOnRefresh | Custom Types semantic equality logic or manual logic in Read method on resource |
Default | Default field on attribute using one of the predefined Defaults or implementing one of the schema package interfaces |
DefaultFunc | Default field on attribute using one of the predefined Defaults or implementing one of the schema package interfaces |
Description | Description field on attribute |
InputDefault | N/A - no longer valid |
StateFunc | Requires implementation of bespoke logic before storing state, for instance in resource Create method |
Elem | ElementType on ListAttribute, MapAttribute or SetAttribute. Refer to Blocks if schema.Resource is present in Elem . |
MaxItems | Use listValidator.SizeAtMost, mapvalidator.SizeAtMost or setvalidator.SizeAtMost on Validators field on list, map or set attribute |
MinItems | Use listValidator.SizeAtLeast, mapvalidator.SizeAtLeast or setvalidator.SizeAtLeast on Validators field on list, map or set attribute |
Set | N/A - no implementation required |
ComputedWhen | N/A - no longer valid |
ConflictsWith | Predefined Validators |
ExactlyOneOf | Predefined Validators |
AtLeastOneOf | Predefined Validators |
RequiredWith | Predefined Validators |
Deprecated | DeprecationMessage field on attribute |
ValidateFunc | Predefined Validators, Custom Validators, or Custom Types validation logic |
ValidateDiagFunc | Predefined Validators, Custom Validators, or Custom Types validation logic |
Sensitive | Sensitive field on attribute |
Migrating boolean fields
Migrate required, optional, computed, and sensitive attribute fields from SDKv2 to the framework. Refer to the table above to migrate other kinds of fields.
In SDKv2, Required
, Optional
, Computed
, and Sensitive
are boolean fields
on the attribute's schema.
In the framework, you set the same fields on the schema.Attribute
implementation, with the same behavior as the corresponding fields from SDKv2.
The following code block shows implementations of these fields for a resource
attribute using the schema.Schema
struct with SDKv2.
SDKv2
func resourceExample() *schema.Resource {
return &schema.Resource{
/* ... */
Schema: map[string]*schema.Schema{
"attribute_example": {
Required: bool
Optional: bool
Computed: bool
Sensitive: bool
/* ... */
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 = schema.Schema{
/* ... */
Attributes: map[string]schema.Attribute{
"attribute_example": schema.XXXAttribute{
Required: bool
Optional: bool
Computed: bool
Sensitive: bool
/* ... */
Examples
Required fields
The following example shows how the example_attribute
attribute on the
exampleDataSource
data source is set to be required with SDKv2.
SDKv2
func exampleDataSource() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"example_attribute": {
Required: true,
/* ... */
},
/* ... */
The following example shows how the example_attribute
attribute on the
exampleDataSource
data source is set to be required with the framework.
Framework
func (d *exampleDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attribute": schema.StringAttribute{
Required: true,
/* ... */
},
/* ... */