@@ -2,7 +2,9 @@ package mongodb
22
33import (
44 "context"
5+ "reflect"
56 "strings"
7+ "time"
68
79 "github.com/fatih/color"
810 "github.com/scaleway/scaleway-cli/v2/core"
@@ -11,6 +13,10 @@ import (
1113 "github.com/scaleway/scaleway-sdk-go/scw"
1214)
1315
16+ const (
17+ instanceActionTimeout = 20 * time .Minute
18+ )
19+
1420var instanceStatusMarshalSpecs = human.EnumMarshalSpecs {
1521 mongodb .InstanceStatusConfiguring : & human.EnumMarshalSpec {
1622 Attribute : color .FgBlue ,
@@ -53,6 +59,18 @@ func instanceCreateBuilder(c *core.Command) *core.Command {
5359 c .ArgSpecs .GetByName ("volume.volume-type" ).Default = core .DefaultValueSetter ("sbs_5k" )
5460 c .ArgSpecs .GetByName ("node-type" ).AutoCompleteFunc = autoCompleteNodeType
5561
62+ c .WaitFunc = func (ctx context.Context , _ , respI interface {}) (interface {}, error ) {
63+ getResp := respI .(* mongodb.Instance )
64+ api := mongodb .NewAPI (core .ExtractClient (ctx ))
65+
66+ return api .WaitForInstance (& mongodb.WaitForInstanceRequest {
67+ InstanceID : getResp .ID ,
68+ Region : getResp .Region ,
69+ Timeout : scw .TimeDurationPtr (instanceActionTimeout ),
70+ RetryInterval : core .DefaultRetryInterval ,
71+ })
72+ }
73+
5674 return c
5775}
5876
@@ -105,3 +123,47 @@ func autoCompleteNodeType(
105123
106124 return suggestions
107125}
126+
127+ type serverWaitRequest struct {
128+ InstanceID string
129+ Region scw.Region
130+ Timeout time.Duration
131+ }
132+
133+ func instanceWaitCommand () * core.Command {
134+ return & core.Command {
135+ Short : `Wait for an instance to reach a stable state` ,
136+ Long : `Wait for an instance to reach a stable state. This is similar to using --wait flag.` ,
137+ Namespace : "mongodb" ,
138+ Resource : "instance" ,
139+ Verb : "wait" ,
140+ Groups : []string {"workflow" },
141+ ArgsType : reflect .TypeOf (serverWaitRequest {}),
142+ Run : func (ctx context.Context , argsI interface {}) (i interface {}, err error ) {
143+ api := mongodb .NewAPI (core .ExtractClient (ctx ))
144+
145+ return api .WaitForInstance (& mongodb.WaitForInstanceRequest {
146+ Region : argsI .(* serverWaitRequest ).Region ,
147+ InstanceID : argsI .(* serverWaitRequest ).InstanceID ,
148+ Timeout : scw .TimeDurationPtr (argsI .(* serverWaitRequest ).Timeout ),
149+ RetryInterval : core .DefaultRetryInterval ,
150+ })
151+ },
152+ ArgSpecs : core.ArgSpecs {
153+ {
154+ Name : "instance-id" ,
155+ Short : `ID of the instance you want to wait for.` ,
156+ Required : true ,
157+ Positional : true ,
158+ },
159+ core .RegionArgSpec (scw .RegionFrPar , scw .RegionNlAms ),
160+ core .WaitTimeoutArgSpec (instanceActionTimeout ),
161+ },
162+ Examples : []* core.Example {
163+ {
164+ Short : "Wait for an instance to reach a stable state" ,
165+ ArgsJSON : `{"instance_id": "11111111-1111-1111-1111-111111111111"}` ,
166+ },
167+ },
168+ }
169+ }
0 commit comments