Skip to content

Commit 9fde91d

Browse files
committed
add wait handlers for custom domains and distributions
1 parent bfdc815 commit 9fde91d

File tree

2 files changed

+553
-319
lines changed

2 files changed

+553
-319
lines changed

services/cdn/wait/wait.go

Lines changed: 79 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,89 +12,107 @@ import (
1212
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
1313
)
1414

15-
const (
16-
DistributionStateCreating = "CREATING"
17-
DistributionStateActive = "ACTIVE"
18-
DistributionStateUpdating = "UPDATING"
19-
DistributionStateDeleting = "DELETING"
20-
DistributionStateError = "ERROR"
21-
)
22-
23-
// Interfaces needed for tests
2415
type APIClientInterface interface {
2516
GetDistributionExecute(ctx context.Context, projectId string, distributionId string) (*cdn.GetDistributionResponse, error)
17+
GetCustomDomainExecute(ctx context.Context, projectId string, distributionId string, domain string) (*cdn.GetCustomDomainResponse, error)
2618
}
2719

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)
20+
const (
21+
DistributionStatusActive = "ACTIVE"
22+
DistributionStatusUpdating = "UPDATING"
23+
DistributionStatusCreating = "CREATING"
24+
DistributionStatusDeleting = "DELETING"
25+
DistributionStatusError = "ERROR"
26+
)
27+
28+
var _ APIClientInterface = &cdn.APIClient{}
29+
30+
func CreateCDNDistributionWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.Distribution] {
31+
handler := wait.New(func() (waitFinished bool, response *cdn.Distribution, err error) {
32+
resp, err := a.GetDistributionExecute(ctx, projectId, distributionId)
3133
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)
34+
return false, nil, err
3935
}
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-
}
36+
if resp == nil || resp.Distribution == nil || resp.Distribution.Status == nil {
37+
return false, nil, errors.New("CDNDistributionWaitHandler: status or distribution missing in response")
4738
}
4839

49-
return false, nil, nil
40+
switch *resp.Distribution.Status {
41+
case DistributionStatusActive:
42+
return true, resp.Distribution, nil
43+
case DistributionStatusCreating, DistributionStatusUpdating:
44+
return false, nil, nil
45+
case DistributionStatusDeleting:
46+
return true, nil, fmt.Errorf("creating CDN distribution failed")
47+
case DistributionStatusError:
48+
return true, nil, fmt.Errorf("creating CDN distribution failed")
49+
default:
50+
return true, nil, fmt.Errorf("CDNDistributionWaitHandler: unexpected status %s", *resp.Distribution.Status)
51+
}
5052
})
53+
handler.SetTimeout(1 * time.Minute)
54+
return handler
55+
}
5156

52-
handler.SetTimeout(10 * time.Minute)
57+
func DeleteCDNDistributionWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.Distribution] {
58+
handler := wait.New(func() (waitFinished bool, response *cdn.Distribution, err error) {
59+
_, err = a.GetDistributionExecute(ctx, projectId, distributionId)
60+
61+
// the distribution is still gettable, e.g. not deleted
62+
if err == nil {
63+
return false, nil, nil
64+
}
65+
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
66+
if ok && oapiErr.StatusCode == http.StatusNotFound {
67+
return true, nil, nil
68+
}
69+
return false, nil, err
70+
})
71+
handler.SetTimeout(30 * time.Second)
5372
return handler
5473
}
5574

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)
75+
func CreateCDNCustomDomainWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] {
76+
handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) {
77+
resp, err := a.GetCustomDomainExecute(ctx, projectId, distributionId, domain)
5978
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)
79+
return false, nil, err
6780
}
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-
}
81+
if resp == nil || resp.CustomDomain == nil || resp.CustomDomain.Status == nil {
82+
return false, nil, errors.New("CDNDistributionWaitHandler: status or custom domain missing in response")
7583
}
7684

77-
return false, nil, nil
85+
switch *resp.CustomDomain.Status {
86+
case cdn.DOMAINSTATUS_ACTIVE:
87+
return true, resp.CustomDomain, nil
88+
case cdn.DOMAINSTATUS_CREATING, cdn.DOMAINSTATUS_UPDATING:
89+
return false, nil, nil
90+
case cdn.DOMAINSTATUS_DELETING:
91+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
92+
case cdn.DOMAINSTATUS_ERROR:
93+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
94+
default:
95+
return true, nil, fmt.Errorf("CDNCustomDomainWaitHandler: unexpected status %s", *resp.CustomDomain.Status)
96+
}
7897
})
79-
80-
handler.SetTimeout(10 * time.Minute)
98+
handler.SetTimeout(1 * time.Minute)
8199
return handler
82100
}
83101

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-
}
102+
func DeleteCDNCustomDomainWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] {
103+
handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) {
104+
_, err = a.GetCustomDomainExecute(ctx, projectId, distributionId, domain)
105+
106+
// the custom domain is still gettable, e.g. not deleted
107+
if err == nil {
108+
return false, nil, nil
94109
}
95-
return false, nil, nil
110+
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
111+
if ok && oapiErr.StatusCode == http.StatusNotFound {
112+
return true, nil, nil
113+
}
114+
return false, nil, err
96115
})
97-
98-
handler.SetTimeout(10 * time.Minute)
116+
handler.SetTimeout(30 * time.Second)
99117
return handler
100118
}

0 commit comments

Comments
 (0)