Skip to content

Commit ae6463e

Browse files
authored
feat(container): add namespace waiters (#2937)
1 parent 715871c commit ae6463e

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

cmd/scw/testdata/test-all-usage-container-namespace-create-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ARGS:
1616

1717
FLAGS:
1818
-h, --help help for create
19+
-w, --wait wait until the namespace is ready
1920

2021
GLOBAL FLAGS:
2122
-c, --config string The path to the config file

cmd/scw/testdata/test-all-usage-container-namespace-delete-usage.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ARGS:
1111

1212
FLAGS:
1313
-h, --help help for delete
14+
-w, --wait wait until the namespace is ready
1415

1516
GLOBAL FLAGS:
1617
-c, --config string The path to the config file

internal/core/errors_cmp.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package core
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
7+
"github.com/scaleway/scaleway-sdk-go/scw"
8+
)
9+
10+
func IsNotFoundError(err error) bool {
11+
notFoundError := &scw.ResourceNotFoundError{}
12+
responseError := &scw.ResponseError{}
13+
return errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, &notFoundError)
14+
}

internal/namespaces/container/v1beta1/custom.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ func GetCommands() *core.Commands {
1414
human.RegisterMarshalerFunc(container.CronStatus(""), human.EnumMarshalFunc(cronStatusMarshalSpecs))
1515

1616
cmds.MustFind("container", "container", "deploy").Override(containerContainerDeployBuilder)
17+
cmds.MustFind("container", "namespace", "create").Override(containerNamespaceCreateBuilder)
18+
cmds.MustFind("container", "namespace", "delete").Override(containerNamespaceDeleteBuilder)
1719

1820
cmds.Add(containerDeployCommand())
1921

internal/namespaces/container/v1beta1/custom_namespace.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package container
22

33
import (
4+
"context"
5+
"time"
6+
47
"github.com/fatih/color"
8+
"github.com/scaleway/scaleway-cli/v2/internal/core"
59
"github.com/scaleway/scaleway-cli/v2/internal/human"
610
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
712
)
813

914
var (
15+
containerNamespaceActionTimeout = 5 * time.Minute
16+
1017
namespaceStatusMarshalSpecs = human.EnumMarshalSpecs{
1118
container.NamespaceStatusCreating: &human.EnumMarshalSpec{Attribute: color.FgBlue},
1219
container.NamespaceStatusDeleting: &human.EnumMarshalSpec{Attribute: color.FgBlue},
@@ -17,3 +24,46 @@ var (
1724
container.NamespaceStatusUnknown: &human.EnumMarshalSpec{Attribute: color.Faint},
1825
}
1926
)
27+
28+
func containerNamespaceCreateBuilder(c *core.Command) *core.Command {
29+
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
30+
res := respI.(*container.Namespace)
31+
32+
client := core.ExtractClient(ctx)
33+
api := container.NewAPI(client)
34+
return api.WaitForNamespace(&container.WaitForNamespaceRequest{
35+
NamespaceID: res.ID,
36+
Region: res.Region,
37+
Timeout: scw.TimeDurationPtr(containerNamespaceActionTimeout),
38+
RetryInterval: core.DefaultRetryInterval,
39+
})
40+
}
41+
42+
return c
43+
}
44+
45+
func containerNamespaceDeleteBuilder(c *core.Command) *core.Command {
46+
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
47+
req := argsI.(*container.DeleteNamespaceRequest)
48+
49+
client := core.ExtractClient(ctx)
50+
api := container.NewAPI(client)
51+
_, err := api.WaitForNamespace(&container.WaitForNamespaceRequest{
52+
NamespaceID: req.NamespaceID,
53+
Region: req.Region,
54+
Timeout: scw.TimeDurationPtr(containerNamespaceActionTimeout),
55+
RetryInterval: core.DefaultRetryInterval,
56+
})
57+
if err != nil {
58+
if core.IsNotFoundError(err) {
59+
return nil, nil
60+
}
61+
62+
return nil, err
63+
}
64+
65+
return nil, nil
66+
}
67+
68+
return c
69+
}

0 commit comments

Comments
 (0)