|
| 1 | +package dedibox |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/scaleway/scaleway-sdk-go/internal/async" |
| 5 | + "github.com/scaleway/scaleway-sdk-go/internal/errors" |
| 6 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + defaultRetryInterval = time.Second * 15 |
| 12 | + defaultTimeout = time.Minute * 30 |
| 13 | +) |
| 14 | + |
| 15 | +type WaitForServiceRequest struct { |
| 16 | + ServiceID uint64 |
| 17 | + Zone scw.Zone |
| 18 | + Status ServiceProvisioningStatus |
| 19 | + Timeout *time.Duration |
| 20 | + RetryInterval *time.Duration |
| 21 | +} |
| 22 | + |
| 23 | +func (s *API) WaitForService(req *WaitForServiceRequest, opts ...scw.RequestOption) (*Service, error) { |
| 24 | + timeout := defaultTimeout |
| 25 | + if req.Timeout != nil { |
| 26 | + timeout = *req.Timeout |
| 27 | + } |
| 28 | + retryInterval := defaultRetryInterval |
| 29 | + if req.RetryInterval != nil { |
| 30 | + retryInterval = *req.RetryInterval |
| 31 | + } |
| 32 | + terminalStatus := map[ServiceProvisioningStatus]struct{}{ |
| 33 | + ServiceProvisioningStatusReady: {}, |
| 34 | + ServiceProvisioningStatusError: {}, |
| 35 | + ServiceProvisioningStatusExpired: {}, |
| 36 | + } |
| 37 | + service, err := async.WaitSync(&async.WaitSyncConfig{ |
| 38 | + Get: func() (interface{}, bool, error) { |
| 39 | + service, err := s.GetService(&GetServiceRequest{ |
| 40 | + Zone: req.Zone, |
| 41 | + ServiceID: req.ServiceID, |
| 42 | + }, opts...) |
| 43 | + if err != nil { |
| 44 | + return nil, false, err |
| 45 | + } |
| 46 | + _, isTerminal := terminalStatus[service.ProvisioningStatus] |
| 47 | + return service, isTerminal, nil |
| 48 | + }, |
| 49 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 50 | + Timeout: timeout, |
| 51 | + }) |
| 52 | + if err != nil { |
| 53 | + return nil, errors.Wrap(err, "waiting for service failed") |
| 54 | + } |
| 55 | + return service.(*Service), nil |
| 56 | +} |
| 57 | + |
| 58 | +type WaitForServerRequest struct { |
| 59 | + ServerID uint64 |
| 60 | + Zone scw.Zone |
| 61 | + Status ServerStatus |
| 62 | + Timeout *time.Duration |
| 63 | + RetryInterval *time.Duration |
| 64 | +} |
| 65 | + |
| 66 | +func (s *API) WaitForServer(req *WaitForServerRequest, opts ...scw.RequestOption) (*Server, error) { |
| 67 | + timeout := defaultTimeout |
| 68 | + if req.Timeout != nil { |
| 69 | + timeout = *req.Timeout |
| 70 | + } |
| 71 | + retryInterval := defaultRetryInterval |
| 72 | + if req.RetryInterval != nil { |
| 73 | + retryInterval = *req.RetryInterval |
| 74 | + } |
| 75 | + terminalStatus := map[ServerStatus]struct{}{ |
| 76 | + ServerStatusReady: {}, |
| 77 | + ServerStatusError: {}, |
| 78 | + ServerStatusLocked: {}, |
| 79 | + } |
| 80 | + server, err := async.WaitSync(&async.WaitSyncConfig{ |
| 81 | + Get: func() (interface{}, bool, error) { |
| 82 | + server, err := s.GetServer(&GetServerRequest{ |
| 83 | + Zone: req.Zone, |
| 84 | + ServerID: req.ServerID, |
| 85 | + }, opts...) |
| 86 | + if err != nil { |
| 87 | + return nil, false, err |
| 88 | + } |
| 89 | + _, isTerminal := terminalStatus[server.Status] |
| 90 | + return server, isTerminal, nil |
| 91 | + }, |
| 92 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 93 | + Timeout: timeout, |
| 94 | + }) |
| 95 | + if err != nil { |
| 96 | + return nil, errors.Wrap(err, "waiting for server failed") |
| 97 | + } |
| 98 | + return server.(*Server), nil |
| 99 | +} |
0 commit comments