A new platform for documentation and tutorials is launching soon.
We are migrating Terraform documentation into HashiCorp Developer, our new developer experience.
Search Terraform documentation » Attribute FieldsA 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.
This page explains how to migrate the required, optional, computed, and sensitive attribute fields from SDKv2 to the
Framework.
» SDKv2In SDKv2, Required
, Optional
, Computed
, and Sensitive
are boolean fields on the attribute's schema.
func resourceExample ( ) * schema. Resource {
return & schema. Resource{
Schema: map [ string ] * schema. Schema{
"attribute_example" : {
Required: bool
Optional: bool
Computed: bool
Sensitive: bool
func resourceExample ( ) * schema. Resource {
return & schema. Resource{
Schema: map [ string ] * schema. Schema{
"attribute_example" : {
Required: bool
Optional: bool
Computed: bool
Sensitive: bool
» FrameworkIn the Framework, you set the same fields on the tfsdk.Attribute
struct, with the same behavior.
func ( d * resourceTypeExample) GetSchema ( context. Context) ( tfsdk. Schema, diag. Diagnostics) {
return tfsdk. Schema{
Attributes: map [ string ] tfsdk. Attribute{
"attribute_example" : {
Required: bool
Optional: bool
Computed: bool
Sensitive: bool
func ( d * resourceTypeExample) GetSchema ( context. Context) ( tfsdk. Schema, diag. Diagnostics) {
return tfsdk. Schema{
Attributes: map [ string ] tfsdk. Attribute{
"attribute_example" : {
Required: bool
Optional: bool
Computed: bool
Sensitive: bool
» ExampleThe 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
and the data_source_http.go
file
after the migration .
» SDKv2The following example from the data_source.go
file shows how the url
attribute on the http
data source is set to
be required with SDKv2.
func dataSource ( ) * schema. Resource {
return & schema. Resource{
Schema: map [ string ] * schema. Schema{
"url" : {
Required: true ,
} ,
func dataSource ( ) * schema. Resource {
return & schema. Resource{
Schema: map [ string ] * schema. Schema{
"url" : {
Required: true ,
} ,
» FrameworkThe following example from the data_source_http.go
file shows how the url
attribute on the http
data source is set
to be required with the Framework.
func ( d * httpDataSource) GetSchema ( context. Context) ( tfsdk. Schema, diag. Diagnostics) {
return tfsdk. Schema{
Attributes: map [ string ] tfsdk. Attribute{
"url" : {
Required: true ,
} ,
func ( d * httpDataSource) GetSchema ( context. Context) ( tfsdk. Schema, diag. Diagnostics) {
return tfsdk. Schema{
Attributes: map [ string ] tfsdk. Attribute{
"url" : {
Required: true ,
} ,