@@ -15,12 +15,18 @@ import (
1515 "time"
1616
1717 "github.com/scaleway/scaleway-sdk-go/errors"
18+ "github.com/scaleway/scaleway-sdk-go/internal/async"
1819 "github.com/scaleway/scaleway-sdk-go/marshaler"
1920 "github.com/scaleway/scaleway-sdk-go/namegenerator"
2021 "github.com/scaleway/scaleway-sdk-go/parameter"
2122 "github.com/scaleway/scaleway-sdk-go/scw"
2223)
2324
25+ const (
26+ defaultApplesiliconRetryInterval = 15 * time .Second
27+ defaultApplesiliconTimeout = 1 * time .Hour
28+ )
29+
2430// always import dependencies
2531var (
2632 _ fmt.Stringer
@@ -1430,6 +1436,58 @@ func (s *API) GetServer(req *GetServerRequest, opts ...scw.RequestOption) (*Serv
14301436 return & resp , nil
14311437}
14321438
1439+ // WaitForServerRequest is used by WaitForServer method.
1440+ type WaitForServerRequest struct {
1441+ GetServerRequest
1442+ Timeout * time.Duration
1443+ RetryInterval * time.Duration
1444+ }
1445+
1446+ // WaitForServer waits for the Server to reach a terminal state.
1447+ func (s * API ) WaitForServer (req * WaitForServerRequest , opts ... scw.RequestOption ) (* Server , error ) {
1448+ timeout := defaultApplesiliconTimeout
1449+ if req .Timeout != nil {
1450+ timeout = * req .Timeout
1451+ }
1452+
1453+ retryInterval := defaultApplesiliconRetryInterval
1454+ if req .RetryInterval != nil {
1455+ retryInterval = * req .RetryInterval
1456+ }
1457+ transientStatuses := map [ServerStatus ]struct {}{
1458+ ServerStatusStarting : {},
1459+ ServerStatusRebooting : {},
1460+ ServerStatusUpdating : {},
1461+ ServerStatusLocking : {},
1462+ ServerStatusUnlocking : {},
1463+ ServerStatusReinstalling : {},
1464+ ServerStatusBusy : {},
1465+ }
1466+
1467+ res , err := async .WaitSync (& async.WaitSyncConfig {
1468+ Get : func () (interface {}, bool , error ) {
1469+ res , err := s .GetServer (& GetServerRequest {
1470+ Zone : req .Zone ,
1471+ ServerID : req .ServerID ,
1472+ }, opts ... )
1473+ if err != nil {
1474+ return nil , false , err
1475+ }
1476+
1477+ _ , isTransient := transientStatuses [res .Status ]
1478+
1479+ return res , ! isTransient , nil
1480+ },
1481+ IntervalStrategy : async .LinearIntervalStrategy (retryInterval ),
1482+ Timeout : timeout ,
1483+ })
1484+ if err != nil {
1485+ return nil , errors .Wrap (err , "waiting for Server failed" )
1486+ }
1487+
1488+ return res .(* Server ), nil
1489+ }
1490+
14331491// UpdateServer: Update the parameters of an existing Apple silicon server, specified by its server ID.
14341492func (s * API ) UpdateServer (req * UpdateServerRequest , opts ... scw.RequestOption ) (* Server , error ) {
14351493 var err error
@@ -1681,6 +1739,54 @@ func (s *PrivateNetworkAPI) GetServerPrivateNetwork(req *PrivateNetworkAPIGetSer
16811739 return & resp , nil
16821740}
16831741
1742+ // WaitForServerPrivateNetworkRequest is used by WaitForServerPrivateNetwork method.
1743+ type WaitForServerPrivateNetworkRequest struct {
1744+ PrivateNetworkAPIGetServerPrivateNetworkRequest
1745+ Timeout * time.Duration
1746+ RetryInterval * time.Duration
1747+ }
1748+
1749+ // WaitForServerPrivateNetwork waits for the ServerPrivateNetwork to reach a terminal state.
1750+ func (s * PrivateNetworkAPI ) WaitForServerPrivateNetwork (req * WaitForServerPrivateNetworkRequest , opts ... scw.RequestOption ) (* ServerPrivateNetwork , error ) {
1751+ timeout := defaultApplesiliconTimeout
1752+ if req .Timeout != nil {
1753+ timeout = * req .Timeout
1754+ }
1755+
1756+ retryInterval := defaultApplesiliconRetryInterval
1757+ if req .RetryInterval != nil {
1758+ retryInterval = * req .RetryInterval
1759+ }
1760+ transientStatuses := map [ServerPrivateNetworkServerStatus ]struct {}{
1761+ ServerPrivateNetworkServerStatusAttaching : {},
1762+ ServerPrivateNetworkServerStatusDetaching : {},
1763+ }
1764+
1765+ res , err := async .WaitSync (& async.WaitSyncConfig {
1766+ Get : func () (interface {}, bool , error ) {
1767+ res , err := s .GetServerPrivateNetwork (& PrivateNetworkAPIGetServerPrivateNetworkRequest {
1768+ Zone : req .Zone ,
1769+ ServerID : req .ServerID ,
1770+ PrivateNetworkID : req .PrivateNetworkID ,
1771+ }, opts ... )
1772+ if err != nil {
1773+ return nil , false , err
1774+ }
1775+
1776+ _ , isTransient := transientStatuses [res .Status ]
1777+
1778+ return res , ! isTransient , nil
1779+ },
1780+ IntervalStrategy : async .LinearIntervalStrategy (retryInterval ),
1781+ Timeout : timeout ,
1782+ })
1783+ if err != nil {
1784+ return nil , errors .Wrap (err , "waiting for ServerPrivateNetwork failed" )
1785+ }
1786+
1787+ return res .(* ServerPrivateNetwork ), nil
1788+ }
1789+
16841790// AddServerPrivateNetwork: Add an Apple silicon server to a Private Network.
16851791func (s * PrivateNetworkAPI ) AddServerPrivateNetwork (req * PrivateNetworkAPIAddServerPrivateNetworkRequest , opts ... scw.RequestOption ) (* ServerPrivateNetwork , error ) {
16861792 var err error
0 commit comments