-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Robert Hoppe <[email protected]>
- Loading branch information
1 parent
5bf5ece
commit b486f6e
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |