A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.hashicorp.com/terraform/plugin/framework/data-sources/timeouts below:

Timeouts | Terraform | HashiCorp Developer

The reality of cloud infrastructure is that it typically takes time to perform operations such as booting operating systems, discovering services, and replicating state across network edges. As the provider developer you should take known delays in data source APIs into account in the Read function of the data source. Terraform supports configurable timeouts to assist in these situations.

The Framework can be used in conjunction with the terraform-plugin-framework-timeouts module in order to allow defining timeouts in configuration and have them be available in the Read function.

Timeouts can be defined using either nested blocks or nested attributes.

If you are writing a new provider using terraform-plugin-framework then we recommend using nested attributes.

If you are migrating a provider from SDKv2 to the Framework and you are already using timeouts you can either continue to use block syntax, or switch to using nested attributes. However, switching to using nested attributes will require that practitioners that are using your provider update their Terraform configuration.

Block

If your configuration is using a nested block to define timeouts, such as the following:

resource "timeouts_example" "example" {
  /* ... */

  timeouts {
    read = "60m"
  }
}

Import the timeouts module.

import (
    /* ... */
    "github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
)

You can use this module to mutate the schema.Schema as follows:

func (d *ThingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
    resp.Schema = schema.Schema{
        /* ... */

        Blocks: map[string]schema.Block{
            "timeouts": timeouts.Block(ctx),
        },
Attribute

If your configuration is using nested attributes to define timeouts, such as the following:

resource "timeouts_example" "example" {
  /* ... */

  timeouts = {
    read = "60m"
  }
}

You can use this module to mutate the schema.Schema as follows:

func (d *ThingDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
    resp.Schema = schema.Schema{
        Attributes: map[string]schema.Attribute{
            /* ... */
            "timeouts": timeouts.Attributes(ctx),
        },

Given a Read method which fetches the entire configuration:

func (e *exampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
    var data exampleDataSourceData

    diags := req.Config.Get(ctx, &data)
    resp.Diagnostics.Append(diags...)

Modify the exampleDataSourceData model to include a field for timeouts using a timeouts.Value type.

type exampleDataSourceData struct {
    /* ... */
    Timeouts    timeouts.Value `tfsdk:"timeouts"`

Call the timeouts.Read() function.

func (e *exampleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
    var data exampleDataSourceData

    diags := req.Config.Get(ctx, &data)
    resp.Diagnostics.Append(diags...)
    if resp.Diagnostics.HasError() {
        return
    }

    readTimeout, diags := data.Timeouts.Read(ctx, 20*time.Minute)

    resp.Diagnostics.Append(diags...)

    if resp.Diagnostics.HasError() {
        return
    }

    ctx, cancel := context.WithTimeout(ctx, readTimeout)
    defer cancel()

    /* ... */
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4