Skip to content

Commit 9e29a0d

Browse files
GnoaleLaure-di
authored andcommitted
fix(iam): handle http 409 transient error (scaleway#3142)
* fix(iam): handle http 409 transient error * exponentialy retry Add amd Remove group member requests * use internal transport retry api
1 parent 21677eb commit 9e29a0d

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

internal/services/iam/group_membership.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"fmt"
66
"strings"
7+
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
1112
"github.com/scaleway/scaleway-sdk-go/scw"
1213
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
14+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
1315
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1416
)
1517

@@ -53,11 +55,11 @@ func resourceIamGroupMembershipCreate(ctx context.Context, d *schema.ResourceDat
5355
userID := types.ExpandStringPtr(d.Get("user_id"))
5456
applicationID := types.ExpandStringPtr(d.Get("application_id"))
5557

56-
group, err := api.AddGroupMember(&iam.AddGroupMemberRequest{
58+
group, err := MakeGroupRequest(ctx, api, &iam.AddGroupMemberRequest{
5759
GroupID: d.Get("group_id").(string),
5860
UserID: userID,
5961
ApplicationID: applicationID,
60-
}, scw.WithContext(ctx))
62+
})
6163
if err != nil {
6264
return diag.FromErr(err)
6365
}
@@ -74,7 +76,7 @@ func resourceIamGroupMembershipRead(ctx context.Context, d *schema.ResourceData,
7476
if err != nil {
7577
return diag.FromErr(err)
7678
}
77-
79+
// http GET request should not return a 409 error
7880
group, err := api.GetGroup(&iam.GetGroupRequest{
7981
GroupID: groupID,
8082
}, scw.WithContext(ctx))
@@ -139,7 +141,7 @@ func resourceIamGroupMembershipDelete(ctx context.Context, d *schema.ResourceDat
139141
req.ApplicationID = &applicationID
140142
}
141143

142-
_, err = api.RemoveGroupMember(req, scw.WithContext(ctx))
144+
_, err = MakeGroupRequest(ctx, api, req)
143145
if err != nil {
144146
if httperrors.Is404(err) {
145147
d.SetId("")
@@ -178,3 +180,24 @@ func ExpandGroupMembershipID(id string) (groupID string, userID string, applicat
178180

179181
return
180182
}
183+
184+
func MakeGroupRequest(ctx context.Context, api *iam.API, request any) (*iam.Group, error) {
185+
retryInterval := 100 * time.Millisecond
186+
187+
group, err := transport.RetryOnTransientStateError(func() (*iam.Group, error) {
188+
switch req := request.(type) {
189+
case *iam.AddGroupMemberRequest:
190+
return api.AddGroupMember(req, scw.WithContext(ctx))
191+
case *iam.RemoveGroupMemberRequest:
192+
return api.RemoveGroupMember(req, scw.WithContext(ctx))
193+
default:
194+
return nil, fmt.Errorf("invalid request type: %T", req)
195+
}
196+
}, func() (string, error) {
197+
time.Sleep(retryInterval) // lintignore: R018
198+
199+
return "", nil
200+
})
201+
202+
return group, err
203+
}

0 commit comments

Comments
 (0)