Skip to content

Commit a4c0e30

Browse files
committed
feat(dedibox): waiter service support
1 parent 26666b0 commit a4c0e30

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Diff for: internal/namespaces/dedibox/v1/custom_server.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package dedibox

Diff for: internal/namespaces/dedibox/v1/custom_service.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,50 @@ package dedibox
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
57
"github.com/scaleway/scaleway-cli/v2/internal/core"
68
"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
721
)
822

923
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+
}
1227

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
1350
}
1451
}

0 commit comments

Comments
 (0)