Skip to content

Commit

Permalink
Introduce wait handler (#220)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Hoppe <[email protected]>
  • Loading branch information
roberth1988 and Robert Hoppe authored Jul 30, 2024
1 parent 5bf5ece commit b486f6e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/services/iaas-api/v1/network/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// this file is used for validating network data and properties

package network

import (
"fmt"
"regexp"
)

// ValidateNetworkName validates a given network name
func ValidateNetworkName(name string) error {
if len(name) > 63 || name == "" {
return fmt.Errorf("name bust be non-empty and < 64 characters")
}

exp := `^[A-Za-z0-9]+((-|_|\s|\.)[A-Za-z0-9]+)*$`
r := regexp.MustCompile(exp)
if !r.MatchString(name) {
return fmt.Errorf("invalid cluster name. valid name is of: %s", exp)
}
return nil
}
89 changes: 89 additions & 0 deletions pkg/services/iaas-api/v1/network/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package network

import (
"context"
"net/http"

"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, name string) *wait.Handler {
return wait.New(func() (res interface{}, done bool, err error) {
resp, err := c.V1ListNetworksInProject(ctx, projectID)
if err != nil {
return nil, false, err
}

if resp.JSON200 != nil && len(resp.JSON200.Items) > 0 {
for _, n := range resp.JSON200.Items {
if n.Name == name {
// the network is created successfully
return n, true, nil
}
}

// the network that is created was not found
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.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)
}

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 {
return nil, false, err
}

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.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.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.StatusCode() == http.StatusConflict {
// can't delete network. It is still has systems connected to it.
return nil, false, errors.New(resp.JSON403.Msg)
}

// 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 b486f6e

Please sign in to comment.