Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Release (2025-XX-YY)
- `cdn`: [v0.2.0](services/cdn/CHANGELOG.md#v020-2025-04-01)
- **API enhancement:** Provide waiter infrastructure

## Release (2025-03-27)
- `alb`: [v0.2.1](services/alb/CHANGELOG.md#v021-2025-03-27)
- **Bugfix:** Removed ConfigureRegion() from API client
- `cdn`: [v0.1.1](services/cdn/CHANGELOG.md#v011-2025-03-27)
Expand Down
3 changes: 3 additions & 0 deletions services/cdn/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v0.2.0 (2025-04-01)
- **API enhancement:** Provide waiter infrastructure

## v0.1.1 (2025-03-27)
- **Bugfix:** Removed ConfigureRegion() from API client

Expand Down
100 changes: 100 additions & 0 deletions services/cdn/wait/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package wait

import (
"context"
"errors"
"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/cdn"
)

const (
DistributionStateCreating = "CREATING"
DistributionStateActive = "ACTIVE"
DistributionStateUpdating = "UPDATING"
DistributionStateDeleting = "DELETING"
DistributionStateError = "ERROR"
)

// Interfaces needed for tests
type APIClientInterface interface {
GetDistributionExecute(ctx context.Context, projectId string, distributionId string) (*cdn.GetDistributionResponse, error)
}

func CreateDistributionPoolWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) {
distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId)
if err != nil {
return false, distribution, err
}
if distribution == nil ||
distribution.Distribution == nil ||
distribution.Distribution.Id == nil ||
distribution.Distribution.Status == nil {
return false, distribution, fmt.Errorf("create failed for distribution with id %s, the response is not valid (state missing)", distributionId)
}
if *distribution.Distribution.Id == distributionId {
switch *distribution.Distribution.Status {
case DistributionStateActive:
return true, distribution, err
default:
return false, distribution, err
}
}

return false, nil, nil
})

handler.SetTimeout(10 * time.Minute)
return handler
}

func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) {
distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId)
if err != nil {
return false, distribution, err
}
if distribution == nil ||
distribution.Distribution == nil ||
distribution.Distribution.Id == nil ||
distribution.Distribution.Status == nil {
return false, distribution, fmt.Errorf("update failed for distribution with id %s, the response is not valid (state missing)", distributionId)
}
if *distribution.Distribution.Id == distributionId {
switch *distribution.Distribution.Status {
case DistributionStateActive:
return true, distribution, err
default:
return false, distribution, err
}
}

return false, nil, nil
})

handler.SetTimeout(10 * time.Minute)
return handler
}

func DeleteDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) {
distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId)
if err != nil {
var oapiError *oapierror.GenericOpenAPIError
if errors.As(err, &oapiError) {
if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
return true, distribution, nil
}
}
}
return false, nil, nil
})

handler.SetTimeout(10 * time.Minute)
return handler
}
Loading
Loading