@@ -13,16 +13,17 @@ import (
1313)
1414
1515const (
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
2424type 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
2829func CreateDistributionPoolWaitHandler (ctx context.Context , api APIClientInterface , projectId , distributionId string ) * wait.AsyncActionHandler [cdn.GetDistributionResponse ] {
@@ -38,18 +39,23 @@ 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 , distribution , nil
46+ case DistributionStatusCreating , DistributionStatusUpdating :
47+ return false , nil , nil
48+ case DistributionStatusDeleting :
49+ return true , nil , fmt .Errorf ("creating CDN distribution failed" )
50+ case DistributionStatusError :
51+ return true , nil , fmt .Errorf ("creating CDN distribution failed" )
4452 default :
45- return false , distribution , err
53+ return true , nil , fmt . Errorf ( "CDNDistributionWaitHandler: unexpected status %s" , * distribution . Distribution . Status )
4654 }
4755 }
48-
4956 return false , nil , nil
5057 })
51-
52- handler .SetTimeout (10 * time .Minute )
58+ handler .SetTimeout (1 * time .Minute )
5359 return handler
5460}
5561
@@ -67,7 +73,7 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
6773 }
6874 if * distribution .Distribution .Id == distributionId {
6975 switch * distribution .Distribution .Status {
70- case DistributionStateActive :
76+ case DistributionStatusActive :
7177 return true , distribution , err
7278 default :
7379 return false , distribution , err
@@ -82,19 +88,64 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
8288}
8389
8490func 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 )
91+ handler := wait .New (func () (waitFinished bool , response * cdn.GetDistributionResponse , err error ) {
92+ _ , err = api .GetDistributionExecute (ctx , projectId , distributionId )
93+
94+ // the distribution is still gettable, e.g. not deleted
95+ if err == nil {
96+ return false , nil , nil
97+ }
98+ 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
99+ if ok && oapiErr .StatusCode == http .StatusNotFound {
100+ return true , nil , nil
101+ }
102+ return false , nil , err
103+ })
104+ handler .SetTimeout (30 * time .Second )
105+ return handler
106+ }
107+
108+ func CreateCDNCustomDomainWaitHandler (ctx context.Context , a APIClientInterface , projectId , distributionId , domain string ) * wait.AsyncActionHandler [cdn.CustomDomain ] {
109+ handler := wait .New (func () (waitFinished bool , response * cdn.CustomDomain , err error ) {
110+ resp , err := a .GetCustomDomainExecute (ctx , projectId , distributionId , domain )
87111 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- }
112+ return false , nil , err
113+ }
114+ if resp == nil || resp .CustomDomain == nil || resp .CustomDomain .Status == nil {
115+ return false , nil , errors .New ("CDNDistributionWaitHandler: status or custom domain missing in response" )
116+ }
117+
118+ switch * resp .CustomDomain .Status {
119+ case cdn .DOMAINSTATUS_ACTIVE :
120+ return true , resp .CustomDomain , nil
121+ case cdn .DOMAINSTATUS_CREATING , cdn .DOMAINSTATUS_UPDATING :
122+ return false , nil , nil
123+ case cdn .DOMAINSTATUS_DELETING :
124+ return true , nil , fmt .Errorf ("creating CDN custom domain failed" )
125+ case cdn .DOMAINSTATUS_ERROR :
126+ return true , nil , fmt .Errorf ("creating CDN custom domain failed" )
127+ default :
128+ return true , nil , fmt .Errorf ("CDNCustomDomainWaitHandler: unexpected status %s" , * resp .CustomDomain .Status )
94129 }
95- return false , nil , nil
96130 })
131+ handler .SetTimeout (1 * time .Minute )
132+ return handler
133+ }
97134
98- handler .SetTimeout (10 * time .Minute )
135+ func DeleteCDNCustomDomainWaitHandler (ctx context.Context , a APIClientInterface , projectId , distributionId , domain string ) * wait.AsyncActionHandler [cdn.CustomDomain ] {
136+ handler := wait .New (func () (waitFinished bool , response * cdn.CustomDomain , err error ) {
137+ _ , err = a .GetCustomDomainExecute (ctx , projectId , distributionId , domain )
138+
139+ // the custom domain is still gettable, e.g. not deleted
140+ if err == nil {
141+ return false , nil , nil
142+ }
143+ 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
144+ if ok && oapiErr .StatusCode == http .StatusNotFound {
145+ return true , nil , nil
146+ }
147+ return false , nil , err
148+ })
149+ handler .SetTimeout (30 * time .Second )
99150 return handler
100151}
0 commit comments