diff --git a/api/registry/v1/registry_utils.go b/api/registry/v1/registry_utils.go new file mode 100644 index 000000000..6cf2b3674 --- /dev/null +++ b/api/registry/v1/registry_utils.go @@ -0,0 +1,49 @@ +package registry + +import ( + "time" + + "github.com/scaleway/scaleway-sdk-go/internal/async" + "github.com/scaleway/scaleway-sdk-go/internal/errors" + "github.com/scaleway/scaleway-sdk-go/scw" +) + +// WaitForNamespaceRequest is used by WaitForNamespace method +type WaitForNamespaceRequest struct { + NamespaceID string + Region scw.Region + Timeout time.Duration +} + +// WaitForNamespace wait for the namespace to be in a "terminal state" before returning. +// This function can be used to wait for a namespace to be ready for example. +func (s *API) WaitForNamespace(req *WaitForNamespaceRequest) (*Namespace, scw.SdkError) { + terminalStatus := map[NamespaceStatus]struct{}{ + NamespaceStatusReady: {}, + NamespaceStatusLocked: {}, + NamespaceStatusError: {}, + NamespaceStatusUnknown: {}, + } + + namespace, err := async.WaitSync(&async.WaitSyncConfig{ + Get: func() (interface{}, error, bool) { + ns, err := s.GetNamespace(&GetNamespaceRequest{ + Region: req.Region, + NamespaceID: req.NamespaceID, + }) + if err != nil { + return nil, err, false + } + + _, isTerminal := terminalStatus[ns.Status] + + return ns, err, isTerminal + }, + Timeout: req.Timeout, + IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), + }) + if err != nil { + return nil, errors.Wrap(err, "waiting for namespace failed") + } + return namespace.(*Namespace), nil +}