Skip to content

Commit

Permalink
feat(provider): choose between constant & exponential backoff for act…
Browse files Browse the repository at this point in the history
…ions

This adds a new configuration parameter called `poll_function` to decide which
type of function is used during the polling.

Allowed values are `constant` and `exponential`. The default value is `exponential`
to avoid changing the current behavior.
  • Loading branch information
beagleknight committed Nov 24, 2023
1 parent 4811531 commit 3b06b8a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
16 changes: 15 additions & 1 deletion hcloud/plugin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
14 changes: 13 additions & 1 deletion hcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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.",
ExactlyOneOf: []string{"constant", "exponential"},
},
},
ResourcesMap: map[string]*schema.Resource{
certificate.UploadedResourceType: certificate.UploadedResource(),
Expand Down Expand Up @@ -139,7 +146,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()))
Expand Down
3 changes: 2 additions & 1 deletion website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 3b06b8a

Please sign in to comment.