|
1 | 1 | package domain |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "time" |
5 | 6 |
|
6 | 7 | "github.com/scaleway/scaleway-sdk-go/errors" |
@@ -135,3 +136,168 @@ func (s *API) WaitForDNSRecordExist( |
135 | 136 |
|
136 | 137 | return dns.(*Record), nil |
137 | 138 | } |
| 139 | + |
| 140 | +// WaitForOrderDomainRequest is used by WaitForOrderDomain method. |
| 141 | +type WaitForOrderDomainRequest struct { |
| 142 | + Domain string |
| 143 | + Timeout *time.Duration |
| 144 | + RetryInterval *time.Duration |
| 145 | +} |
| 146 | + |
| 147 | +// WaitForOrderDomain waits until the domain reaches a terminal status. |
| 148 | +func (s *RegistrarAPI) WaitForOrderDomain( |
| 149 | + req *WaitForOrderDomainRequest, |
| 150 | + opts ...scw.RequestOption, |
| 151 | +) (*Domain, error) { |
| 152 | + timeout := defaultTimeout |
| 153 | + if req.Timeout != nil { |
| 154 | + timeout = *req.Timeout |
| 155 | + } |
| 156 | + retryInterval := defaultRetryInterval |
| 157 | + if req.RetryInterval != nil { |
| 158 | + retryInterval = *req.RetryInterval |
| 159 | + } |
| 160 | + |
| 161 | + // Terminal statuses indicating success or error. |
| 162 | + terminalStatuses := map[DomainStatus]struct{}{ |
| 163 | + DomainStatusActive: {}, |
| 164 | + DomainStatusExpired: {}, |
| 165 | + DomainStatusLocked: {}, |
| 166 | + DomainStatusCreateError: {}, |
| 167 | + DomainStatusRenewError: {}, |
| 168 | + DomainStatusXferError: {}, |
| 169 | + } |
| 170 | + |
| 171 | + var lastStatus DomainStatus |
| 172 | + |
| 173 | + domain, err := async.WaitSync(&async.WaitSyncConfig{ |
| 174 | + Get: func() (interface{}, bool, error) { |
| 175 | + resp, err := s.GetDomain(&RegistrarAPIGetDomainRequest{ |
| 176 | + Domain: req.Domain, |
| 177 | + }, opts...) |
| 178 | + if err != nil { |
| 179 | + return nil, false, err |
| 180 | + } |
| 181 | + |
| 182 | + lastStatus = resp.Status |
| 183 | + |
| 184 | + if _, isTerminal := terminalStatuses[resp.Status]; isTerminal { |
| 185 | + return resp, true, nil |
| 186 | + } |
| 187 | + return resp, false, nil |
| 188 | + }, |
| 189 | + Timeout: timeout, |
| 190 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 191 | + }) |
| 192 | + if err != nil { |
| 193 | + return nil, errors.Wrap(err, fmt.Sprintf("waiting for domain %s failed, last known status: %s", req.Domain, lastStatus)) |
| 194 | + } |
| 195 | + |
| 196 | + return domain.(*Domain), nil |
| 197 | +} |
| 198 | + |
| 199 | +// WaitForAutoRenewStatusRequest defines the parameters for waiting on the auto‑renew feature. |
| 200 | +type WaitForAutoRenewStatusRequest struct { |
| 201 | + Domain string // The domain to wait for. |
| 202 | + Timeout *time.Duration // Optional timeout. |
| 203 | + RetryInterval *time.Duration // Optional retry interval. |
| 204 | +} |
| 205 | + |
| 206 | +// WaitForAutoRenewStatus polls the domain until its auto‑renew feature reaches a terminal state |
| 207 | +// (either "enabled" or "disabled"). It uses GetDomain() to fetch the current status. |
| 208 | +func (s *RegistrarAPI) WaitForAutoRenewStatus(req *WaitForAutoRenewStatusRequest, opts ...scw.RequestOption) (*Domain, error) { |
| 209 | + // Use default timeout and retry interval if not provided. |
| 210 | + timeout := defaultTimeout |
| 211 | + if req.Timeout != nil { |
| 212 | + timeout = *req.Timeout |
| 213 | + } |
| 214 | + retryInterval := defaultRetryInterval |
| 215 | + if req.RetryInterval != nil { |
| 216 | + retryInterval = *req.RetryInterval |
| 217 | + } |
| 218 | + |
| 219 | + // Terminal statuses for auto_renew: enabled or disabled. |
| 220 | + terminalStatuses := map[DomainFeatureStatus]struct{}{ |
| 221 | + DomainFeatureStatusEnabled: {}, |
| 222 | + DomainFeatureStatusDisabled: {}, |
| 223 | + } |
| 224 | + |
| 225 | + var lastStatus DomainFeatureStatus |
| 226 | + |
| 227 | + domainResult, err := async.WaitSync(&async.WaitSyncConfig{ |
| 228 | + Get: func() (interface{}, bool, error) { |
| 229 | + resp, err := s.GetDomain(&RegistrarAPIGetDomainRequest{ |
| 230 | + Domain: req.Domain, |
| 231 | + }, opts...) |
| 232 | + if err != nil { |
| 233 | + return nil, false, err |
| 234 | + } |
| 235 | + |
| 236 | + lastStatus = resp.AutoRenewStatus |
| 237 | + if _, isTerminal := terminalStatuses[resp.AutoRenewStatus]; isTerminal { |
| 238 | + return resp, true, nil |
| 239 | + } |
| 240 | + return resp, false, nil |
| 241 | + }, |
| 242 | + Timeout: timeout, |
| 243 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 244 | + }) |
| 245 | + if err != nil { |
| 246 | + return nil, errors.Wrap(err, fmt.Sprintf("waiting for auto_renew to reach a terminal state for domain %s failed, last known status: %s", req.Domain, lastStatus)) |
| 247 | + } |
| 248 | + return domainResult.(*Domain), nil |
| 249 | +} |
| 250 | + |
| 251 | +// WaitForDNSSECStatusRequest defines the parameters for waiting on the DNSSEC feature. |
| 252 | +type WaitForDNSSECStatusRequest struct { |
| 253 | + Domain string // The domain to wait for. |
| 254 | + Timeout *time.Duration // Optional timeout. |
| 255 | + RetryInterval *time.Duration // Optional retry interval. |
| 256 | +} |
| 257 | + |
| 258 | +// WaitForDNSSECStatus polls the domain until its DNSSEC feature reaches a terminal state |
| 259 | +// (either "enabled" or "disabled"). It uses GetDomain() to fetch the current status. |
| 260 | +func (s *RegistrarAPI) WaitForDNSSECStatus(req *WaitForDNSSECStatusRequest, opts ...scw.RequestOption) (*Domain, error) { |
| 261 | + // Use default timeout and retry interval if not provided. |
| 262 | + timeout := defaultTimeout |
| 263 | + if req.Timeout != nil { |
| 264 | + timeout = *req.Timeout |
| 265 | + } |
| 266 | + retryInterval := defaultRetryInterval |
| 267 | + if req.RetryInterval != nil { |
| 268 | + retryInterval = *req.RetryInterval |
| 269 | + } |
| 270 | + |
| 271 | + // Terminal statuses for DNSSEC: enabled or disabled. |
| 272 | + terminalStatuses := map[DomainFeatureStatus]struct{}{ |
| 273 | + DomainFeatureStatusEnabled: {}, |
| 274 | + DomainFeatureStatusDisabled: {}, |
| 275 | + } |
| 276 | + |
| 277 | + var lastStatus DomainFeatureStatus |
| 278 | + |
| 279 | + domainResult, err := async.WaitSync(&async.WaitSyncConfig{ |
| 280 | + Get: func() (interface{}, bool, error) { |
| 281 | + // Retrieve the domain. |
| 282 | + resp, err := s.GetDomain(&RegistrarAPIGetDomainRequest{ |
| 283 | + Domain: req.Domain, |
| 284 | + }, opts...) |
| 285 | + if err != nil { |
| 286 | + return nil, false, err |
| 287 | + } |
| 288 | + |
| 289 | + // Check the current DNSSEC status. |
| 290 | + lastStatus = resp.Dnssec.Status |
| 291 | + if _, isTerminal := terminalStatuses[resp.Dnssec.Status]; isTerminal { |
| 292 | + return resp, true, nil |
| 293 | + } |
| 294 | + return resp, false, nil |
| 295 | + }, |
| 296 | + Timeout: timeout, |
| 297 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 298 | + }) |
| 299 | + if err != nil { |
| 300 | + return nil, errors.Wrap(err, fmt.Sprintf("waiting for dnssec to reach a terminal state for domain %s failed, last known status: %s", req.Domain, lastStatus)) |
| 301 | + } |
| 302 | + return domainResult.(*Domain), nil |
| 303 | +} |
0 commit comments