@@ -8,29 +8,60 @@ import (
88 "github.com/scaleway/scaleway-sdk-go/scw"
99)
1010
11- // WaitForServerRequest is used by WaitForServer method
11+ // WaitForServerRequest is used by WaitForServer method.
1212type WaitForServerRequest struct {
1313 ServerID string
1414 Zone scw.Zone
1515 Timeout time.Duration
1616}
1717
1818// WaitForServer wait for the server to be in a "terminal state" before returning.
19- // This function can be used to wait for a server to be installed for example .
19+ // This function can be used to wait for a server to be created .
2020func (s * API ) WaitForServer (req * WaitForServerRequest ) (* Server , scw.SdkError ) {
21-
2221 terminalStatus := map [ServerStatus ]struct {}{
23- ServerStatusReady : {},
24- ServerStatusStopped : {},
25- ServerStatusError : {},
26- ServerStatusUndelivered : {},
27- ServerStatusLocked : {},
28- ServerStatusUnknown : {},
22+ ServerStatusReady : {},
23+ ServerStatusStopped : {},
24+ ServerStatusError : {},
25+ ServerStatusLocked : {},
26+ ServerStatusUnknown : {},
2927 }
3028
29+ server , err := async .WaitSync (& async.WaitSyncConfig {
30+ Get : func () (interface {}, error , bool ) {
31+ res , err := s .GetServer (& GetServerRequest {
32+ ServerID : req .ServerID ,
33+ Zone : req .Zone ,
34+ })
35+ if err != nil {
36+ return nil , err , false
37+ }
38+
39+ _ , isTerminal := terminalStatus [res .Status ]
40+ return res , err , isTerminal
41+ },
42+ Timeout : req .Timeout ,
43+ IntervalStrategy : async .LinearIntervalStrategy (5 * time .Second ),
44+ })
45+ if err != nil {
46+ return nil , errors .Wrap (err , "waiting for server failed" )
47+ }
48+
49+ return server .(* Server ), nil
50+ }
51+
52+ // WaitForServerInstallRequest is used by WaitForServerInstall method.
53+ type WaitForServerInstallRequest struct {
54+ ServerID string
55+ Zone scw.Zone
56+ Timeout time.Duration
57+ }
58+
59+ // WaitForServerInstall wait for the server install to be in a
60+ // "terminal state" before returning.
61+ // This function can be used to wait for a server to be installed.
62+ func (s * API ) WaitForServerInstall (req * WaitForServerInstallRequest ) (* Server , scw.SdkError ) {
3163 installTerminalStatus := map [ServerInstallStatus ]struct {}{
3264 ServerInstallStatusCompleted : {},
33- ServerInstallStatusToInstall : {},
3465 ServerInstallStatusError : {},
3566 ServerInstallStatusUnknown : {},
3667 }
@@ -41,23 +72,22 @@ func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, scw.SdkError) {
4172 ServerID : req .ServerID ,
4273 Zone : req .Zone ,
4374 })
44-
4575 if err != nil {
4676 return nil , err , false
4777 }
48- _ , isTerminal := terminalStatus [res .Status ]
49- isTerminalInstall := true
50- if res .Install != nil {
51- _ , isTerminalInstall = installTerminalStatus [res .Install .Status ]
78+
79+ if res .Install == nil {
80+ return nil , errors .New ("server creation has not begun for server %s" , req .ServerID ), false
5281 }
5382
54- return res , err , isTerminal && isTerminalInstall
83+ _ , isTerminal := installTerminalStatus [res .Install .Status ]
84+ return res , err , isTerminal
5585 },
5686 Timeout : req .Timeout ,
57- IntervalStrategy : async .LinearIntervalStrategy (5 * time .Second ),
87+ IntervalStrategy : async .LinearIntervalStrategy (15 * time .Second ),
5888 })
5989 if err != nil {
60- return nil , errors .Wrap (err , "waiting for server failed" )
90+ return nil , errors .Wrap (err , "waiting for server installation failed" )
6191 }
6292
6393 return server .(* Server ), nil
0 commit comments