Skip to content

Commit fe085cd

Browse files
authored
feat(vpc): retry deleting private network if error 500 (#4912)
1 parent 62c4b64 commit fe085cd

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

internal/namespaces/vpc/v2/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func GetCommands() *core.Commands {
1313

1414
cmds.Remove("vpc", "post")
1515
cmds.MustFind("vpc", "private-network", "get").Override(privateNetworkGetBuilder)
16+
cmds.MustFind("vpc", "private-network", "delete").Override(privateNetworkDeleteBuilder)
1617
human.RegisterMarshalerFunc(vpc.PrivateNetwork{}, privateNetworkMarshalerFunc)
1718

1819
cmds.Merge(core.NewCommands(

internal/namespaces/vpc/v2/custom_private_network.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package vpc
22

33
import (
44
"context"
5+
"errors"
6+
"net/http"
7+
"time"
58

69
"github.com/scaleway/scaleway-cli/v2/core"
710
"github.com/scaleway/scaleway-cli/v2/core/human"
@@ -715,3 +718,39 @@ func intersectZones(regionZones, apiZones []scw.Zone) []scw.Zone {
715718

716719
return intersect
717720
}
721+
722+
func privateNetworkDeleteBuilder(c *core.Command) *core.Command {
723+
c.Run = func(ctx context.Context, args any) (i any, e error) {
724+
request := args.(*vpc.DeletePrivateNetworkRequest)
725+
726+
client := core.ExtractClient(ctx)
727+
api := vpc.NewAPI(client)
728+
729+
return tryDeletingPrivateNetwork(ctx, api, request.Region, request.PrivateNetworkID, 5)
730+
}
731+
732+
return c
733+
}
734+
735+
func tryDeletingPrivateNetwork(
736+
ctx context.Context,
737+
api *vpc.API,
738+
region scw.Region,
739+
pnID string,
740+
retriesLeft int,
741+
) (*vpc.PrivateNetwork, error) {
742+
err := api.DeletePrivateNetwork(&vpc.DeletePrivateNetworkRequest{
743+
PrivateNetworkID: pnID,
744+
Region: region,
745+
}, scw.WithContext(ctx))
746+
747+
var respErr *scw.ResponseError
748+
if errors.As(err, &respErr) && respErr.StatusCode == http.StatusInternalServerError {
749+
time.Sleep(time.Second * 5)
750+
if retriesLeft > 0 {
751+
return tryDeletingPrivateNetwork(ctx, api, region, pnID, retriesLeft-1)
752+
}
753+
}
754+
755+
return nil, err
756+
}

0 commit comments

Comments
 (0)