Skip to content

Commit febdd5a

Browse files
scaleway-botyfodil
andauthored
feat: generate waiters (#2823)
Co-authored-by: Yacine Fodil <[email protected]> Co-authored-by: Yacine FODIL <[email protected]>
1 parent ed9354a commit febdd5a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3136
-2099
lines changed

api/applesilicon/v1alpha1/apple_silicon_utils.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,6 @@ const (
1313
defaultTimeout = 60 * time.Minute
1414
)
1515

16-
// WaitForInstanceRequest is used by WaitForServer method.
17-
type WaitForServerRequest struct {
18-
ServerID string
19-
Zone scw.Zone
20-
Timeout *time.Duration
21-
RetryInterval *time.Duration
22-
}
23-
24-
// WaitForServer waits for the instance to be in a "terminal state" before returning.
25-
// This function can be used to wait for an instance to be ready for example.
26-
func (s *API) WaitForServer(req *WaitForServerRequest, opts ...scw.RequestOption) (*Server, error) {
27-
timeout := defaultTimeout
28-
if req.Timeout != nil {
29-
timeout = *req.Timeout
30-
}
31-
retryInterval := defaultRetryInterval
32-
if req.RetryInterval != nil {
33-
retryInterval = *req.RetryInterval
34-
}
35-
36-
terminalStatus := map[ServerStatus]struct{}{
37-
ServerStatusReady: {},
38-
ServerStatusError: {},
39-
}
40-
41-
server, err := async.WaitSync(&async.WaitSyncConfig{
42-
Get: func() (any, bool, error) {
43-
res, err := s.GetServer(&GetServerRequest{
44-
ServerID: req.ServerID,
45-
Zone: req.Zone,
46-
}, opts...)
47-
if err != nil {
48-
return nil, false, err
49-
}
50-
_, isTerminal := terminalStatus[res.Status]
51-
52-
return res, isTerminal, nil
53-
},
54-
Timeout: timeout,
55-
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
56-
})
57-
if err != nil {
58-
return nil, errors.Wrap(err, "waiting for server failed")
59-
}
60-
return server.(*Server), nil
61-
}
62-
6316
func (s *PrivateNetworkAPI) WaitForServerPrivateNetworks(req *WaitForServerRequest, opts ...scw.RequestOption) ([]*ServerPrivateNetwork, error) {
6417
timeout := defaultTimeout
6518
if req.Timeout != nil {

api/applesilicon/v1alpha1/applesilicon_sdk.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2531
var (
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.
14341492
func (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.
16851791
func (s *PrivateNetworkAPI) AddServerPrivateNetwork(req *PrivateNetworkAPIAddServerPrivateNetworkRequest, opts ...scw.RequestOption) (*ServerPrivateNetwork, error) {
16861792
var err error

api/baremetal/v1/baremetal_sdk.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
defaultBaremetalRetryInterval = 15 * time.Second
27+
defaultBaremetalTimeout = 2 * time.Hour
28+
)
29+
2430
// always import dependencies
2531
var (
2632
_ fmt.Stringer
@@ -2285,6 +2291,58 @@ func (s *API) GetServer(req *GetServerRequest, opts ...scw.RequestOption) (*Serv
22852291
return &resp, nil
22862292
}
22872293

2294+
// WaitForServerRequest is used by WaitForServer method.
2295+
type WaitForServerRequest struct {
2296+
GetServerRequest
2297+
Timeout *time.Duration
2298+
RetryInterval *time.Duration
2299+
}
2300+
2301+
// WaitForServer waits for the Server to reach a terminal state.
2302+
func (s *API) WaitForServer(req *WaitForServerRequest, opts ...scw.RequestOption) (*Server, error) {
2303+
timeout := defaultBaremetalTimeout
2304+
if req.Timeout != nil {
2305+
timeout = *req.Timeout
2306+
}
2307+
2308+
retryInterval := defaultBaremetalRetryInterval
2309+
if req.RetryInterval != nil {
2310+
retryInterval = *req.RetryInterval
2311+
}
2312+
transientStatuses := map[ServerStatus]struct{}{
2313+
ServerStatusDelivering: {},
2314+
ServerStatusStopping: {},
2315+
ServerStatusStarting: {},
2316+
ServerStatusDeleting: {},
2317+
ServerStatusOrdered: {},
2318+
ServerStatusResetting: {},
2319+
ServerStatusMigrating: {},
2320+
}
2321+
2322+
res, err := async.WaitSync(&async.WaitSyncConfig{
2323+
Get: func() (interface{}, bool, error) {
2324+
res, err := s.GetServer(&GetServerRequest{
2325+
Zone: req.Zone,
2326+
ServerID: req.ServerID,
2327+
}, opts...)
2328+
if err != nil {
2329+
return nil, false, err
2330+
}
2331+
2332+
_, isTransient := transientStatuses[res.Status]
2333+
2334+
return res, !isTransient, nil
2335+
},
2336+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
2337+
Timeout: timeout,
2338+
})
2339+
if err != nil {
2340+
return nil, errors.Wrap(err, "waiting for Server failed")
2341+
}
2342+
2343+
return res.(*Server), nil
2344+
}
2345+
22882346
// CreateServer: Create a new Elastic Metal server. Once the server is created, proceed with the [installation of an OS](#post-3e949e).
22892347
func (s *API) CreateServer(req *CreateServerRequest, opts ...scw.RequestOption) (*Server, error) {
22902348
var err error

api/baremetal/v1/server_utils.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,6 @@ const (
1313
defaultTimeout = 2 * time.Hour
1414
)
1515

16-
// WaitForServerRequest is used by WaitForServer method.
17-
type WaitForServerRequest struct {
18-
ServerID string
19-
Zone scw.Zone
20-
Timeout *time.Duration
21-
RetryInterval *time.Duration
22-
}
23-
24-
// WaitForServer wait for the server to be in a "terminal state" before returning.
25-
// This function can be used to wait for a server to be created.
26-
func (s *API) WaitForServer(req *WaitForServerRequest, opts ...scw.RequestOption) (*Server, error) {
27-
timeout := defaultTimeout
28-
if req.Timeout != nil {
29-
timeout = *req.Timeout
30-
}
31-
retryInterval := defaultRetryInterval
32-
if req.RetryInterval != nil {
33-
retryInterval = *req.RetryInterval
34-
}
35-
36-
terminalStatus := map[ServerStatus]struct{}{
37-
ServerStatusReady: {},
38-
ServerStatusStopped: {},
39-
ServerStatusError: {},
40-
ServerStatusLocked: {},
41-
ServerStatusUnknown: {},
42-
}
43-
44-
server, err := async.WaitSync(&async.WaitSyncConfig{
45-
Get: func() (any, bool, error) {
46-
res, err := s.GetServer(&GetServerRequest{
47-
ServerID: req.ServerID,
48-
Zone: req.Zone,
49-
}, opts...)
50-
if err != nil {
51-
return nil, false, err
52-
}
53-
54-
_, isTerminal := terminalStatus[res.Status]
55-
return res, isTerminal, err
56-
},
57-
Timeout: timeout,
58-
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
59-
})
60-
if err != nil {
61-
return nil, errors.Wrap(err, "waiting for server failed")
62-
}
63-
64-
return server.(*Server), nil
65-
}
66-
6716
// WaitForServerInstallRequest is used by WaitForServerInstall method.
6817
type WaitForServerInstallRequest struct {
6918
ServerID string

0 commit comments

Comments
 (0)