Blocks
Some providers, resources, and data sources include repeatable nested blocks in their attributes. These nested blocks typically represent separate objects that are related to (or embedded within) the containing object.
This page explains how to migrate nested blocks that are not computed (i.e., do not set
Computed: true
) from SDKv2 to the Framework. Refer to
Blocks with Computed Fields for more details
about migrating nested blocks that contain fields that are computed.
The following table describes the mapping between SDK Schema Fields and the Framework.
SDK Schema Field | Framework | |
---|---|---|
Type | ListNestedBlock, SetNestedBlock | |
ConfigMode | Schema must be explictly defined using Attributes and Blocks | |
Required | listvalidator.IsRequired, setvalidator.IsRequired | |
Optional | N/A - no implementation required | |
Computed | Blocks with Computed Fields | |
ForceNew | RequiresReplace on PlanModifiers field on attribute within block or implementation of ResourceWithModifyPlan interface | |
DiffSuppressFunc | PlanModifiers field on attribute within block or implementation of ResourceWithModifyPlan interface | |
DiffSuppressOnRefresh | Read method on resource | |
Description | Description field on block | |
InputDefault | N/A - no longer valid | |
StateFunc | Requires implementation of bespoke logic before storing state, for instance in resource Create method | |
Elem | NestedObject within block | |
MaxItems | Use listValidator.SizeAtMost or setvalidator.SizeAtMost on Validators field on ListNestedBlock or SetNestedBlock | |
MinItems | Use listValidator.SizeAtLeast or setvalidator.SizeAtLeast on Validators field on ListNestedBlock or SetNestedBlock | |
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 within block | |
ValidateFunc | Predefined Validators and Custom Validators | |
ValidateDiagFunc | Predefined Validators and Custom Validators | |
Sensitive | N/A - only supported on attributes |
Nested Block Example
The following example shows a nested block in Terraform resource configuration. The subject
nested
block within the tls_cert_request
resource configures the subject of a certificate request with the common_name
and
organization
attributes.
SDKv2
In SDKv2, blocks are defined by an attribute whose type is TypeList
or TypeSet
and whose Elem
field is set to a
schema.Resource
that contains a map of the block's attribute names to corresponding schemaSchema
structs.
Framework
In the Framework, you implement nested blocks with the Blocks
field of your provider, resource, or data source's
schema, as returned by the Schema
method. The Blocks
field maps the name of each block to a
schema.Block
definition.
Example
SDKv2
The following example shows the implementation of the example_nested_block
nested block with SDKv2.
Framework
The following example shows how the nested example_nested_block
block
is defined with the Framework after the migration.