@@ -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 ] {
@@ -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
8495func 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