Skip to content

Commit

Permalink
bugfix: WaitHandlers for Create and Delete network are fixed (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilGeorgiev committed Feb 23, 2024
1 parent 5f52368 commit 98a5c24
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions pkg/services/iaas-api/v1alpha/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,79 @@ package iaas
import (
"context"
"net/http"
"strings"

"github.com/SchwarzIT/community-stackit-go-client/pkg/wait"
"github.com/pkg/errors"

openapiTypes "github.com/SchwarzIT/community-stackit-go-client/pkg/helpers/types"
)

// WaitHandler wait for the network to be created and return it.
func (*V1CreateNetworkResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID openapiTypes.UUID) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.V1ListNetworksInProject(ctx, projectID)
if err != nil {
if strings.Contains(err.Error(), http.StatusText(http.StatusForbidden)) {
return nil, false, nil
}
return nil, false, err
}
if resp.StatusCode() == http.StatusForbidden {
return nil, false, nil

if resp.JSON200 != nil {
// the network is created successfully
return nil, true, nil
}
if resp.StatusCode() == http.StatusInternalServerError {
return nil, false, nil

if resp.JSON400 != nil {
// if the server returns 400 then we can't retry the same request because the result will be the same
return nil, false, errors.New(resp.JSON400.Msg)
}
if resp.JSON200 == nil {
return nil, false, nil

if resp.JSON401 != nil {
// if the server returns 401 then we can't retry the same request because the result will be the same.
return nil, false, errors.New(resp.JSON401.Msg)
}

return resp, true, nil
if resp.JSON403 != nil {
// if the server returns 403 then we can't retry the same request because the result will be the same
return nil, false, errors.New(resp.JSON403.Msg)
}
// in all other cases we will retry the request until the network is not created or an error occurred.
return nil, false, nil
})
}

// WaitHandler wait for the network to be deleted
func (*V1DeleteNetworkResponse) WaitHandler(ctx context.Context, c *ClientWithResponses, projectID, networkID openapiTypes.UUID) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.V1GetNetwork(ctx, projectID, networkID)
if err != nil {
if strings.Contains(err.Error(), http.StatusText(http.StatusForbidden)) {
return nil, false, nil
}
return nil, false, err
}
if resp.StatusCode() == http.StatusForbidden {
return nil, false, nil

if resp.JSON404 != nil {
// the network is deleted successfully
return resp, true, nil
}

if resp.JSON400 != nil {
// can't retry the same request because the response will be the same
return nil, false, errors.New(resp.JSON400.Msg)
}
if resp.StatusCode() == http.StatusInternalServerError {
return nil, false, nil

if resp.JSON401 != nil {
// can't retry the same request because the response will be always the same
return nil, false, errors.New(resp.JSON401.Msg)
}
if resp.StatusCode() == http.StatusNotFound {
return nil, false, nil

if resp.JSON403 != nil {
// can't retry the same request because the response will be always the same
return nil, false, errors.New(resp.JSON403.Msg)
}
if resp.JSON200 == nil {
return nil, false, nil

if resp.StatusCode() == http.StatusConflict {
// can't delete network. It is still has systems connected to it.
return nil, false, errors.New(resp.JSON403.Msg)
}

return resp, true, nil
// in all other cases we will retry the request until the network is not deleted or an error occurred.
return nil, false, nil
})
}

0 comments on commit 98a5c24

Please sign in to comment.