|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/scaleway/scaleway-sdk-go/internal/async" |
| 7 | + "github.com/scaleway/scaleway-sdk-go/internal/errors" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 9 | +) |
| 10 | + |
| 11 | +// WaitForNamespaceRequest is used by WaitForNamespace method |
| 12 | +type WaitForNamespaceRequest struct { |
| 13 | + NamespaceID string |
| 14 | + Region scw.Region |
| 15 | + Timeout time.Duration |
| 16 | +} |
| 17 | + |
| 18 | +// WaitForNamespace wait for the namespace to be in a "terminal state" before returning. |
| 19 | +// This function can be used to wait for a namespace to be ready for example. |
| 20 | +func (s *API) WaitForNamespace(req *WaitForNamespaceRequest) (*Namespace, scw.SdkError) { |
| 21 | + terminalStatus := map[NamespaceStatus]struct{}{ |
| 22 | + NamespaceStatusReady: {}, |
| 23 | + NamespaceStatusLocked: {}, |
| 24 | + NamespaceStatusError: {}, |
| 25 | + NamespaceStatusUnknown: {}, |
| 26 | + } |
| 27 | + |
| 28 | + namespace, err := async.WaitSync(&async.WaitSyncConfig{ |
| 29 | + Get: func() (interface{}, error, bool) { |
| 30 | + ns, err := s.GetNamespace(&GetNamespaceRequest{ |
| 31 | + Region: req.Region, |
| 32 | + NamespaceID: req.NamespaceID, |
| 33 | + }) |
| 34 | + if err != nil { |
| 35 | + return nil, err, false |
| 36 | + } |
| 37 | + |
| 38 | + _, isTerminal := terminalStatus[ns.Status] |
| 39 | + |
| 40 | + return ns, err, isTerminal |
| 41 | + }, |
| 42 | + Timeout: req.Timeout, |
| 43 | + IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + return nil, errors.Wrap(err, "waiting for namespace failed") |
| 47 | + } |
| 48 | + return namespace.(*Namespace), nil |
| 49 | +} |
0 commit comments