Skip to content

Latest commit

 

History

History
205 lines (176 loc) · 6.68 KB

File metadata and controls

205 lines (176 loc) · 6.68 KB
page_title description
Attribute Types: Migrating from SDKv2 to the Framework
Migrate attribute type from SDKv2 to the plugin Framework

Attribute Types

-> Note: The Plugin Framework is in beta.

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, and are implemented by populating the NestedAttributes field on the tfsdk.Attribute struct. 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 Type field on your attribute's tfsdk.Attribute struct.

func (r *resourceExample) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
    return tfsdk.Schema{
        /* ... */
        Attributes: map[string]tfsdk.Attribute{
            "bool_example": {
				Type:    types.BoolType,
				/* ... */
			},
			"float64_example": {
				Type:    types.Float64Type,
				/* ... */
			},
			"int64_example": {
				Type:    types.Int64Type,
				/* ... */
			},
			"list_example": {
				Type:    types.ListType{ElemType: types.BoolType},
				/* ... */
			},
			"map_example": {
				Type:    types.MapType{ElemType: types.Float64Type},
				/* ... */
			},
			"set_example": {
				Type:    types.SetType{ElemType: types.Int64Type},
				/* ... */
			},
			"string_example": {
				Type:    types.StringType,
				/* ... */
			},
            /* ... */

Migration Notes

Remember the following differences between SDKv2 and the Framework when completing the migration.

  • In the Framework, an Attribute struct has either the Type or Attributes field defined. The Type field is set to a primitive type such as an integer or string, and you use the Attributes field for NestedAttributes. Refer to Providers for an example of using a single nested attribute. Nested attributes are also described in more detail on the Schemas page in the Framework documentation.

Example

The 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.

SDKv2

The following example from the data_source.go file shows the implementation of the type field of the url attribute for the http data source with SDKv2.

func dataSource() *schema.Resource {
    return &schema.Resource{
        Schema: map[string]*schema.Schema{
            "url": {
                Type:        schema.TypeString,
                /* ... */
            },
            /* ... */

Framework

The following example from the data_source_http.go file shows how the type of the url attribute for the http data source is defined with the Framework after the migration.

func (d *httpDataSource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
    return tfsdk.Schema{
        Attributes: map[string]tfsdk.Attribute{
            "url": {
                Type:        types.StringType,
                /* ... */
            },
            /* ... */

The following examples show how to migrate portions of the tls provider.

For a complete example, clone the terraform-provider-tls repository and compare the common_cert.go file in v3.4.0 and the resource_cert_request.go file after the migration.

SDKv2

The following example from the common_cert.go file shows the implementation of the type field of the dns_names attribute with SDKv2.

func resourceCertRequest() *schema.Resource {
    return &schema.Resource{
        Schema: map[string]*schema.Schema{
            "dns_names": {
                Type:    schema.TypeList,
                Elem:    &schema.Schema{
			        Type: schema.TypeString,
		        },
                /* ... */
            },
            /* ... */

Framework

The following example from the data_source_http.go file shows how the type of the url attribute for the http data source is defined with the Framework after the migration.

func (r *certRequestResource) GetSchema(context.Context) (tfsdk.Schema, diag.Diagnostics) {
    return tfsdk.Schema{
        Attributes: map[string]tfsdk.Attribute{
            "dns_names": {
                Type:    types.ListType{
                    ElemType: types.StringType
                },
                /* ... */
            },
            /* ... */