@@ -10,6 +10,7 @@ import (
1010
1111const (
1212 waitForClusterDefaultTimeout = time .Minute * 15
13+ waitForPoolDefaultTimeout = time .Minute * 15
1314)
1415
1516// WaitForClusterRequest is used by WaitForCluster method.
@@ -55,3 +56,79 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) {
5556 }
5657 return cluster .(* Cluster ), nil
5758}
59+
60+ // WaitForClusterPoolsRequest is used by WaitForClusterPools method.
61+ type WaitForClusterPoolsRequest struct {
62+ ClusterID string
63+ Region scw.Region
64+ Timeout * time.Duration
65+ }
66+
67+ // WaitForClusterPools waits for the pools of a cluster to be ready
68+ func (s * API ) WaitForClusterPools (req * WaitForClusterPoolsRequest ) error {
69+ timeout := waitForPoolDefaultTimeout
70+ if req .Timeout != nil {
71+ timeout = * req .Timeout
72+ }
73+
74+ pools , err := s .ListPools (& ListPoolsRequest {
75+ ClusterID : req .ClusterID ,
76+ Region : req .Region ,
77+ })
78+ if err != nil {
79+ return err
80+ }
81+
82+ for _ , pool := range pools .Pools {
83+ err = s .WaitForPool (& WaitForPoolRequest {
84+ PoolID : pool .ID ,
85+ Timeout : & timeout ,
86+ })
87+
88+ if err != nil {
89+ return err
90+ }
91+ }
92+
93+ return nil
94+ }
95+
96+ // WaitForPoolRequest is used by WaitForPool method.
97+ type WaitForPoolRequest struct {
98+ PoolID string
99+ Region scw.Region
100+ Timeout * time.Duration
101+ }
102+
103+ // WaitForPool waits for a pool to be ready
104+ func (s * API ) WaitForPool (req * WaitForPoolRequest ) error {
105+ terminalStatus := map [PoolStatus ]struct {}{
106+ PoolStatusReady : {},
107+ PoolStatusWarning : {},
108+ }
109+
110+ timeout := waitForPoolDefaultTimeout
111+ if req .Timeout != nil {
112+ timeout = * req .Timeout
113+ }
114+
115+ _ , err := async .WaitSync (& async.WaitSyncConfig {
116+ Get : func () (interface {}, bool , error ) {
117+ res , err := s .GetPool (& GetPoolRequest {
118+ PoolID : req .PoolID ,
119+ Region : req .Region ,
120+ })
121+
122+ if err != nil {
123+ return nil , false , err
124+ }
125+ _ , isTerminal := terminalStatus [res .Status ]
126+
127+ return nil , isTerminal , nil
128+ },
129+ Timeout : timeout ,
130+ IntervalStrategy : async .LinearIntervalStrategy (5 * time .Second ),
131+ })
132+
133+ return err
134+ }
0 commit comments