Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(provider): choose between constant & exponential backoff for actions #798

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"},
beagleknight marked this conversation as resolved.
Show resolved Hide resolved
},
},
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