Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add waiters for IaaS network and network area operations #495

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion services/iaas/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
STACKIT IaaS SDK for Go
Copyright 2024 Schwarz IT KG
Copyright 2024 Schwarz IT KG
5 changes: 4 additions & 1 deletion services/iaas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module github.com/stackitcloud/stackit-sdk-go/services/iaas

go 1.18

require github.com/stackitcloud/stackit-sdk-go/core v0.12.0
require (
github.com/google/go-cmp v0.6.0
github.com/stackitcloud/stackit-sdk-go/core v0.12.0
)

require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions services/iaas/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/stackitcloud/stackit-sdk-go/core v0.12.0 h1:auIzUUNRuydKOScvpICP4MifGgvOajiDQd+ncGmBL0U=
Expand Down
152 changes: 152 additions & 0 deletions services/iaas/wait/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package wait

import (
"context"
"fmt"
"net/http"
"time"

"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/core/wait"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
)

const (
CreateSuccess = "CREATED"
)

// Interfaces needed for tests
type APIClientInterface interface {
GetNetworkAreaExecute(ctx context.Context, organizationId, areaId string) (*iaas.NetworkArea, error)
GetProjectRequestExecute(ctx context.Context, projectId string, requestId string) (*iaas.Request, error)
GetNetworkExecute(ctx context.Context, projectId, networkId string) (*iaas.Network, error)
}

// CreateNetworkAreaWaitHandler will wait for network area creation
func CreateNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.NetworkArea] {
handler := wait.New(func() (waitFinished bool, response *iaas.NetworkArea, err error) {
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
if err != nil {
return false, area, err
}
if area.AreaId == nil || area.State == nil {
return false, area, fmt.Errorf("create failed for network area with id %s, the response is not valid: the id or the state are missing", areaId)
}
if *area.AreaId == areaId && *area.State == CreateSuccess {
return true, area, nil
}
return false, area, nil
DiogoFerrao marked this conversation as resolved.
Show resolved Hide resolved
})
handler.SetTimeout(10 * time.Minute)
return handler
}

// UpdateNetworkAreaWaitHandler will wait for network area update
func UpdateNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.NetworkArea] {
handler := wait.New(func() (waitFinished bool, response *iaas.NetworkArea, err error) {
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
if err != nil {
return false, area, err
}
if area.AreaId == nil || area.State == nil {
return false, nil, fmt.Errorf("update failed for network area with id %s, the response is not valid: the id or the state are missing", areaId)
}
// The state returns to "CREATED" after a successful update is completed
if *area.AreaId == areaId && *area.State == CreateSuccess {
return true, area, nil
}
return false, area, nil
})
handler.SetSleepBeforeWait(2 * time.Second)
handler.SetTimeout(10 * time.Minute)
return handler
}

// DeleteNetworkAreaWaitHandler will wait for network area deletion
func DeleteNetworkAreaWaitHandler(ctx context.Context, a APIClientInterface, organizationId, areaId string) *wait.AsyncActionHandler[iaas.NetworkArea] {
handler := wait.New(func() (waitFinished bool, response *iaas.NetworkArea, err error) {
area, err := a.GetNetworkAreaExecute(ctx, organizationId, areaId)
if err == nil {
return false, nil, nil
}
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if !ok {
return false, area, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError: %w", err)
}
if oapiErr.StatusCode != http.StatusNotFound {
return false, area, err
}
return true, nil, nil
DiogoFerrao marked this conversation as resolved.
Show resolved Hide resolved
})
handler.SetTimeout(10 * time.Minute)
return handler
}

// CreateNetworkWaitHandler will wait for network creation
func CreateNetworkWaitHandler(ctx context.Context, a APIClientInterface, projectId, requestId string) *wait.AsyncActionHandler[iaas.Network] {
handler := wait.New(func() (waitFinished bool, response *iaas.Network, err error) {
request, err := a.GetProjectRequestExecute(ctx, projectId, requestId)
if err != nil {
return false, nil, err
}
if request == nil || request.Resources == nil || len(*request.Resources) == 0 || (*request.Resources)[0].Id == nil {
return false, nil, fmt.Errorf("no resources found for request with id %s", requestId)
}
networkId := *(*request.Resources)[0].Id
network, err := a.GetNetworkExecute(ctx, projectId, networkId)
if err != nil {
return false, network, err
}
if network.NetworkId == nil || network.State == nil {
return false, network, fmt.Errorf("create failed for network with id %s, the response is not valid: the id or the state are missing", networkId)
}
if *network.NetworkId == networkId && *network.State == CreateSuccess {
return true, network, nil
}
return false, network, nil
})
handler.SetSleepBeforeWait(2 * time.Second)
handler.SetTimeout(10 * time.Minute)
return handler
}

// UpdateNetworkWaitHandler will wait for network update
func UpdateNetworkWaitHandler(ctx context.Context, a APIClientInterface, projectId, networkId string) *wait.AsyncActionHandler[iaas.Network] {
handler := wait.New(func() (waitFinished bool, response *iaas.Network, err error) {
network, err := a.GetNetworkExecute(ctx, projectId, networkId)
if err != nil {
return false, network, err
}
if network.NetworkId == nil || network.State == nil {
return false, network, fmt.Errorf("update failed for network area with id %s, the response is not valid: the id or the state are missing", networkId)
}
// The state returns to "CREATED" after a successful update is completed
if *network.NetworkId == networkId && *network.State == CreateSuccess {
return true, network, nil
}
return false, network, nil
})
handler.SetSleepBeforeWait(2 * time.Second)
handler.SetTimeout(10 * time.Minute)
return handler
}

// DeleteNetworkWaitHandler will wait for network deletion
func DeleteNetworkWaitHandler(ctx context.Context, a APIClientInterface, projectId, networkId string) *wait.AsyncActionHandler[iaas.Network] {
handler := wait.New(func() (waitFinished bool, response *iaas.Network, err error) {
network, err := a.GetNetworkExecute(ctx, projectId, networkId)
if err == nil {
return false, nil, nil
}
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if !ok {
return false, network, fmt.Errorf("could not convert error to oapierror.GenericOpenAPIError: %w", err)
}
if oapiErr.StatusCode != http.StatusNotFound {
return false, network, err
}
return true, nil, nil
})
handler.SetTimeout(10 * time.Minute)
return handler
}
Loading
Loading