Skip to content

Commit 96f6c33

Browse files
add wait handlers for custom domains and distributions (#1823)
* add wait handlers for custom domains and distributions * review comments * changelog --------- Co-authored-by: Malte Ehrlen <malte.ehrlen@freiheit.com>
1 parent b696944 commit 96f6c33

File tree

6 files changed

+702
-277
lines changed

6 files changed

+702
-277
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Release (2025-XX-YY)
2+
- `cdn`: [v0.3.0](services/cdn/CHANGELOG.md#v030-2025-04-04)
3+
- **New:** Add waiter for creation of `CustomDomain`
4+
15
## Release (2025-XX-YY)
26
- `cdn`: [v0.2.0](services/cdn/CHANGELOG.md#v020-2025-04-01)
37
- **API enhancement:** Provide waiter infrastructure

patch

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
diff --git a/CHANGELOG.md b/CHANGELOG.md
2+
index 2c8562e1..649d463b 100644
3+
--- a/CHANGELOG.md
4+
+++ b/CHANGELOG.md
5+
@@ -1,3 +1,7 @@
6+
+## Release (2025-XX-YY)
7+
+ - `cdn`: [v0.3.0](services/cdn/CHANGELOG.md#v030-2025-04-04)
8+
+ - **New:** Add waiter for creation of `CustomDomain`
9+
+
10+
## Release (2025-XX-YY)
11+
- `cdn`: [v0.2.0](services/cdn/CHANGELOG.md#v020-2025-04-01)
12+
- **API enhancement:** Provide waiter infrastructure
13+
diff --git a/services/cdn/CHANGELOG.md b/services/cdn/CHANGELOG.md
14+
index dd315f01..b8e57e59 100644
15+
--- a/services/cdn/CHANGELOG.md
16+
+++ b/services/cdn/CHANGELOG.md
17+
@@ -1,3 +1,6 @@
18+
+## v0.3.0 (2025-04-04)
19+
+- **New:** Add waiter for creation of `CustomDomain`
20+
+
21+
## v0.2.0 (2025-04-01)
22+
- **API enhancement:** Provide waiter infrastructure
23+

services/cdn/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v0.3.0 (2025-04-04)
2+
- **New:** Add waiter for creation of `CustomDomain`
3+
14
## v0.2.0 (2025-04-01)
25
- **API enhancement:** Provide waiter infrastructure
36

services/cdn/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/stackitcloud/stackit-sdk-go/services/cdn
33
go 1.21
44

55
require (
6+
github.com/google/go-cmp v0.7.0
67
github.com/google/uuid v1.6.0
78
github.com/stackitcloud/stackit-sdk-go/core v0.16.2
89
)

services/cdn/wait/wait.go

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ import (
1313
)
1414

1515
const (
16-
DistributionStateCreating = "CREATING"
17-
DistributionStateActive = "ACTIVE"
18-
DistributionStateUpdating = "UPDATING"
19-
DistributionStateDeleting = "DELETING"
20-
DistributionStateError = "ERROR"
16+
DistributionStatusCreating = "CREATING"
17+
DistributionStatusActive = "ACTIVE"
18+
DistributionStatusUpdating = "UPDATING"
19+
DistributionStatusDeleting = "DELETING"
20+
DistributionStatusError = "ERROR"
2121
)
2222

2323
// Interfaces needed for tests
2424
type APIClientInterface interface {
2525
GetDistributionExecute(ctx context.Context, projectId string, distributionId string) (*cdn.GetDistributionResponse, error)
26+
GetCustomDomainExecute(ctx context.Context, projectId string, distributionId string, domain string) (*cdn.GetCustomDomainResponse, error)
2627
}
2728

2829
func CreateDistributionPoolWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
@@ -39,17 +40,21 @@ func CreateDistributionPoolWaitHandler(ctx context.Context, api APIClientInterfa
3940
}
4041
if *distribution.Distribution.Id == distributionId {
4142
switch *distribution.Distribution.Status {
42-
case DistributionStateActive:
43-
return true, distribution, err
43+
case DistributionStatusActive:
44+
return true, distribution, nil
45+
case DistributionStatusCreating, DistributionStatusUpdating:
46+
return false, nil, nil
47+
case DistributionStatusDeleting:
48+
return true, nil, fmt.Errorf("creating CDN distribution failed")
49+
case DistributionStatusError:
50+
return true, nil, fmt.Errorf("creating CDN distribution failed")
4451
default:
45-
return false, distribution, err
52+
return true, nil, fmt.Errorf("CDNDistributionWaitHandler: unexpected status %s", *distribution.Distribution.Status)
4653
}
4754
}
48-
4955
return false, nil, nil
5056
})
51-
52-
handler.SetTimeout(10 * time.Minute)
57+
handler.SetTimeout(1 * time.Minute)
5358
return handler
5459
}
5560

@@ -67,10 +72,16 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
6772
}
6873
if *distribution.Distribution.Id == distributionId {
6974
switch *distribution.Distribution.Status {
70-
case DistributionStateActive:
75+
case DistributionStatusActive:
7176
return true, distribution, err
77+
case DistributionStatusUpdating:
78+
return false, nil, nil
79+
case DistributionStatusDeleting:
80+
return true, nil, fmt.Errorf("updating CDN distribution failed")
81+
case DistributionStatusError:
82+
return true, nil, fmt.Errorf("updating CDN distribution failed")
7283
default:
73-
return false, distribution, err
84+
return true, nil, fmt.Errorf("UpdateDistributionWaitHandler: unexpected status %s", *distribution.Distribution.Status)
7485
}
7586
}
7687

@@ -83,18 +94,68 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
8394

8495
func DeleteDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
8596
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-
}
97+
_, err = api.GetDistributionExecute(ctx, projectId, distributionId)
98+
99+
// the distribution is still gettable, e.g. not deleted
100+
if err == nil {
101+
return false, nil, nil
102+
}
103+
var oapiError *oapierror.GenericOpenAPIError
104+
if errors.As(err, &oapiError) {
105+
if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
106+
return true, nil, nil
93107
}
94108
}
95-
return false, nil, nil
109+
110+
return false, nil, err
96111
})
112+
handler.SetTimeout(30 * time.Second)
113+
return handler
114+
}
97115

