|
| 1 | +package vpcgw |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/scaleway/scaleway-sdk-go/errors" |
| 7 | + "github.com/scaleway/scaleway-sdk-go/internal/async" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + defaultTimeout = 5 * time.Minute |
| 13 | + defaultRetryInterval = 15 * time.Second |
| 14 | +) |
| 15 | + |
| 16 | +// WaitForGatewayRequest is used by WaitForGateway method |
| 17 | +type WaitForGatewayRequest struct { |
| 18 | + GatewayID string |
| 19 | + Zone scw.Zone |
| 20 | + Timeout *time.Duration |
| 21 | + RetryInterval *time.Duration |
| 22 | +} |
| 23 | + |
| 24 | +// WaitForGateway waits for the gateway to be in a "terminal state" before returning. |
| 25 | +// This function can be used to wait for a gateway to be ready for example. |
| 26 | +func (s *API) WaitForGateway(req *WaitForGatewayRequest, opts ...scw.RequestOption) (*Gateway, error) { |
| 27 | + timeout := defaultTimeout |
| 28 | + if req.Timeout != nil { |
| 29 | + timeout = *req.Timeout |
| 30 | + } |
| 31 | + retryInterval := defaultRetryInterval |
| 32 | + if req.RetryInterval != nil { |
| 33 | + retryInterval = *req.RetryInterval |
| 34 | + } |
| 35 | + |
| 36 | + terminalStatus := map[GatewayStatus]struct{}{ |
| 37 | + GatewayStatusUnknownStatus: {}, |
| 38 | + GatewayStatusStopped: {}, |
| 39 | + GatewayStatusRunning: {}, |
| 40 | + GatewayStatusFailed: {}, |
| 41 | + GatewayStatusLocked: {}, |
| 42 | + } |
| 43 | + |
| 44 | + gateway, err := async.WaitSync(&async.WaitSyncConfig{ |
| 45 | + Get: func() (interface{}, bool, error) { |
| 46 | + ns, err := s.GetGateway(&GetGatewayRequest{ |
| 47 | + Zone: req.Zone, |
| 48 | + GatewayID: req.GatewayID, |
| 49 | + }, opts...) |
| 50 | + if err != nil { |
| 51 | + return nil, false, err |
| 52 | + } |
| 53 | + |
| 54 | + _, isTerminal := terminalStatus[ns.Status] |
| 55 | + |
| 56 | + return ns, isTerminal, err |
| 57 | + }, |
| 58 | + Timeout: timeout, |
| 59 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + return nil, errors.Wrap(err, "waiting for gateway failed") |
| 63 | + } |
| 64 | + |
| 65 | + return gateway.(*Gateway), nil |
| 66 | +} |
| 67 | + |
| 68 | +// WaitForGatewayNetworkRequest is used by WaitForGatewayNetwork method |
| 69 | +type WaitForGatewayNetworkRequest struct { |
| 70 | + GatewayNetworkID string |
| 71 | + Zone scw.Zone |
| 72 | + Timeout *time.Duration |
| 73 | + RetryInterval *time.Duration |
| 74 | +} |
| 75 | + |
| 76 | +// WaitForGatewayNetwork waits for the gateway network to be in a "terminal state" before returning. |
| 77 | +// This function can be used to wait for a gateway network to be ready for example. |
| 78 | +func (s *API) WaitForGatewayNetwork(req *WaitForGatewayNetworkRequest, opts ...scw.RequestOption) (*GatewayNetwork, error) { |
| 79 | + timeout := defaultTimeout |
| 80 | + if req.Timeout != nil { |
| 81 | + timeout = *req.Timeout |
| 82 | + } |
| 83 | + retryInterval := defaultRetryInterval |
| 84 | + if req.RetryInterval != nil { |
| 85 | + retryInterval = *req.RetryInterval |
| 86 | + } |
| 87 | + |
| 88 | + terminalStatus := map[GatewayNetworkStatus]struct{}{ |
| 89 | + GatewayNetworkStatusReady: {}, |
| 90 | + GatewayNetworkStatusUnknownStatus: {}, |
| 91 | + } |
| 92 | + |
| 93 | + gatewayNetwork, err := async.WaitSync(&async.WaitSyncConfig{ |
| 94 | + Get: func() (interface{}, bool, error) { |
| 95 | + ns, err := s.GetGatewayNetwork(&GetGatewayNetworkRequest{ |
| 96 | + Zone: req.Zone, |
| 97 | + GatewayNetworkID: req.GatewayNetworkID, |
| 98 | + }, opts...) |
| 99 | + if err != nil { |
| 100 | + return nil, false, err |
| 101 | + } |
| 102 | + |
| 103 | + _, isTerminal := terminalStatus[ns.Status] |
| 104 | + |
| 105 | + return ns, isTerminal, err |
| 106 | + }, |
| 107 | + Timeout: timeout, |
| 108 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 109 | + }) |
| 110 | + if err != nil { |
| 111 | + return nil, errors.Wrap(err, "waiting for gateway network failed") |
| 112 | + } |
| 113 | + |
| 114 | + return gatewayNetwork.(*GatewayNetwork), nil |
| 115 | +} |
0 commit comments