Skip to content

Commit 0ef9e5a

Browse files
authored
fix(rdb): retry database read on transient_state 409 by waiting instance then reissuing ListDatabases (#3369)
1 parent fe97912 commit 0ef9e5a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

internal/services/rdb/database.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ func ResourceRdbDatabaseCreate(ctx context.Context, d *schema.ResourceData, m an
134134
}
135135

136136
func getDatabase(ctx context.Context, api *rdb.API, r scw.Region, instanceID, dbName string) (*rdb.Database, error) {
137-
res, err := api.ListDatabases(&rdb.ListDatabasesRequest{
138-
Region: r,
139-
InstanceID: instanceID,
140-
Name: &dbName,
141-
}, scw.WithContext(ctx))
137+
res, err := retryRDBReadOnTransient(ctx, api, r, instanceID, func() (*rdb.ListDatabasesResponse, error) {
138+
return api.ListDatabases(&rdb.ListDatabasesRequest{
139+
Region: r,
140+
InstanceID: instanceID,
141+
Name: &dbName,
142+
}, scw.WithContext(ctx))
143+
})
142144
if err != nil {
143145
return nil, err
144146
}

internal/services/rdb/helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/scaleway/scaleway-sdk-go/scw"
1313
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1414
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
15+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
1516
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1617
)
1718

@@ -119,3 +120,14 @@ func LoadBalancerDiffSuppressFunc(k, oldValue, newValue string, d *schema.Resour
119120

120121
return false
121122
}
123+
124+
// retryRDBReadOnTransient wraps a read action to handle transient_state (HTTP 409) by
125+
// waiting for the instance to be ready, then retrying the action.
126+
func retryRDBReadOnTransient[T any](ctx context.Context, api *rdb.API, region scw.Region, instanceID string, action func() (T, error)) (T, error) {
127+
return transport.RetryOnTransientStateError(
128+
action,
129+
func() (*rdb.Instance, error) {
130+
return waitForRDBInstance(ctx, api, region, instanceID, defaultInstanceTimeout)
131+
},
132+
)
133+
}

0 commit comments

Comments
 (0)