@@ -2,13 +2,50 @@ package dedibox
2
2
3
3
import (
4
4
"context"
5
+ "errors"
6
+ "fmt"
5
7
"github.com/scaleway/scaleway-cli/v2/internal/core"
6
8
"github.com/scaleway/scaleway-sdk-go/api/dedibox/v1"
9
+ "github.com/scaleway/scaleway-sdk-go/scw"
10
+ "net/http"
11
+ "time"
12
+ )
13
+
14
+ const (
15
+ serviceActionTimeout = time .Minute * 60
16
+ )
17
+
18
+ const (
19
+ serviceActionCreate = iota
20
+ serviceActionDelete
7
21
)
8
22
9
23
func serviceCreateBuilder (c * core.Command ) * core.Command {
10
- c .WaitFunc = func (ctx context.Context , argsI , respI interface {}) (interface {}, error ) {
11
- api := dedibox .NewAPI (core .ExtractClient (ctx ))
24
+ c .WaitFunc = waitForServiceFunc (serviceActionCreate )
25
+ return c
26
+ }
12
27
28
+ func waitForServiceFunc (action int ) core.WaitFunc {
29
+ return func (ctx context.Context , argsI , respI interface {}) (interface {}, error ) {
30
+ service , err := dedibox .NewAPI (core .ExtractClient (ctx )).WaitForService (& dedibox.WaitForServiceRequest {
31
+ ServiceID : respI .(* dedibox.Service ).ID ,
32
+ Zone : argsI .(* dedibox.CreateServerRequest ).Zone ,
33
+ Timeout : scw .TimeDurationPtr (serviceActionTimeout ),
34
+ RetryInterval : core .DefaultRetryInterval ,
35
+ })
36
+ switch action {
37
+ case serviceActionCreate :
38
+ return service , err
39
+ case serviceActionDelete :
40
+ if err != nil {
41
+ notFoundError := & scw.ResourceNotFoundError {}
42
+ responseError := & scw.ResponseError {}
43
+ if errors .As (err , & responseError ) && responseError .StatusCode == http .StatusNotFound || errors .As (err , & notFoundError ) {
44
+ return fmt .Sprintf ("Server %s successfully deleted." , respI .(* dedibox.Service ).ID ), nil
45
+ }
46
+ }
47
+ }
48
+ print ("error: " , err )
49
+ return nil , err
13
50
}
14
51
}
0 commit comments