@@ -13,16 +13,17 @@ import (
13
13
)
14
14
15
15
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"
21
21
)
22
22
23
23
// Interfaces needed for tests
24
24
type APIClientInterface interface {
25
25
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 )
26
27
}
27
28
28
29
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
39
40
}
40
41
if * distribution .Distribution .Id == distributionId {
41
42
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" )
44
51
default :
45
- return false , distribution , err
52
+ return true , nil , fmt . Errorf ( "CDNDistributionWaitHandler: unexpected status %s" , * distribution . Distribution . Status )
46
53
}
47
54
}
48
-
49
55
return false , nil , nil
50
56
})
51
-
52
- handler .SetTimeout (10 * time .Minute )
57
+ handler .SetTimeout (1 * time .Minute )
53
58
return handler
54
59
}
55
60
@@ -67,10 +72,16 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
67
72
}
68
73
if * distribution .Distribution .Id == distributionId {
69
74
switch * distribution .Distribution .Status {
70
- case DistributionStateActive :
75
+ case DistributionStatusActive :
71
76
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" )
72
83
default :
73
- return false , distribution , err
84
+ return true , nil , fmt . Errorf ( "UpdateDistributionWaitHandler: unexpected status %s" , * distribution . Distribution . Status )
74
85
}
75
86
}
76
87
@@ -83,18 +94,68 @@ func UpdateDistributionWaitHandler(ctx context.Context, api APIClientInterface,
83
94
84
95
func DeleteDistributionWaitHandler (ctx context.Context , api APIClientInterface , projectId , distributionId string ) * wait.AsyncActionHandler [cdn.GetDistributionResponse ] {
85
96
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
93
107
}
94
108
}
95
- return false , nil , nil
109
+
110
+ return false , nil , err
96
111
})
112
+ handler .SetTimeout (30 * time .Second )
113
+ return handler
114
+ }
97
115
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 )
99
160
return handler
100
161
}
0 commit comments