From b486f6ec6e3f772e66eaece03869e5a4de8ca772 Mon Sep 17 00:00:00 2001 From: Robert Hoppe Date: Tue, 30 Jul 2024 22:39:27 +0200 Subject: [PATCH] Introduce wait handler (#220) Co-authored-by: Robert Hoppe --- pkg/services/iaas-api/v1/network/validate.go | 22 +++++ pkg/services/iaas-api/v1/network/wait.go | 89 ++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 pkg/services/iaas-api/v1/network/validate.go create mode 100644 pkg/services/iaas-api/v1/network/wait.go diff --git a/pkg/services/iaas-api/v1/network/validate.go b/pkg/services/iaas-api/v1/network/validate.go new file mode 100644 index 0000000..0972e66 --- /dev/null +++ b/pkg/services/iaas-api/v1/network/validate.go @@ -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 +} diff --git a/pkg/services/iaas-api/v1/network/wait.go b/pkg/services/iaas-api/v1/network/wait.go new file mode 100644 index 0000000..605b686 --- /dev/null +++ b/pkg/services/iaas-api/v1/network/wait.go @@ -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 + }) +}