Skip to content

Commit a9586cc

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

File tree

2 files changed

+631
-206
lines changed

2 files changed

+631
-206
lines changed

services/cdn/wait/wait.go

Lines changed: 75 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] {
@@ -38,18 +39,25 @@ func CreateDistributionPoolWaitHandler(ctx context.Context, api APIClientInterfa
3839
return false, distribution, fmt.Errorf("create failed for distribution with id %s, the response is not valid (state missing)", distributionId)
3940
}
4041
if *distribution.Distribution.Id == distributionId {
42+
4143
switch *distribution.Distribution.Status {
42-
case DistributionStateActive:
43-
return true, distribution, err
44+
case DistributionStatusActive:
45+
return true, &cdn.GetDistributionResponse{
46+
Distribution: distribution.Distribution,
47+
}, nil
48+
case DistributionStatusCreating, DistributionStatusUpdating:
49+
return false, nil, nil
50+
case DistributionStatusDeleting:
51+
return true, nil, fmt.Errorf("creating CDN distribution failed")
52+
case DistributionStatusError:
53+
return true, nil, fmt.Errorf("creating CDN distribution failed")
4454
default:
45-
return false, distribution, err
55+
return true, nil, fmt.Errorf("CDNDistributionWaitHandler: unexpected status %s", *distribution.Distribution.Status)
4656
}
4757
}
48-
4958
return false, nil, nil
5059
})
51-
52-
handler.SetTimeout(10 * time.Minute)
60+
handler.SetTimeout(1 * time.Minute)
5361
return handler
5462
}
5563

@@ -67,7 +75,7 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
6775
}
6876
if *distribution.Distribution.Id == distributionId {
6977
switch *distribution.Distribution.Status {
70-
case DistributionStateActive:
78+
case DistributionStatusActive:
7179
return true, distribution, err
7280
default:
7381
return false, distribution, err
@@ -82,19 +90,64 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
8290
}
8391

8492
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)
93+
handler := wait.New(func() (waitFinished bool, response *cdn.GetDistributionResponse, err error) {
94+
_, err = api.GetDistributionExecute(ctx, projectId, distributionId)
95+
96+
// the distribution is still gettable, e.g. not deleted
97+
if err == nil {
98+
return false, nil, nil
99+
}
100+
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
101+
if ok && oapiErr.StatusCode == http.StatusNotFound {
102+
return true, nil, nil
103+
}
104+
return false, nil, err
105+
})
106+
handler.SetTimeout(30 * time.Second)
107+
return handler
108+
}
109+
110+
func CreateCDNCustomDomainWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] {
111+
handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) {
112+
resp, err := a.GetCustomDomainExecute(ctx, projectId, distributionId, domain)
87113
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-
}
114+
return false, nil, err
115+
}
116+
if resp == nil || resp.CustomDomain == nil || resp.CustomDomain.Status == nil {
117+
return false, nil, errors.New("CDNDistributionWaitHandler: status or custom domain missing in response")
118+
}
119+
120+
switch *resp.CustomDomain.Status {
121+
case cdn.DOMAINSTATUS_ACTIVE:
122+
return true, resp.CustomDomain, nil
123+
case cdn.DOMAINSTATUS_CREATING, cdn.DOMAINSTATUS_UPDATING:
124+
return false, nil, nil
125+
case cdn.DOMAINSTATUS_DELETING:
126+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
127+
case cdn.DOMAINSTATUS_ERROR:
128+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
129+
default:
130+
return true, nil, fmt.Errorf("CDNCustomDomainWaitHandler: unexpected status %s", *resp.CustomDomain.Status)
94131
}
95-
return false, nil, nil
96132
})
133+
handler.SetTimeout(1 * time.Minute)
134+
return handler
135+
}
97136

98-
handler.SetTimeout(10 * time.Minute)
137+
func DeleteCDNCustomDomainWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] {
138+
handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) {
139+
_, err = a.GetCustomDomainExecute(ctx, projectId, distributionId, domain)
140+
141+
// the custom domain is still gettable, e.g. not deleted
142+
if err == nil {
143+
return false, nil, nil
144+
}
145+
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
146+
if ok && oapiErr.StatusCode == http.StatusNotFound {
147+
return true, nil, nil
148+
}
149+
return false, nil, err
150+
})
151+
handler.SetTimeout(30 * time.Second)
99152
return handler
100153
}

0 commit comments

Comments
 (0)