Skip to content

Commit b4f5448

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

File tree

2 files changed

+626
-204
lines changed

2 files changed

+626
-204
lines changed

services/cdn/wait/wait.go

Lines changed: 70 additions & 20 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,7 +72,7 @@ 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
7277
default:
7378
return false, distribution, err
@@ -84,17 +89,62 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
8489
func DeleteDistributionWaitHandler(ctx context.Context, api APIClientInterface, projectId, distributionId string) *wait.AsyncActionHandler[cdn.GetDistributionResponse] {
8590
handler := wait.New(func() (waitFinished bool, distribution *cdn.GetDistributionResponse, err error) {
8691
distribution, err = api.GetDistributionExecute(ctx, projectId, distributionId)
92+
93+
// the distribution is still gettable, e.g. not deleted
94+
if err == nil {
95+
return false, nil, nil
96+
}
97+
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
98+
if ok && oapiErr.StatusCode == http.StatusNotFound {
99+
return true, nil, nil
100+
}
101+
return false, nil, err
102+
})
103+
handler.SetTimeout(30 * time.Second)
104+
return handler
105+
}
106+
107+
func CreateCDNCustomDomainWaitHandler(ctx context.Context, a APIClientInterface, projectId, distributionId, domain string) *wait.AsyncActionHandler[cdn.CustomDomain] {
108+
handler := wait.New(func() (waitFinished bool, response *cdn.CustomDomain, err error) {
109+
resp, err := a.GetCustomDomainExecute(ctx, projectId, distributionId, domain)
87110
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-
}
111+
return false, nil, err
112+
}
113+
if resp == nil || resp.CustomDomain == nil || resp.CustomDomain.Status == nil {
114+
return false, nil, errors.New("CDNDistributionWaitHandler: status or custom domain missing in response")
115+
}
116+
117+
switch *resp.CustomDomain.Status {
118+
case cdn.DOMAINSTATUS_ACTIVE:
119+
return true, resp.CustomDomain, nil
120+
case cdn.DOMAINSTATUS_CREATING, cdn.DOMAINSTATUS_UPDATING:
121+
return false, nil, nil
122+
case cdn.DOMAINSTATUS_DELETING:
123+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
124+
case cdn.DOMAINSTATUS_ERROR:
125+
return true, nil, fmt.Errorf("creating CDN custom domain failed")
126+
default:
127+
return true, nil, fmt.Errorf("CDNCustomDomainWaitHandler: unexpected status %s", *resp.CustomDomain.Status)
94128
}
95-
return false, nil, nil
96129
})
130+
handler.SetTimeout(1 * time.Minute)
131+
return handler
132+
}
97133

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

0 commit comments

Comments
 (0)