98-
handler.SetTimeout(10 * time.Minute)
116+
func CreateCDNCustomDomainWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] {
117+
handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) {
118+
resp, err := a.GetCustomDomainExecute(ctx, projectId, distributionId, domain)
119+
if err != nil {
120+
return false, nil, err
121+
}
122+
if resp == nil || resp.CustomDomain == nil || resp.CustomDomain.Status == nil {
123+
return false, nil, errors.New("CDNDistributionWaitHandler: status or custom domain missing in response")
124+
}
125+
126+
switch *resp.CustomDomain.Status {
127+
case cdn.DOMAINSTATUS_ACTIVE:
128+
return true, resp.CustomDomain, nil
129+
case cdn.DOMAINSTATUS_CREATING, cdn.DOMAINSTATUS_UPDATING:
130+
return false, nil, nil
131+
case cdn.DOMAINSTATUS_DELETING:
132+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
133+
case cdn.DOMAINSTATUS_ERROR:
134+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
135+
default:
136+
return true, nil, fmt.Errorf("CDNCustomDomainWaitHandler: unexpected status %s", *resp.CustomDomain.Status)
137+
}
138+
})
139+
handler.SetTimeout(1 * time.Minute)
140+
return handler
141+
}
142+
143+
func DeleteCDNCustomDomainWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] {
144+
handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) {
145+
_, err = a.GetCustomDomainExecute(ctx, projectId, distributionId, domain)
146+
147+
// the custom domain is still gettable, e.g. not deleted
148+
if err == nil {
149+
return false, nil, nil
150+
}
151+
var oapiError *oapierror.GenericOpenAPIError
152+
if errors.As(err, &oapiError) {
153+
if statusCode := oapiError.StatusCode; statusCode == http.StatusNotFound || statusCode == http.StatusGone {
154+
return true, nil, nil
155+
}
156+
}
157+
return false, nil, err
158+
})
159+
handler.SetTimeout(30 * time.Second)
99160
return handler
100161
}

0 commit comments

Comments
 (0)