Skip to content

Commit

Permalink
Add poll_function
Browse files Browse the repository at this point in the history
This adds a poll_function argument that can be used to configure the function used
during the polling (default: exponential).
  • Loading branch information
beagleknight committed Nov 23, 2023
1 parent efee586 commit b41ae2a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion hcloud/plugin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ 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,
},
},
// 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 +73,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 +126,6 @@ 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 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.ConstantBackoff(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

0 comments on commit b41ae2a

Please sign in to comment.