From a4c0e30e8f8443e30eae729bcb4f3d784162480b Mon Sep 17 00:00:00 2001 From: lmasson Date: Thu, 1 Aug 2024 11:12:29 +0200 Subject: [PATCH] feat(dedibox): waiter service support --- .../namespaces/dedibox/v1/custom_server.go | 1 + .../namespaces/dedibox/v1/custom_service.go | 41 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 internal/namespaces/dedibox/v1/custom_server.go diff --git a/internal/namespaces/dedibox/v1/custom_server.go b/internal/namespaces/dedibox/v1/custom_server.go new file mode 100644 index 0000000000..d2f8975e10 --- /dev/null +++ b/internal/namespaces/dedibox/v1/custom_server.go @@ -0,0 +1 @@ +package dedibox diff --git a/internal/namespaces/dedibox/v1/custom_service.go b/internal/namespaces/dedibox/v1/custom_service.go index b68f52c7de..1162d3fa83 100644 --- a/internal/namespaces/dedibox/v1/custom_service.go +++ b/internal/namespaces/dedibox/v1/custom_service.go @@ -2,13 +2,50 @@ package dedibox import ( "context" + "errors" + "fmt" "github.com/scaleway/scaleway-cli/v2/internal/core" "github.com/scaleway/scaleway-sdk-go/api/dedibox/v1" + "github.com/scaleway/scaleway-sdk-go/scw" + "net/http" + "time" +) + +const ( + serviceActionTimeout = time.Minute * 60 +) + +const ( + serviceActionCreate = iota + serviceActionDelete ) func serviceCreateBuilder(c *core.Command) *core.Command { - c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { - api := dedibox.NewAPI(core.ExtractClient(ctx)) + c.WaitFunc = waitForServiceFunc(serviceActionCreate) + return c +} +func waitForServiceFunc(action int) core.WaitFunc { + return func(ctx context.Context, argsI, respI interface{}) (interface{}, error) { + service, err := dedibox.NewAPI(core.ExtractClient(ctx)).WaitForService(&dedibox.WaitForServiceRequest{ + ServiceID: respI.(*dedibox.Service).ID, + Zone: argsI.(*dedibox.CreateServerRequest).Zone, + Timeout: scw.TimeDurationPtr(serviceActionTimeout), + RetryInterval: core.DefaultRetryInterval, + }) + switch action { + case serviceActionCreate: + return service, err + case serviceActionDelete: + if err != nil { + notFoundError := &scw.ResourceNotFoundError{} + responseError := &scw.ResponseError{} + if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, ¬FoundError) { + return fmt.Sprintf("Server %s successfully deleted.", respI.(*dedibox.Service).ID), nil + } + } + } + print("error: ", err) + return nil, err } }