Skip to content

Commit bf29127

Browse files
authored
feat(cdn): waiter for CDN package
* feat: added waiter for cdn * feat: adapted changelogs * feat: linter issues
1 parent 5f2f6c1 commit bf29127

File tree

4 files changed

+442
-0
lines changed

4 files changed

+442
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Release (2025-XX-YY)
2+
- `cdn`: [v0.2.0](services/cdn/CHANGELOG.md#v020-2025-04-01)
3+
- **API enhancement:** Provide waiter infrastructure
24

5+
## Release (2025-03-27)
36
- `alb`: [v0.2.1](services/alb/CHANGELOG.md#v021-2025-03-27)
47
- **Bugfix:** Removed ConfigureRegion() from API client
58
- `cdn`: [v0.1.1](services/cdn/CHANGELOG.md#v011-2025-03-27)

services/cdn/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.2.0 (2025-04-01)
2+
- **API enhancement:** Provide waiter infrastructure
3+
14
## v0.1.1 (2025-03-27)
25
- **Bugfix:** Removed ConfigureRegion() from API client
36

services/cdn/wait/wait.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package wait
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
"time"
9+
10+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
11+
"github.com/stackitcloud/stackit-sdk-go/core/wait"
12+
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
13+
)
14+
15+
const (
16+
DistributionStateCreating = "CREATING"
17+
DistributionStateActive = "ACTIVE"
18+
DistributionStateUpdating = "UPDATING"
19+
DistributionStateDeleting = "DELETING"
20+
DistributionStateError = "ERROR"
21+
)
22+
23+
// Interfaces needed for tests
24+
type APIClientInterface interface {
25+
GetDistributionExecute(ctx context.Context, projectId string, distributionId string) (*cdn.GetDistributionResponse, error)
26+
}
27+
28+
func CreateDistributionPoolWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
29+
handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) {
30+
distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId)
31+
if err != nil {
32+
return false, distribution, err
33+
}
34+
if distribution == nil ||
35+
distribution.Distribution == nil ||
36+
distribution.Distribution.Id == nil ||
37+
distribution.Distribution.Status == nil {
38+
return false, distribution, fmt.Errorf("create failed for distribution with id %s, the response is not valid (state missing)", distributionId)
39+
}
40+
if *distribution.Distribution.Id == distributionId {
41+
switch *distribution.Distribution.Status {
42+
case DistributionStateActive:
43+
return true, distribution, err
44+
default:
45+
return false, distribution, err
46+
}
47+
}
48+
49+
return false, nil, nil
50+
})
51+
52+
handler.SetTimeout(10 * time.Minute)
53+
return handler
54+
}
55+
56+
func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
57+
handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) {
58+
distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId)
59+
if err != nil {
60+
return false, distribution, err
61+
}
62+
if distribution == nil ||
63+
distribution.Distribution == nil ||
64+
distribution.Distribution.Id == nil ||
65+
distribution.Distribution.Status == nil {
66+
return false, distribution, fmt.Errorf("update failed for distribution with id %s, the response is not valid (state missing)", distributionId)
67+
}
68+
if *distribution.Distribution.Id == distributionId {
69+
switch *distribution.Distribution.Status {
70+
case DistributionStateActive:
71+
return true, distribution, err
72+
default:
73+
return false, distribution, err
74+
}
75+
}
76+
77+
return false, nil, nil
78+
})
79+
80+
handler.SetTimeout(10 * time.Minute)
81+
return handler
82+
}
83+
84+
func DeleteDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
85+
handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) {
86+
distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId)
87+
if err != nil {
88+
var oapiError *oapierror.GenericOpenAPIError
89+
if errors.As(err, &oapiError) {
90+
if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
91+
return true, distribution, nil
92+
}
93+
}
94+
}
95+
return false, nil, nil
96+
})
97+
98+
handler.SetTimeout(10 * time.Minute)
99+
return handler
100+
}

0 commit comments

Comments
 (0)