@@ -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
@@ -499,7 +505,7 @@ type OS struct {
499505 // Deprecated: CompatibleServerTypes: list of compatible server types. Deprecated.
500506 CompatibleServerTypes * []string `json:"compatible_server_types,omitempty"`
501507
502- // ReleaseNotesURL: url of the release notes for the OS image or softwares pre-installed.
508+ // ReleaseNotesURL: url of the release notes for the OS image or software pre-installed.
503509 ReleaseNotesURL string `json:"release_notes_url"`
504510
505511 // Description: a summary of the OS image content and configuration.
@@ -1430,6 +1436,59 @@ 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+ Zone scw.Zone
1442+ ServerID string
1443+ Timeout * time.Duration
1444+ RetryInterval * time.Duration
1445+ }
1446+
1447+ // WaitForServer waits for the Server to reach a terminal state.
1448+ func (s * API ) WaitForServer (req * WaitForServerRequest , opts ... scw.RequestOption ) (* Server , error ) {
1449+ timeout := defaultApplesiliconTimeout
1450+ if req .Timeout != nil {
1451+ timeout = * req .Timeout
1452+ }
1453+
1454+ retryInterval := defaultApplesiliconRetryInterval
1455+ if req .RetryInterval != nil {
1456+ retryInterval = * req .RetryInterval
1457+ }
1458+ transientStatuses := map [ServerStatus ]struct {}{
1459+ ServerStatusStarting : {},
1460+ ServerStatusRebooting : {},
1461+ ServerStatusUpdating : {},
1462+ ServerStatusLocking : {},
1463+ ServerStatusUnlocking : {},
1464+ ServerStatusReinstalling : {},
1465+ ServerStatusBusy : {},
1466+ }
1467+
1468+ res , err := async .WaitSync (& async.WaitSyncConfig {
1469+ Get : func () (any , bool , error ) {
1470+ res , err := s .GetServer (& GetServerRequest {
1471+ Zone : req .Zone ,
1472+ ServerID : req .ServerID ,
1473+ }, opts ... )
1474+ if err != nil {
1475+ return nil , false , err
1476+ }
1477+
1478+ _ , isTransient := transientStatuses [res .Status ]
1479+
1480+ return res , ! isTransient , nil
1481+ },
1482+ IntervalStrategy : async .LinearIntervalStrategy (retryInterval ),
1483+ Timeout : timeout ,
1484+ })
1485+ if err != nil {
1486+ return nil , errors .Wrap (err , "waiting for Server failed" )
1487+ }
1488+
1489+ return res .(* Server ), nil
1490+ }
1491+
14331492// UpdateServer: Update the parameters of an existing Apple silicon server, specified by its server ID.
14341493func (s * API ) UpdateServer (req * UpdateServerRequest , opts ... scw.RequestOption ) (* Server , error ) {
14351494 var err error
@@ -1531,7 +1590,7 @@ func (s *API) RebootServer(req *RebootServerRequest, opts ...scw.RequestOption)
15311590 return & resp , nil
15321591}
15331592
1534- // ReinstallServer: Reinstall an existing Apple silicon server (specified by its server ID) from a new image (OS). All the data on the disk is deleted and all configuration is reset to the defailt configuration values of the image (OS).
1593+ // ReinstallServer: Reinstall an existing Apple silicon server (specified by its server ID) from a new image (OS). All the data on the disk is deleted and all configuration is reset to the default configuration values of the image (OS).
15351594func (s * API ) ReinstallServer (req * ReinstallServerRequest , opts ... scw.RequestOption ) (* Server , error ) {
15361595 var err error
15371596
@@ -1681,6 +1740,56 @@ func (s *PrivateNetworkAPI) GetServerPrivateNetwork(req *PrivateNetworkAPIGetSer
16811740 return & resp , nil
16821741}
16831742
1743+ // WaitForServerPrivateNetworkRequest is used by WaitForServerPrivateNetwork method.
1744+ type WaitForServerPrivateNetworkRequest struct {
1745+ Zone scw.Zone
1746+ ServerID string
1747+ PrivateNetworkID string
1748+ Timeout * time.Duration
1749+ RetryInterval * time.Duration
1750+ }
1751+
1752+ // WaitForServerPrivateNetwork waits for the ServerPrivateNetwork to reach a terminal state.
1753+ func (s * PrivateNetworkAPI ) WaitForServerPrivateNetwork (req * WaitForServerPrivateNetworkRequest , opts ... scw.RequestOption ) (* ServerPrivateNetwork , error ) {
1754+ timeout := defaultApplesiliconTimeout
1755+ if req .Timeout != nil {
1756+ timeout = * req .Timeout
1757+ }
1758+
1759+ retryInterval := defaultApplesiliconRetryInterval
1760+ if req .RetryInterval != nil {
1761+ retryInterval = * req .RetryInterval
1762+ }
1763+ transientStatuses := map [ServerPrivateNetworkServerStatus ]struct {}{
1764+ ServerPrivateNetworkServerStatusAttaching : {},
1765+ ServerPrivateNetworkServerStatusDetaching : {},
1766+ }
1767+
1768+ res , err := async .WaitSync (& async.WaitSyncConfig {
1769+ Get : func () (any , bool , error ) {
1770+ res , err := s .GetServerPrivateNetwork (& PrivateNetworkAPIGetServerPrivateNetworkRequest {
1771+ Zone : req .Zone ,
1772+ ServerID : req .ServerID ,
1773+ PrivateNetworkID : req .PrivateNetworkID ,
1774+ }, opts ... )
1775+ if err != nil {
1776+ return nil , false , err
1777+ }
1778+
1779+ _ , isTransient := transientStatuses [res .Status ]
1780+
1781+ return res , ! isTransient , nil
1782+ },
1783+ IntervalStrategy : async .LinearIntervalStrategy (retryInterval ),
1784+ Timeout : timeout ,
1785+ })
1786+ if err != nil {
1787+ return nil , errors .Wrap (err , "waiting for ServerPrivateNetwork failed" )
1788+ }
1789+
1790+ return res .(* ServerPrivateNetwork ), nil
1791+ }
1792+
16841793// AddServerPrivateNetwork: Add an Apple silicon server to a Private Network.
16851794func (s * PrivateNetworkAPI ) AddServerPrivateNetwork (req * PrivateNetworkAPIAddServerPrivateNetworkRequest , opts ... scw.RequestOption ) (* ServerPrivateNetwork , error ) {
16861795 var err error
0 commit comments