diff --git a/hcloud/plugin_provider.go b/hcloud/plugin_provider.go index 443a1223d..40b828854 100644 --- a/hcloud/plugin_provider.go +++ b/hcloud/plugin_provider.go @@ -7,11 +7,13 @@ import ( "time" "github.com/hashicorp/go-hclog" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/hashicorp/terraform-plugin-framework/provider/schema" "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" @@ -56,6 +58,13 @@ func (p *PluginProvider) Schema(_ context.Context, _ provider.SchemaRequest, res Description: "The interval at which actions are polled by the client. Default `500ms`. Increase this interval if you run into rate limiting errors.", Optional: true, }, + "poll_function": schema.StringAttribute{ + Description: "The type of function to be used during the polling.", + Optional: true, + Validators: []validator.String{ + stringvalidator.OneOf([]string{"constant", "exponential"}...), + }, + }, }, // TODO: Uncomment once we get rid of the SDK v2 Provider // MarkdownDescription: `The Hetzner Cloud (hcloud) provider is used to interact with the resources supported by @@ -69,6 +78,7 @@ type PluginProviderModel struct { Token types.String `tfsdk:"token"` Endpoint types.String `tfsdk:"endpoint"` PollInterval types.String `tfsdk:"poll_interval"` + PollFunction types.String `tfsdk:"poll_function"` } // Configure is called at the beginning of the provider lifecycle, when @@ -121,7 +131,11 @@ func (p *PluginProvider) Configure(ctx context.Context, req provider.ConfigureRe fmt.Sprintf("An unexpected error was encountered trying to parse the value.\n\n%s", err.Error()), ) } - opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ExponentialBackoff(2, pollInterval))) + if data.PollFunction.ValueString() == "constant" { + opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ConstantBackoff(pollInterval))) + } else { + opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ExponentialBackoff(2, pollInterval))) + } } if resp.Diagnostics.HasError() { diff --git a/hcloud/provider.go b/hcloud/provider.go index e84a4e4cf..20d7585cb 100644 --- a/hcloud/provider.go +++ b/hcloud/provider.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hetznercloud/hcloud-go/hcloud" @@ -68,6 +69,13 @@ func Provider() *schema.Provider { Default: "500ms", Description: "The interval at which actions are polled by the client. Default `500ms`. Increase this interval if you run into rate limiting errors.", }, + "poll_function": { + Type: schema.TypeString, + Optional: true, + Default: "exponential", + Description: "The type of function to be used during the polling.", + ValidateFunc: validation.StringInSlice([]string{"constant", "exponential"}, false), + }, }, ResourcesMap: map[string]*schema.Resource{ certificate.UploadedResourceType: certificate.UploadedResource(), @@ -139,7 +147,12 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{}, if err != nil { return nil, hcclient.ErrorToDiag(err) } - opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ExponentialBackoff(2, pollInterval))) + pollFunction, ok := d.GetOk("poll_function") + if ok && pollFunction == "constant" { + opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ConstantBackoff(pollInterval))) + } else { + opts = append(opts, hcloud.WithPollBackoffFunc(hcloud.ExponentialBackoff(2, pollInterval))) + } } if logging.LogLevel() != "" { opts = append(opts, hcloud.WithDebugWriter(log.Writer())) diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index 0b7c949ac..60f6b1e20 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -38,7 +38,8 @@ The following arguments are supported: - `token` - (Required, string) This is the Hetzner Cloud API Token, can also be specified with the `HCLOUD_TOKEN` environment variable. - `endpoint` - (Optional, string) Hetzner Cloud API endpoint, can be used to override the default API Endpoint `https://api.hetzner.cloud/v1`. -- `poll_interval` - (Optional, string) Configures the interval in which actions are polled by the client. Default `500ms`. Increase this interval if you run into rate limiting errors. +- `poll_interval` - (Optional, string) Configures the interval in which actions are polled by the client. Default `500ms`. Increase this interval if you run into rate limiting errors. +- `poll_function` - (Optional, string) Configures the type of function to be used during the polling. Valid values are `constant` and `exponential`. Default `exponential`. ## Delete Protection