From a1a516f9adbbe5b77ff39904628296a6b1e6ee8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Tue, 12 Mar 2024 10:51:08 +0100 Subject: [PATCH] chore: migrate transport to its own package (#2446) * chore: migrate transport to itw own package * Fix linter --- .../transport/retry.go | 38 +++++++--- internal/transport/retry_aws.go | 63 ++++++++++++++++ scaleway/helpers.go | 71 ------------------- scaleway/helpers_apple_silicon.go | 5 +- scaleway/helpers_baremetal.go | 17 ++--- scaleway/helpers_block.go | 9 +-- scaleway/helpers_cockpit.go | 5 +- scaleway/helpers_container.go | 17 ++--- scaleway/helpers_container_triggers.go | 5 +- scaleway/helpers_document_db.go | 5 +- scaleway/helpers_domain.go | 9 +-- scaleway/helpers_flexible_ip.go | 5 +- scaleway/helpers_function.go | 21 +++--- scaleway/helpers_instance.go | 31 ++++---- scaleway/helpers_iot.go | 5 +- scaleway/helpers_k8s.go | 17 ++--- scaleway/helpers_lb.go | 17 ++--- scaleway/helpers_rdb.go | 13 ++-- scaleway/helpers_redis.go | 5 +- scaleway/helpers_registry.go | 9 +-- scaleway/helpers_sdb_sql.go | 5 +- scaleway/helpers_tem.go | 5 +- scaleway/helpers_vpcgw.go | 13 ++-- scaleway/helpers_webhosting.go | 5 +- scaleway/provider.go | 3 +- scaleway/provider_test.go | 7 +- scaleway/resource_document_db_read_replica.go | 5 +- scaleway/resource_instance_image.go | 3 +- scaleway/resource_instance_snapshot.go | 3 +- scaleway/resource_instance_volume.go | 5 +- scaleway/resource_lb_ip_test.go | 5 +- scaleway/resource_lb_test.go | 5 +- scaleway/resource_mnq_sqs_queue.go | 9 +-- scaleway/resource_object_bucket_test.go | 3 +- scaleway/resource_vpc_gateway_network.go | 3 +- 35 files changed, 244 insertions(+), 202 deletions(-) rename scaleway/retryable_transport.go => internal/transport/retry.go (64%) create mode 100644 internal/transport/retry_aws.go diff --git a/scaleway/retryable_transport.go b/internal/transport/retry.go similarity index 64% rename from scaleway/retryable_transport.go rename to internal/transport/retry.go index f212eae5e..128f2d91b 100644 --- a/scaleway/retryable_transport.go +++ b/internal/transport/retry.go @@ -1,24 +1,29 @@ -package scaleway +package transport import ( "bytes" "context" + "errors" "io" "io/ioutil" "net/http" "time" "github.com/hashicorp/go-retryablehttp" + "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" ) -type retryableTransportOptions struct { +// DefaultWaitRetryInterval is used to set the retry interval to 0 during acceptance tests +var DefaultWaitRetryInterval *time.Duration + +type RetryableTransportOptions struct { RetryMax *int RetryWaitMax *time.Duration RetryWaitMin *time.Duration } -func newRetryableTransportWithOptions(defaultTransport http.RoundTripper, options retryableTransportOptions) http.RoundTripper { +func NewRetryableTransportWithOptions(defaultTransport http.RoundTripper, options RetryableTransportOptions) http.RoundTripper { c := retryablehttp.NewClient() c.HTTPClient = &http.Client{Transport: defaultTransport} @@ -55,22 +60,22 @@ func newRetryableTransportWithOptions(defaultTransport http.RoundTripper, option c.RetryWaitMin = *options.RetryWaitMin } - return &retryableTransport{c} + return &RetryableTransport{c} } +// NewRetryableTransport creates a http transport with retry capability. // TODO Retry logic should be moved in the SDK -// newRetryableTransport creates a http transport with retry capability. -func newRetryableTransport(defaultTransport http.RoundTripper) http.RoundTripper { - return newRetryableTransportWithOptions(defaultTransport, retryableTransportOptions{}) +func NewRetryableTransport(defaultTransport http.RoundTripper) http.RoundTripper { + return NewRetryableTransportWithOptions(defaultTransport, RetryableTransportOptions{}) } -// client is a bridge between scw.httpClient interface and retryablehttp.Client -type retryableTransport struct { +// RetryableTransport client is a bridge between scw.httpClient interface and retryablehttp.Client +type RetryableTransport struct { *retryablehttp.Client } // RoundTrip wraps calling an HTTP method with retries. -func (c *retryableTransport) RoundTrip(r *http.Request) (*http.Response, error) { +func (c *RetryableTransport) RoundTrip(r *http.Request) (*http.Response, error) { var body io.ReadSeeker if r.Body != nil { bs, err := ioutil.ReadAll(r.Body) @@ -95,3 +100,16 @@ func (c *retryableTransport) RoundTrip(r *http.Request) (*http.Response, error) } return c.Client.Do(req) } + +func RetryOnTransientStateError[T any, U any](action func() (T, error), waiter func() (U, error)) (T, error) { //nolint:ireturn + t, err := action() + var transientStateError *scw.TransientStateError + if errors.As(err, &transientStateError) { + _, err := waiter() + if err != nil { + return t, err + } + return RetryOnTransientStateError(action, waiter) + } + return t, err +} diff --git a/internal/transport/retry_aws.go b/internal/transport/retry_aws.go new file mode 100644 index 000000000..806ad3818 --- /dev/null +++ b/internal/transport/retry_aws.go @@ -0,0 +1,63 @@ +package transport + +import ( + "context" + "errors" + "time" + + "github.com/hashicorp/aws-sdk-go-base/tfawserr" +) + +// RetryWhenAWSErrCodeEquals retries a function when it returns a specific AWS error +func RetryWhenAWSErrCodeEquals[T any](ctx context.Context, codes []string, config *RetryWhenConfig[T]) (T, error) { //nolint: ireturn + return retryWhen(ctx, config, func(err error) bool { + return tfawserr.ErrCodeEquals(err, codes...) + }) +} + +// RetryWhenAWSErrCodeNotEquals retries a function until it returns a specific AWS error +func RetryWhenAWSErrCodeNotEquals[T any](ctx context.Context, codes []string, config *RetryWhenConfig[T]) (T, error) { //nolint: ireturn + return retryWhen(ctx, config, func(err error) bool { + if err == nil { + return true + } + + return !tfawserr.ErrCodeEquals(err, codes...) + }) +} + +// retryWhen executes the function passed in the configuration object until the timeout is reached or the context is cancelled. +// It will retry if the shouldRetry function returns true. It will stop if the shouldRetry function returns false. +func retryWhen[T any](ctx context.Context, config *RetryWhenConfig[T], shouldRetry func(error) bool) (T, error) { //nolint: ireturn + retryInterval := config.Interval + if DefaultWaitRetryInterval != nil { + retryInterval = *DefaultWaitRetryInterval + } + + timer := time.NewTimer(config.Timeout) + + for { + result, err := config.Function() + if shouldRetry(err) { + select { + case <-timer.C: + return result, ErrRetryWhenTimeout + case <-ctx.Done(): + return result, ctx.Err() + default: + time.Sleep(retryInterval) // lintignore:R018 + continue + } + } + + return result, err + } +} + +type RetryWhenConfig[T any] struct { + Timeout time.Duration + Interval time.Duration + Function func() (T, error) +} + +var ErrRetryWhenTimeout = errors.New("timeout reached") diff --git a/scaleway/helpers.go b/scaleway/helpers.go index 9ba27bda6..62936b9c6 100644 --- a/scaleway/helpers.go +++ b/scaleway/helpers.go @@ -14,7 +14,6 @@ import ( "testing" "time" - "github.com/hashicorp/aws-sdk-go-base/tfawserr" "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -32,9 +31,6 @@ const ( EndpointsID = ServiceName // ID to look up a service endpoint with. ) -// DefaultWaitRetryInterval is used to set the retry interval to 0 during acceptance tests -var DefaultWaitRetryInterval *time.Duration - // RegionalID represents an ID that is linked with a region, eg fr-par/11111111-1111-1111-1111-111111111111 type RegionalID struct { ID string @@ -945,19 +941,6 @@ func validateMapKeyLowerCase() schema.SchemaValidateDiagFunc { } } -func retryOnTransientStateError[T any, U any](action func() (T, error), waiter func() (U, error)) (T, error) { //nolint:ireturn - t, err := action() - var transientStateError *scw.TransientStateError - if errors.As(err, &transientStateError) { - _, err := waiter() - if err != nil { - return t, err - } - return retryOnTransientStateError(action, waiter) - } - return t, err -} - // compareLocalities compare two localities // They are equal if they are the same or if one is a zone contained in a region func compareLocalities(loc1, loc2 string) bool { @@ -1104,60 +1087,6 @@ func NotFound(err error) bool { return errors.As(err, &e) } -type RetryWhenConfig[T any] struct { - Timeout time.Duration - Interval time.Duration - Function func() (T, error) -} - -var ErrRetryWhenTimeout = errors.New("timeout reached") - -// retryWhen executes the function passed in the configuration object until the timeout is reached or the context is cancelled. -// It will retry if the shouldRetry function returns true. It will stop if the shouldRetry function returns false. -func retryWhen[T any](ctx context.Context, config *RetryWhenConfig[T], shouldRetry func(error) bool) (T, error) { //nolint: ireturn - retryInterval := config.Interval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval - } - - timer := time.NewTimer(config.Timeout) - - for { - result, err := config.Function() - if shouldRetry(err) { - select { - case <-timer.C: - return result, ErrRetryWhenTimeout - case <-ctx.Done(): - return result, ctx.Err() - default: - time.Sleep(retryInterval) // lintignore:R018 - continue - } - } - - return result, err - } -} - -// retryWhenAWSErrCodeEquals retries a function when it returns a specific AWS error -func retryWhenAWSErrCodeEquals[T any](ctx context.Context, codes []string, config *RetryWhenConfig[T]) (T, error) { //nolint: ireturn - return retryWhen(ctx, config, func(err error) bool { - return tfawserr.ErrCodeEquals(err, codes...) - }) -} - -// retryWhenAWSErrCodeNotEquals retries a function until it returns a specific AWS error -func retryWhenAWSErrCodeNotEquals[T any](ctx context.Context, codes []string, config *RetryWhenConfig[T]) (T, error) { //nolint: ireturn - return retryWhen(ctx, config, func(err error) bool { - if err == nil { - return true - } - - return !tfawserr.ErrCodeEquals(err, codes...) - }) -} - func sliceContainsString(slice []string, str string) bool { for _, v := range slice { if v == str { diff --git a/scaleway/helpers_apple_silicon.go b/scaleway/helpers_apple_silicon.go index a33e433d8..11abc31a4 100644 --- a/scaleway/helpers_apple_silicon.go +++ b/scaleway/helpers_apple_silicon.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" applesilicon "github.com/scaleway/scaleway-sdk-go/api/applesilicon/v1alpha1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -40,8 +41,8 @@ func asAPIWithZoneAndID(m interface{}, id string) (*applesilicon.API, scw.Zone, func waitForAppleSiliconServer(ctx context.Context, api *applesilicon.API, zone scw.Zone, serverID string, timeout time.Duration) (*applesilicon.Server, error) { retryInterval := defaultAppleSiliconServerRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } server, err := api.WaitForServer(&applesilicon.WaitForServerRequest{ diff --git a/scaleway/helpers_baremetal.go b/scaleway/helpers_baremetal.go index 1088414b2..ab35607f9 100644 --- a/scaleway/helpers_baremetal.go +++ b/scaleway/helpers_baremetal.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -260,8 +261,8 @@ func detachAllPrivateNetworkFromBaremetal(ctx context.Context, d *schema.Resourc func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zone, serverID string, timeout time.Duration) (*baremetal.Server, error) { retryInterval := baremetalRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } server, err := api.WaitForServer(&baremetal.WaitForServerRequest{ @@ -276,8 +277,8 @@ func waitForBaremetalServer(ctx context.Context, api *baremetal.API, zone scw.Zo func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone scw.Zone, serverID string, timeout time.Duration) (*baremetal.Server, error) { retryInterval := baremetalRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } server, err := api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{ @@ -292,8 +293,8 @@ func waitForBaremetalServerInstall(ctx context.Context, api *baremetal.API, zone func waitForBaremetalServerOptions(ctx context.Context, api *baremetal.API, zone scw.Zone, serverID string, timeout time.Duration) (*baremetal.Server, error) { retryInterval := baremetalRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } server, err := api.WaitForServerOptions(&baremetal.WaitForServerOptionsRequest{ @@ -308,8 +309,8 @@ func waitForBaremetalServerOptions(ctx context.Context, api *baremetal.API, zone func waitForBaremetalServerPrivateNetwork(ctx context.Context, api *baremetal.PrivateNetworkAPI, zone scw.Zone, serverID string, timeout time.Duration) ([]*baremetal.ServerPrivateNetwork, error) { retryInterval := baremetalRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } serverPrivateNetwork, err := api.WaitForServerPrivateNetworks(&baremetal.WaitForServerPrivateNetworksRequest{ Zone: zone, diff --git a/scaleway/helpers_block.go b/scaleway/helpers_block.go index 37e2a1283..1218cdf31 100644 --- a/scaleway/helpers_block.go +++ b/scaleway/helpers_block.go @@ -9,6 +9,7 @@ import ( block "github.com/scaleway/scaleway-sdk-go/api/block/v1alpha1" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -44,8 +45,8 @@ func blockAPIWithZoneAndID(m interface{}, zonedID string) (*block.API, scw.Zone, func waitForBlockVolume(ctx context.Context, blockAPI *block.API, zone scw.Zone, id string, timeout time.Duration) (*block.Volume, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } volume, err := blockAPI.WaitForVolumeAndReferences(&block.WaitForVolumeAndReferencesRequest{ @@ -71,8 +72,8 @@ func customDiffCannotShrink(key string) schema.CustomizeDiffFunc { func waitForBlockSnapshot(ctx context.Context, blockAPI *block.API, zone scw.Zone, id string, timeout time.Duration) (*block.Snapshot, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } snapshot, err := blockAPI.WaitForSnapshot(&block.WaitForSnapshotRequest{ diff --git a/scaleway/helpers_cockpit.go b/scaleway/helpers_cockpit.go index 090b69838..f7192e210 100644 --- a/scaleway/helpers_cockpit.go +++ b/scaleway/helpers_cockpit.go @@ -9,6 +9,7 @@ import ( cockpit "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -117,8 +118,8 @@ func flattenCockpitTokenScopes(scopes *cockpit.TokenScopes) []map[string]interfa func waitForCockpit(ctx context.Context, api *cockpit.API, projectID string, timeout time.Duration) (*cockpit.Cockpit, error) { retryInterval := defaultContainerRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForCockpit(&cockpit.WaitForCockpitRequest{ diff --git a/scaleway/helpers_container.go b/scaleway/helpers_container.go index f7cf0a3d9..050b1d939 100644 --- a/scaleway/helpers_container.go +++ b/scaleway/helpers_container.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -113,8 +114,8 @@ func setCreateContainerRequest(d *schema.ResourceData, region scw.Region) (*cont func waitForContainerNamespace(ctx context.Context, containerAPI *container.API, region scw.Region, namespaceID string, timeout time.Duration) (*container.Namespace, error) { retryInterval := defaultContainerRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } ns, err := containerAPI.WaitForNamespace(&container.WaitForNamespaceRequest{ @@ -129,8 +130,8 @@ func waitForContainerNamespace(ctx context.Context, containerAPI *container.API, func waitForContainerCron(ctx context.Context, api *container.API, cronID string, region scw.Region, timeout time.Duration) (*container.Cron, error) { retryInterval := defaultContainerRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } request := container.WaitForCronRequest{ @@ -145,8 +146,8 @@ func waitForContainerCron(ctx context.Context, api *container.API, cronID string func waitForContainer(ctx context.Context, api *container.API, containerID string, region scw.Region, timeout time.Duration) (*container.Container, error) { retryInterval := defaultContainerRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } request := container.WaitForContainerRequest{ @@ -161,8 +162,8 @@ func waitForContainer(ctx context.Context, api *container.API, containerID strin func waitForContainerDomain(ctx context.Context, api *container.API, domainID string, region scw.Region, timeout time.Duration) (*container.Domain, error) { retryInterval := defaultContainerRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } request := container.WaitForDomainRequest{ diff --git a/scaleway/helpers_container_triggers.go b/scaleway/helpers_container_triggers.go index 643a5a591..f1b32c5b4 100644 --- a/scaleway/helpers_container_triggers.go +++ b/scaleway/helpers_container_triggers.go @@ -7,12 +7,13 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func waitForContainerTrigger(ctx context.Context, containerAPI *container.API, region scw.Region, id string, timeout time.Duration) (*container.Trigger, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } trigger, err := containerAPI.WaitForTrigger(&container.WaitForTriggerRequest{ diff --git a/scaleway/helpers_document_db.go b/scaleway/helpers_document_db.go index 6381b6446..41cb88a76 100644 --- a/scaleway/helpers_document_db.go +++ b/scaleway/helpers_document_db.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -45,8 +46,8 @@ func documentDBAPIWithRegionAndID(m interface{}, regionalID string) (*documentdb func waitForDocumentDBInstance(ctx context.Context, api *documentdb.API, region scw.Region, id string, timeout time.Duration) (*documentdb.Instance, error) { retryInterval := defaultWaitDocumentDBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } instance, err := api.WaitForInstance(&documentdb.WaitForInstanceRequest{ diff --git a/scaleway/helpers_domain.go b/scaleway/helpers_domain.go index 3a28b2179..5e07488e3 100644 --- a/scaleway/helpers_domain.go +++ b/scaleway/helpers_domain.go @@ -10,6 +10,7 @@ import ( domain "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -264,8 +265,8 @@ func expandDomainView(i interface{}, ok bool) *domain.RecordViewConfig { func waitForDNSZone(ctx context.Context, domainAPI *domain.API, dnsZone string, timeout time.Duration) (*domain.DNSZone, error) { retryInterval := defaultDomainZoneRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return domainAPI.WaitForDNSZone(&domain.WaitForDNSZoneRequest{ @@ -277,8 +278,8 @@ func waitForDNSZone(ctx context.Context, domainAPI *domain.API, dnsZone string, func waitForDNSRecordExist(ctx context.Context, domainAPI *domain.API, dnsZone, recordName string, recordType domain.RecordType, timeout time.Duration) (*domain.Record, error) { retryInterval := defaultDomainZoneRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return domainAPI.WaitForDNSRecordExist(&domain.WaitForDNSRecordExistRequest{ diff --git a/scaleway/helpers_flexible_ip.go b/scaleway/helpers_flexible_ip.go index 9f3cc453e..99ff90bee 100644 --- a/scaleway/helpers_flexible_ip.go +++ b/scaleway/helpers_flexible_ip.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" flexibleip "github.com/scaleway/scaleway-sdk-go/api/flexibleip/v1alpha1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -40,8 +41,8 @@ func fipAPIWithZoneAndID(m interface{}, id string) (*flexibleip.API, scw.Zone, s func waitFlexibleIP(ctx context.Context, api *flexibleip.API, zone scw.Zone, id string, timeout time.Duration) (*flexibleip.FlexibleIP, error) { retryInterval := retryFlexibleIPInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForFlexibleIP(&flexibleip.WaitForFlexibleIPRequest{ diff --git a/scaleway/helpers_function.go b/scaleway/helpers_function.go index 2a46c0b13..bb4e23264 100644 --- a/scaleway/helpers_function.go +++ b/scaleway/helpers_function.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" function "github.com/scaleway/scaleway-sdk-go/api/function/v1beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -50,8 +51,8 @@ func functionAPIWithRegionAndID(m interface{}, id string) (*function.API, scw.Re func waitForFunctionNamespace(ctx context.Context, functionAPI *function.API, region scw.Region, id string, timeout time.Duration) (*function.Namespace, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } ns, err := functionAPI.WaitForNamespace(&function.WaitForNamespaceRequest{ @@ -66,8 +67,8 @@ func waitForFunctionNamespace(ctx context.Context, functionAPI *function.API, re func waitForFunction(ctx context.Context, functionAPI *function.API, region scw.Region, id string, timeout time.Duration) (*function.Function, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } f, err := functionAPI.WaitForFunction(&function.WaitForFunctionRequest{ @@ -82,8 +83,8 @@ func waitForFunction(ctx context.Context, functionAPI *function.API, region scw. func waitForFunctionCron(ctx context.Context, functionAPI *function.API, region scw.Region, cronID string, timeout time.Duration) (*function.Cron, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return functionAPI.WaitForCron(&function.WaitForCronRequest{ @@ -96,8 +97,8 @@ func waitForFunctionCron(ctx context.Context, functionAPI *function.API, region func waitForFunctionDomain(ctx context.Context, functionAPI *function.API, region scw.Region, id string, timeout time.Duration) (*function.Domain, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } domain, err := functionAPI.WaitForDomain(&function.WaitForDomainRequest{ @@ -112,8 +113,8 @@ func waitForFunctionDomain(ctx context.Context, functionAPI *function.API, regio func waitForFunctionTrigger(ctx context.Context, functionAPI *function.API, region scw.Region, id string, timeout time.Duration) (*function.Trigger, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } trigger, err := functionAPI.WaitForTrigger(&function.WaitForTriggerRequest{ diff --git a/scaleway/helpers_instance.go b/scaleway/helpers_instance.go index c6f3ab0df..93ac6cb7e 100644 --- a/scaleway/helpers_instance.go +++ b/scaleway/helpers_instance.go @@ -16,6 +16,7 @@ import ( "github.com/scaleway/scaleway-sdk-go/api/instance/v1" "github.com/scaleway/scaleway-sdk-go/api/vpc/v2" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -174,7 +175,7 @@ func reachState(ctx context.Context, api *InstanceBlockAPI, zone scw.Zone, serve _, err := api.blockAPI.WaitForVolumeAndReferences(&block.WaitForVolumeAndReferencesRequest{ VolumeID: volume.ID, Zone: zone, - RetryInterval: DefaultWaitRetryInterval, + RetryInterval: transport.DefaultWaitRetryInterval, }) if err != nil { return err @@ -183,7 +184,7 @@ func reachState(ctx context.Context, api *InstanceBlockAPI, zone scw.Zone, serve _, err = api.WaitForVolume(&instance.WaitForVolumeRequest{ Zone: zone, VolumeID: volume.ID, - RetryInterval: DefaultWaitRetryInterval, + RetryInterval: transport.DefaultWaitRetryInterval, }) if err != nil { return err @@ -197,7 +198,7 @@ func reachState(ctx context.Context, api *InstanceBlockAPI, zone scw.Zone, serve Action: a, Zone: zone, Timeout: scw.TimeDurationPtr(defaultInstanceServerWaitTimeout), - RetryInterval: DefaultWaitRetryInterval, + RetryInterval: transport.DefaultWaitRetryInterval, }) if err != nil { return err @@ -467,8 +468,8 @@ func (ph *privateNICsHandler) get(key string) (interface{}, error) { func waitForInstanceSnapshot(ctx context.Context, api *instance.API, zone scw.Zone, id string, timeout time.Duration) (*instance.Snapshot, error) { retryInterval := defaultInstanceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } snapshot, err := api.WaitForSnapshot(&instance.WaitForSnapshotRequest{ @@ -483,8 +484,8 @@ func waitForInstanceSnapshot(ctx context.Context, api *instance.API, zone scw.Zo func waitForInstanceVolume(ctx context.Context, api *instance.API, zone scw.Zone, id string, timeout time.Duration) (*instance.Volume, error) { retryInterval := defaultInstanceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } volume, err := api.WaitForVolume(&instance.WaitForVolumeRequest{ @@ -498,8 +499,8 @@ func waitForInstanceVolume(ctx context.Context, api *instance.API, zone scw.Zone func waitForInstanceServer(ctx context.Context, api *instance.API, zone scw.Zone, id string, timeout time.Duration) (*instance.Server, error) { retryInterval := defaultInstanceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } server, err := api.WaitForServer(&instance.WaitForServerRequest{ @@ -514,8 +515,8 @@ func waitForInstanceServer(ctx context.Context, api *instance.API, zone scw.Zone func waitForPrivateNIC(ctx context.Context, instanceAPI *instance.API, zone scw.Zone, serverID string, privateNICID string, timeout time.Duration) (*instance.PrivateNIC, error) { retryInterval := defaultInstanceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } nic, err := instanceAPI.WaitForPrivateNIC(&instance.WaitForPrivateNICRequest{ @@ -531,8 +532,8 @@ func waitForPrivateNIC(ctx context.Context, instanceAPI *instance.API, zone scw. func waitForMACAddress(ctx context.Context, instanceAPI *instance.API, zone scw.Zone, serverID string, privateNICID string, timeout time.Duration) (*instance.PrivateNIC, error) { retryInterval := defaultInstanceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } nic, err := instanceAPI.WaitForMACAddress(&instance.WaitForMACAddressRequest{ @@ -548,8 +549,8 @@ func waitForMACAddress(ctx context.Context, instanceAPI *instance.API, zone scw. func waitForInstanceImage(ctx context.Context, api *instance.API, zone scw.Zone, id string, timeout time.Duration) (*instance.Image, error) { retryInterval := defaultInstanceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } image, err := api.WaitForImage(&instance.WaitForImageRequest{ diff --git a/scaleway/helpers_iot.go b/scaleway/helpers_iot.go index 758ac996a..0a145cc24 100644 --- a/scaleway/helpers_iot.go +++ b/scaleway/helpers_iot.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/scaleway/scaleway-sdk-go/api/iot/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -37,8 +38,8 @@ func iotAPIWithRegionAndID(m interface{}, id string) (*iot.API, scw.Region, stri func waitIotHub(ctx context.Context, api *iot.API, region scw.Region, id string, timeout time.Duration) (*iot.Hub, error) { retryInterval := defaultIoTRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } hub, err := api.WaitForHub(&iot.WaitForHubRequest{ diff --git a/scaleway/helpers_k8s.go b/scaleway/helpers_k8s.go index e412b7e25..52598c9c5 100644 --- a/scaleway/helpers_k8s.go +++ b/scaleway/helpers_k8s.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/scaleway/scaleway-sdk-go/api/k8s/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -78,8 +79,8 @@ func k8sGetLatestVersionFromMinor(ctx context.Context, k8sAPI *k8s.API, region s func waitK8SCluster(ctx context.Context, k8sAPI *k8s.API, region scw.Region, clusterID string, timeout time.Duration) (*k8s.Cluster, error) { retryInterval := defaultK8SRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } cluster, err := k8sAPI.WaitForCluster(&k8s.WaitForClusterRequest{ @@ -94,8 +95,8 @@ func waitK8SCluster(ctx context.Context, k8sAPI *k8s.API, region scw.Region, clu func waitK8SClusterPool(ctx context.Context, k8sAPI *k8s.API, region scw.Region, clusterID string, timeout time.Duration) (*k8s.Cluster, error) { retryInterval := defaultK8SRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return k8sAPI.WaitForClusterPool(&k8s.WaitForClusterRequest{ @@ -108,8 +109,8 @@ func waitK8SClusterPool(ctx context.Context, k8sAPI *k8s.API, region scw.Region, func waitK8SClusterStatus(ctx context.Context, k8sAPI *k8s.API, cluster *k8s.Cluster, status k8s.ClusterStatus, timeout time.Duration) (*k8s.Cluster, error) { retryInterval := defaultK8SRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } cluster, err := k8sAPI.WaitForCluster(&k8s.WaitForClusterRequest{ @@ -131,8 +132,8 @@ func waitK8SClusterStatus(ctx context.Context, k8sAPI *k8s.API, cluster *k8s.Clu func waitK8SPoolReady(ctx context.Context, k8sAPI *k8s.API, region scw.Region, poolID string, timeout time.Duration) (*k8s.Pool, error) { retryInterval := defaultK8SRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } pool, err := k8sAPI.WaitForPool(&k8s.WaitForPoolRequest{ diff --git a/scaleway/helpers_lb.go b/scaleway/helpers_lb.go index f5ba31f6f..c154fa991 100644 --- a/scaleway/helpers_lb.go +++ b/scaleway/helpers_lb.go @@ -17,6 +17,7 @@ import ( lbSDK "github.com/scaleway/scaleway-sdk-go/api/lb/v1" "github.com/scaleway/scaleway-sdk-go/scw" validator "github.com/scaleway/scaleway-sdk-go/validation" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -459,8 +460,8 @@ func expandLbPrivateNetworkDHCPConfig(raw interface{}) *lbSDK.PrivateNetworkDHCP func waitForLB(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) (*lbSDK.LB, error) { retryInterval := defaultWaitLBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } loadBalancer, err := lbAPI.WaitForLb(&lbSDK.ZonedAPIWaitForLBRequest{ @@ -475,8 +476,8 @@ func waitForLB(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID s func waitForLbInstances(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) (*lbSDK.LB, error) { retryInterval := defaultWaitLBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } loadBalancer, err := lbAPI.WaitForLbInstances(&lbSDK.ZonedAPIWaitForLBInstancesRequest{ @@ -491,8 +492,8 @@ func waitForLbInstances(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zon func waitForLBPN(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID string, timeout time.Duration) ([]*lbSDK.PrivateNetwork, error) { retryInterval := defaultWaitLBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } privateNetworks, err := lbAPI.WaitForLBPN(&lbSDK.ZonedAPIWaitForLBPNRequest{ @@ -507,8 +508,8 @@ func waitForLBPN(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, lbID func waitForLBCertificate(ctx context.Context, lbAPI *lbSDK.ZonedAPI, zone scw.Zone, id string, timeout time.Duration) (*lbSDK.Certificate, error) { retryInterval := defaultWaitLBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } certificate, err := lbAPI.WaitForLBCertificate(&lbSDK.ZonedAPIWaitForLBCertificateRequest{ diff --git a/scaleway/helpers_rdb.go b/scaleway/helpers_rdb.go index 7d6de0f43..65c3ead70 100644 --- a/scaleway/helpers_rdb.go +++ b/scaleway/helpers_rdb.go @@ -13,6 +13,7 @@ import ( "github.com/scaleway/scaleway-sdk-go/api/ipam/v1" "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -70,8 +71,8 @@ func expandInstanceSettings(i interface{}) []*rdb.InstanceSetting { func waitForRDBInstance(ctx context.Context, api *rdb.API, region scw.Region, id string, timeout time.Duration) (*rdb.Instance, error) { retryInterval := defaultWaitRDBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForInstance(&rdb.WaitForInstanceRequest{ @@ -84,8 +85,8 @@ func waitForRDBInstance(ctx context.Context, api *rdb.API, region scw.Region, id func waitForRDBDatabaseBackup(ctx context.Context, api *rdb.API, region scw.Region, id string, timeout time.Duration) (*rdb.DatabaseBackup, error) { retryInterval := defaultWaitRDBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForDatabaseBackup(&rdb.WaitForDatabaseBackupRequest{ @@ -98,8 +99,8 @@ func waitForRDBDatabaseBackup(ctx context.Context, api *rdb.API, region scw.Regi func waitForRDBReadReplica(ctx context.Context, api *rdb.API, region scw.Region, id string, timeout time.Duration) (*rdb.ReadReplica, error) { retryInterval := defaultWaitRDBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForReadReplica(&rdb.WaitForReadReplicaRequest{ diff --git a/scaleway/helpers_redis.go b/scaleway/helpers_redis.go index 2e9170b06..8ddb623cb 100644 --- a/scaleway/helpers_redis.go +++ b/scaleway/helpers_redis.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/scaleway/scaleway-sdk-go/api/redis/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -46,8 +47,8 @@ func redisAPIWithZoneAndID(m interface{}, id string) (*redis.API, scw.Zone, stri func waitForRedisCluster(ctx context.Context, api *redis.API, zone scw.Zone, id string, timeout time.Duration) (*redis.Cluster, error) { retryInterval := defaultWaitRedisClusterRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForCluster(&redis.WaitForClusterRequest{ diff --git a/scaleway/helpers_registry.go b/scaleway/helpers_registry.go index 7849add60..b2cab7c82 100644 --- a/scaleway/helpers_registry.go +++ b/scaleway/helpers_registry.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/scaleway/scaleway-sdk-go/api/registry/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -45,8 +46,8 @@ func registryAPIWithRegionAndID(m interface{}, id string) (*registry.API, scw.Re func waitForRegistryNamespace(ctx context.Context, api *registry.API, region scw.Region, id string, timeout time.Duration) (*registry.Namespace, error) { retryInterval := defaultRegistryNamespaceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } ns, err := api.WaitForNamespace(®istry.WaitForNamespaceRequest{ @@ -61,8 +62,8 @@ func waitForRegistryNamespace(ctx context.Context, api *registry.API, region scw func waitForRegistryNamespaceDelete(ctx context.Context, api *registry.API, region scw.Region, id string, timeout time.Duration) (*registry.Namespace, error) { retryInterval := defaultRegistryNamespaceRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } terminalStatus := map[registry.NamespaceStatus]struct{}{ diff --git a/scaleway/helpers_sdb_sql.go b/scaleway/helpers_sdb_sql.go index a0ea14d48..c6dcb8272 100644 --- a/scaleway/helpers_sdb_sql.go +++ b/scaleway/helpers_sdb_sql.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" serverless_sqldb "github.com/scaleway/scaleway-sdk-go/api/serverless_sqldb/v1alpha1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -41,8 +42,8 @@ func serverlessSQLdbAPIWithRegionAndID(m interface{}, regionalID string) (*serve func waitForServerlessSQLDBDatabase(ctx context.Context, sdbAPI *serverless_sqldb.API, region scw.Region, id string, timeout time.Duration) (*serverless_sqldb.Database, error) { retryInterval := defaultFunctionRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } database, err := sdbAPI.WaitForDatabase(&serverless_sqldb.WaitForDatabaseRequest{ diff --git a/scaleway/helpers_tem.go b/scaleway/helpers_tem.go index 5583b7d41..139934bbd 100644 --- a/scaleway/helpers_tem.go +++ b/scaleway/helpers_tem.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" tem "github.com/scaleway/scaleway-sdk-go/api/tem/v1alpha1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -40,8 +41,8 @@ func temAPIWithRegionAndID(m interface{}, id string) (*tem.API, scw.Region, stri func waitForTemDomain(ctx context.Context, api *tem.API, region scw.Region, id string, timeout time.Duration) (*tem.Domain, error) { retryInterval := defaultTemDomainRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } domain, err := api.WaitForDomain(&tem.WaitForDomainRequest{ diff --git a/scaleway/helpers_vpcgw.go b/scaleway/helpers_vpcgw.go index c76a1bc50..28423f7e3 100644 --- a/scaleway/helpers_vpcgw.go +++ b/scaleway/helpers_vpcgw.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" vpcgw "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -41,8 +42,8 @@ func vpcgwAPIWithZoneAndID(m interface{}, id string) (*vpcgw.API, scw.Zone, stri func waitForVPCPublicGateway(ctx context.Context, api *vpcgw.API, zone scw.Zone, id string, timeout time.Duration) (*vpcgw.Gateway, error) { retryInterval := defaultVPCGatewayRetry - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } gateway, err := api.WaitForGateway(&vpcgw.WaitForGatewayRequest{ @@ -57,8 +58,8 @@ func waitForVPCPublicGateway(ctx context.Context, api *vpcgw.API, zone scw.Zone, func waitForVPCGatewayNetwork(ctx context.Context, api *vpcgw.API, zone scw.Zone, id string, timeout time.Duration) (*vpcgw.GatewayNetwork, error) { retryIntervalGWNetwork := defaultVPCGatewayRetry - if DefaultWaitRetryInterval != nil { - retryIntervalGWNetwork = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryIntervalGWNetwork = *transport.DefaultWaitRetryInterval } gatewayNetwork, err := api.WaitForGatewayNetwork(&vpcgw.WaitForGatewayNetworkRequest{ @@ -73,8 +74,8 @@ func waitForVPCGatewayNetwork(ctx context.Context, api *vpcgw.API, zone scw.Zone func waitForDHCPEntries(ctx context.Context, api *vpcgw.API, zone scw.Zone, gatewayID string, macAddress string, timeout time.Duration) (*vpcgw.ListDHCPEntriesResponse, error) { retryIntervalDHCPEntries := defaultVPCGatewayRetry - if DefaultWaitRetryInterval != nil { - retryIntervalDHCPEntries = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryIntervalDHCPEntries = *transport.DefaultWaitRetryInterval } req := &vpcgw.WaitForDHCPEntriesRequest{ diff --git a/scaleway/helpers_webhosting.go b/scaleway/helpers_webhosting.go index 4b15b050d..1a2b1b376 100644 --- a/scaleway/helpers_webhosting.go +++ b/scaleway/helpers_webhosting.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" webhosting "github.com/scaleway/scaleway-sdk-go/api/webhosting/v1alpha1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const ( @@ -83,8 +84,8 @@ func flattenHostingOptions(options []*webhosting.HostingOption) []map[string]int func waitForHosting(ctx context.Context, api *webhosting.API, region scw.Region, hostingID string, timeout time.Duration) (*webhosting.Hosting, error) { retryInterval := hostingRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForHosting(&webhosting.WaitForHostingRequest{ diff --git a/scaleway/provider.go b/scaleway/provider.go index 64a5acdef..6a3600171 100644 --- a/scaleway/provider.go +++ b/scaleway/provider.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) const appendUserAgentEnvVar = "TF_APPEND_USER_AGENT" @@ -364,7 +365,7 @@ func buildMeta(ctx context.Context, config *metaConfig) (*Meta, error) { scw.WithProfile(profile), } - httpClient := &http.Client{Transport: newRetryableTransport(http.DefaultTransport)} + httpClient := &http.Client{Transport: transport.NewRetryableTransport(http.DefaultTransport)} if config.httpClient != nil { httpClient = config.httpClient } diff --git a/scaleway/provider_test.go b/scaleway/provider_test.go index f7d166528..38178be91 100644 --- a/scaleway/provider_test.go +++ b/scaleway/provider_test.go @@ -25,6 +25,7 @@ import ( iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/scaleway-sdk-go/strcase" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" "github.com/stretchr/testify/require" ) @@ -317,12 +318,12 @@ func getHTTPRecoder(t *testing.T, update bool) (client *http.Client, cleanup fun // Add a filter that will replace sensitive values with fixed values r.AddSaveFilter(cassetteSensitiveFieldsAnonymizer) - retryOptions := retryableTransportOptions{} + retryOptions := transport.RetryableTransportOptions{} if !*UpdateCassettes { retryOptions.RetryWaitMax = scw.TimeDurationPtr(0) } - return &http.Client{Transport: newRetryableTransportWithOptions(r, retryOptions)}, func() { + return &http.Client{Transport: transport.NewRetryableTransportWithOptions(r, retryOptions)}, func() { require.NoError(t, r.Stop()) // Make sure recorder is stopped once done with it }, nil } @@ -577,7 +578,7 @@ func NewTestTools(t *testing.T) *TestTools { if !*UpdateCassettes { tmp := 0 * time.Second - DefaultWaitRetryInterval = &tmp + transport.DefaultWaitRetryInterval = &tmp } return &TestTools{ diff --git a/scaleway/resource_document_db_read_replica.go b/scaleway/resource_document_db_read_replica.go index c1042e34a..dd9199dd9 100644 --- a/scaleway/resource_document_db_read_replica.go +++ b/scaleway/resource_document_db_read_replica.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" documentdb "github.com/scaleway/scaleway-sdk-go/api/documentdb/v1beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func resourceScalewayDocumentDBReadReplica() *schema.Resource { @@ -176,8 +177,8 @@ func expandDocumentDBReadReplicaEndpointsSpecPrivateNetwork(data interface{}) (* func waitForDocumentDBReadReplica(ctx context.Context, api *documentdb.API, region scw.Region, id string, timeout time.Duration) (*documentdb.ReadReplica, error) { retryInterval := defaultWaitDocumentDBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } return api.WaitForReadReplica(&documentdb.WaitForReadReplicaRequest{ diff --git a/scaleway/resource_instance_image.go b/scaleway/resource_instance_image.go index 18e543c48..dab8d37ea 100644 --- a/scaleway/resource_instance_image.go +++ b/scaleway/resource_instance_image.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func resourceScalewayInstanceImage() *schema.Resource { @@ -213,7 +214,7 @@ func resourceScalewayInstanceImageCreate(ctx context.Context, d *schema.Resource _, err = instanceAPI.WaitForImage(&instance.WaitForImageRequest{ ImageID: res.Image.ID, Zone: zone, - RetryInterval: DefaultWaitRetryInterval, + RetryInterval: transport.DefaultWaitRetryInterval, Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutCreate)), }, scw.WithContext(ctx)) if err != nil { diff --git a/scaleway/resource_instance_snapshot.go b/scaleway/resource_instance_snapshot.go index 700e4486e..7491d73d9 100644 --- a/scaleway/resource_instance_snapshot.go +++ b/scaleway/resource_instance_snapshot.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func resourceScalewayInstanceSnapshot() *schema.Resource { @@ -143,7 +144,7 @@ func resourceScalewayInstanceSnapshotCreate(ctx context.Context, d *schema.Resou _, err = instanceAPI.WaitForSnapshot(&instance.WaitForSnapshotRequest{ SnapshotID: res.Snapshot.ID, Zone: zone, - RetryInterval: DefaultWaitRetryInterval, + RetryInterval: transport.DefaultWaitRetryInterval, Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutCreate)), }) if err != nil { diff --git a/scaleway/resource_instance_volume.go b/scaleway/resource_instance_volume.go index 590406913..66385f403 100644 --- a/scaleway/resource_instance_volume.go +++ b/scaleway/resource_instance_volume.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/scaleway/scaleway-sdk-go/api/instance/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func resourceScalewayInstanceVolume() *schema.Resource { @@ -116,7 +117,7 @@ func resourceScalewayInstanceVolumeCreate(ctx context.Context, d *schema.Resourc _, err = instanceAPI.WaitForVolume(&instance.WaitForVolumeRequest{ VolumeID: res.Volume.ID, Zone: zone, - RetryInterval: DefaultWaitRetryInterval, + RetryInterval: transport.DefaultWaitRetryInterval, Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutCreate)), }, scw.WithContext(ctx)) if err != nil { @@ -232,7 +233,7 @@ func resourceScalewayInstanceVolumeDelete(ctx context.Context, d *schema.Resourc volume, err := instanceAPI.WaitForVolume(&instance.WaitForVolumeRequest{ Zone: zone, VolumeID: id, - RetryInterval: DefaultWaitRetryInterval, + RetryInterval: transport.DefaultWaitRetryInterval, Timeout: scw.TimeDurationPtr(d.Timeout(schema.TimeoutDelete)), }, scw.WithContext(ctx)) if err != nil { diff --git a/scaleway/resource_lb_ip_test.go b/scaleway/resource_lb_ip_test.go index e5d5b512a..e9deec53a 100644 --- a/scaleway/resource_lb_ip_test.go +++ b/scaleway/resource_lb_ip_test.go @@ -10,6 +10,7 @@ import ( lbSDK "github.com/scaleway/scaleway-sdk-go/api/lb/v1" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func init() { @@ -151,8 +152,8 @@ func testAccCheckScalewayLbIPDestroy(tt *TestTools) resource.TestCheckFunc { if lbExist && len(lbID) > 0 { retryInterval := defaultWaitLBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } _, err := lbAPI.WaitForLbInstances(&lbSDK.ZonedAPIWaitForLBInstancesRequest{ diff --git a/scaleway/resource_lb_test.go b/scaleway/resource_lb_test.go index 6f6b57c78..47d741c8e 100644 --- a/scaleway/resource_lb_test.go +++ b/scaleway/resource_lb_test.go @@ -13,6 +13,7 @@ import ( lbSDK "github.com/scaleway/scaleway-sdk-go/api/lb/v1" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func init() { @@ -37,8 +38,8 @@ func testSweepLB(_ string) error { for _, l := range listLBs.LBs { retryInterval := defaultWaitLBRetryInterval - if DefaultWaitRetryInterval != nil { - retryInterval = *DefaultWaitRetryInterval + if transport.DefaultWaitRetryInterval != nil { + retryInterval = *transport.DefaultWaitRetryInterval } _, err := lbAPI.WaitForLbInstances(&lbSDK.ZonedAPIWaitForLBInstancesRequest{ diff --git a/scaleway/resource_mnq_sqs_queue.go b/scaleway/resource_mnq_sqs_queue.go index 45492ea3d..3544c8436 100644 --- a/scaleway/resource_mnq_sqs_queue.go +++ b/scaleway/resource_mnq_sqs_queue.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" mnq "github.com/scaleway/scaleway-sdk-go/api/mnq/v1beta1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func resourceScalewayMNQSQSQueue() *schema.Resource { @@ -166,7 +167,7 @@ func resourceScalewayMNQSQSQueueCreate(ctx context.Context, d *schema.ResourceDa QueueName: scw.StringPtr(queueName), } - _, err = retryWhenAWSErrCodeEquals(ctx, []string{sqs.ErrCodeQueueDeletedRecently}, &RetryWhenConfig[*sqs.CreateQueueOutput]{ + _, err = transport.RetryWhenAWSErrCodeEquals(ctx, []string{sqs.ErrCodeQueueDeletedRecently}, &transport.RetryWhenConfig[*sqs.CreateQueueOutput]{ Timeout: d.Timeout(schema.TimeoutCreate), Interval: defaultMNQQueueRetryInterval, Function: func() (*sqs.CreateQueueOutput, error) { @@ -193,7 +194,7 @@ func resourceScalewayMNQSQSQueueRead(ctx context.Context, d *schema.ResourceData return diag.FromErr(err) } - queue, err := retryWhenAWSErrCodeEquals(ctx, []string{sqs.ErrCodeQueueDoesNotExist}, &RetryWhenConfig[*sqs.GetQueueUrlOutput]{ + queue, err := transport.RetryWhenAWSErrCodeEquals(ctx, []string{sqs.ErrCodeQueueDoesNotExist}, &transport.RetryWhenConfig[*sqs.GetQueueUrlOutput]{ Timeout: d.Timeout(schema.TimeoutRead), Interval: defaultMNQQueueRetryInterval, Function: func() (*sqs.GetQueueUrlOutput, error) { @@ -242,7 +243,7 @@ func resourceScalewayMNQSQSQueueUpdate(ctx context.Context, d *schema.ResourceDa return diag.FromErr(err) } - queue, err := retryWhenAWSErrCodeEquals(ctx, []string{sqs.ErrCodeQueueDoesNotExist}, &RetryWhenConfig[*sqs.GetQueueUrlOutput]{ + queue, err := transport.RetryWhenAWSErrCodeEquals(ctx, []string{sqs.ErrCodeQueueDoesNotExist}, &transport.RetryWhenConfig[*sqs.GetQueueUrlOutput]{ Timeout: d.Timeout(schema.TimeoutUpdate), Interval: defaultMNQQueueRetryInterval, Function: func() (*sqs.GetQueueUrlOutput, error) { @@ -304,7 +305,7 @@ func resourceScalewayMNQSQSQueueDelete(ctx context.Context, d *schema.ResourceDa return diag.Errorf("failed to delete SQS Queue (%s): %s", d.Id(), err) } - _, _ = retryWhenAWSErrCodeNotEquals(ctx, []string{sqs.ErrCodeQueueDoesNotExist}, &RetryWhenConfig[*sqs.GetQueueUrlOutput]{ + _, _ = transport.RetryWhenAWSErrCodeNotEquals(ctx, []string{sqs.ErrCodeQueueDoesNotExist}, &transport.RetryWhenConfig[*sqs.GetQueueUrlOutput]{ Timeout: d.Timeout(schema.TimeoutCreate), Interval: defaultMNQQueueRetryInterval, Function: func() (*sqs.GetQueueUrlOutput, error) { diff --git a/scaleway/resource_object_bucket_test.go b/scaleway/resource_object_bucket_test.go index 90fb69d3e..78177b846 100644 --- a/scaleway/resource_object_bucket_test.go +++ b/scaleway/resource_object_bucket_test.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/scaleway/scaleway-sdk-go/scw" "github.com/scaleway/terraform-provider-scaleway/v2/internal/logging" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func init() { @@ -825,7 +826,7 @@ func testAccCheckScalewayObjectBucketLifecycleConfigurationExists(tt *TestTools, _, err = s3Client.GetBucketLifecycleConfiguration(input) if err != nil { - if err == ErrRetryWhenTimeout { + if err == transport.ErrRetryWhenTimeout { return fmt.Errorf("object Storage Bucket Replication Configuration for bucket (%s) not found", rs.Primary.ID) } return err diff --git a/scaleway/resource_vpc_gateway_network.go b/scaleway/resource_vpc_gateway_network.go index f687aaa6a..855e7d4a6 100644 --- a/scaleway/resource_vpc_gateway_network.go +++ b/scaleway/resource_vpc_gateway_network.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" vpcgw "github.com/scaleway/scaleway-sdk-go/api/vpcgw/v1" "github.com/scaleway/scaleway-sdk-go/scw" + "github.com/scaleway/terraform-provider-scaleway/v2/internal/transport" ) func resourceScalewayVPCGatewayNetwork() *schema.Resource { @@ -163,7 +164,7 @@ func resourceScalewayVPCGatewayNetworkCreate(ctx context.Context, d *schema.Reso req.DHCPID = &dhcpZoned.ID } - gatewayNetwork, err := retryOnTransientStateError(func() (*vpcgw.GatewayNetwork, error) { + gatewayNetwork, err := transport.RetryOnTransientStateError(func() (*vpcgw.GatewayNetwork, error) { return vpcgwAPI.CreateGatewayNetwork(req, scw.WithContext(ctx)) }, func() (*vpcgw.Gateway, error) { tflog.Warn(ctx, "Public gateway is in transient state after waiting, retrying...")