@@ -207,3 +207,58 @@ func (s *API) WaitForServerOptions(req *WaitForServerOptionsRequest, opts ...scw
207207
208208 return server .(* Server ), nil
209209}
210+
211+ // WaitForServerPrivateNetworksRequest is used by WaitForServerPrivateNetworks method.
212+ type WaitForServerPrivateNetworksRequest struct {
213+ ServerID string
214+ Zone scw.Zone
215+ Timeout * time.Duration
216+ RetryInterval * time.Duration
217+ }
218+
219+ // WaitForServerPrivateNetworks wait for all server private networks to be in a "terminal state" before returning.
220+ // This function can be used to wait for all server private networks to be set.
221+ func (s * PrivateNetworkAPI ) WaitForServerPrivateNetworks (req * WaitForServerPrivateNetworksRequest , opts ... scw.RequestOption ) ([]* ServerPrivateNetwork , error ) {
222+ timeout := defaultTimeout
223+ if req .Timeout != nil {
224+ timeout = * req .Timeout
225+ }
226+ retryInterval := defaultRetryInterval
227+ if req .RetryInterval != nil {
228+ retryInterval = * req .RetryInterval
229+ }
230+
231+ terminalStatus := map [ServerPrivateNetworkStatus ]struct {}{
232+ ServerPrivateNetworkStatusAttached : {},
233+ ServerPrivateNetworkStatusError : {},
234+ ServerPrivateNetworkStatusUnknown : {},
235+ ServerPrivateNetworkStatusLocked : {},
236+ }
237+
238+ serverPrivateNetwork , err := async .WaitSync (& async.WaitSyncConfig {
239+ Get : func () (interface {}, bool , error ) {
240+ res , err := s .ListServerPrivateNetworks (& PrivateNetworkAPIListServerPrivateNetworksRequest {
241+ ServerID : & req .ServerID ,
242+ Zone : req .Zone ,
243+ }, opts ... )
244+ if err != nil {
245+ return nil , false , err
246+ }
247+
248+ for i := range res .ServerPrivateNetworks {
249+ _ , isTerminal := terminalStatus [res .ServerPrivateNetworks [i ].Status ]
250+ if ! isTerminal {
251+ return res , isTerminal , nil
252+ }
253+ }
254+ return res , true , err
255+ },
256+ Timeout : timeout ,
257+ IntervalStrategy : async .LinearIntervalStrategy (retryInterval ),
258+ })
259+ if err != nil {
260+ return nil , errors .Wrap (err , "waiting for server private networks failed" )
261+ }
262+
263+ return serverPrivateNetwork .([]* ServerPrivateNetwork ), nil
264+ }
0 commit comments