Skip to content

Commit 77ff45b

Browse files
yfodilremyleone
andauthored
feat(rdb): rename ACL add/delete arguments (#2571)
Co-authored-by: Rémy Léone <rleone@scaleway.com>
1 parent 77118eb commit 77ff45b

9 files changed

+713
-284
lines changed

cmd/scw/testdata/test-all-usage-rdb-acl-add-usage.golden

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

1414
FLAGS:
1515
-h, --help help for add
16+
-w, --wait wait until the acl is ready
1617

1718
GLOBAL FLAGS:
1819
-c, --config string The path to the config file

cmd/scw/testdata/test-all-usage-rdb-acl-delete-usage.golden

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ USAGE:
66
scw rdb acl delete [arg=value ...]
77

88
ARGS:
9-
instance-id UUID of the instance you want to delete an ACL rules from
10-
acl-rule-ips.{index} ACL rules IP present on the instance
11-
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
9+
instance-id UUID of the instance you want to delete an ACL rules from
10+
rules.{index}.ip ACL rules IP present on the instance
11+
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
1212

1313
FLAGS:
1414
-h, --help help for delete
15+
-w, --wait wait until the acl is ready
1516

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

docs/commands/rdb.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ scw rdb acl delete [arg=value ...]
111111
| Name | | Description |
112112
|------|---|-------------|
113113
| instance-id | Required | UUID of the instance you want to delete an ACL rules from |
114-
| acl-rule-ips.{index} | Required | ACL rules IP present on the instance |
114+
| rules.{index}.ip | Required | ACL rules IP present on the instance |
115115
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |
116116

117117

internal/namespaces/rdb/v1/custom_acl.go

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package rdb
22

33
import (
44
"context"
5+
"reflect"
56

67
"github.com/fatih/color"
78
"github.com/scaleway/scaleway-cli/v2/internal/core"
89
"github.com/scaleway/scaleway-cli/v2/internal/human"
910
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
11+
"github.com/scaleway/scaleway-sdk-go/scw"
1012
)
1113

1214
var (
@@ -17,30 +19,101 @@ var (
1719
)
1820

1921
func aclAddBuilder(c *core.Command) *core.Command {
22+
type customAddACLRequest struct {
23+
*rdb.AddInstanceACLRulesRequest
24+
Rules []*rdb.ACLRuleRequest
25+
}
26+
27+
c.ArgSpecs.GetByName("rules.{index}.ip").Name = "rules.{index}.ip"
28+
c.ArgSpecs.GetByName("rules.{index}.description").Name = "rules.{index}.description"
29+
30+
c.ArgsType = reflect.TypeOf(customAddACLRequest{})
31+
2032
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
21-
aclAddResponseI, err := runner(ctx, argsI)
33+
args := argsI.(*customAddACLRequest)
34+
35+
request := args.AddInstanceACLRulesRequest
36+
request.Rules = args.Rules
37+
38+
aclAddResponseI, err := runner(ctx, request)
2239
if err != nil {
2340
return nil, err
2441
}
42+
2543
aclAddResponse := aclAddResponseI.(*rdb.AddInstanceACLRulesResponse)
2644
return aclAddResponse.Rules, nil
2745
}
2846

47+
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
48+
args := argsI.(*customAddACLRequest)
49+
50+
api := rdb.NewAPI(core.ExtractClient(ctx))
51+
_, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{
52+
InstanceID: args.InstanceID,
53+
Region: args.Region,
54+
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
55+
RetryInterval: core.DefaultRetryInterval,
56+
})
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return respI.([]*rdb.ACLRule), nil
62+
}
63+
2964
return c
3065
}
3166

3267
func aclDeleteBuilder(c *core.Command) *core.Command {
68+
type deleteRules struct {
69+
IP string `json:"ip"`
70+
}
71+
72+
type customDeleteACLRequest struct {
73+
*rdb.DeleteInstanceACLRulesRequest
74+
Rules []deleteRules
75+
}
76+
77+
c.ArgSpecs.GetByName("acl-rule-ips.{index}").Name = "rules.{index}.ip"
78+
79+
c.ArgsType = reflect.TypeOf(customDeleteACLRequest{})
80+
3381
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
34-
aclDeleteResponseI, err := runner(ctx, argsI)
82+
args := argsI.(*customDeleteACLRequest)
83+
84+
request := args.DeleteInstanceACLRulesRequest
85+
for _, ip := range args.Rules {
86+
request.ACLRuleIPs = append(request.ACLRuleIPs, ip.IP)
87+
}
88+
89+
aclDeleteResponseI, err := runner(ctx, request)
3590
if err != nil {
3691
return nil, err
3792
}
93+
3894
aclDeleteResponse := aclDeleteResponseI.(*rdb.DeleteInstanceACLRulesResponse)
3995
return rdb.ListInstanceACLRulesResponse{
4096
Rules: aclDeleteResponse.Rules,
4197
TotalCount: uint32(len(aclDeleteResponse.Rules)),
4298
}, nil
4399
}
44100

101+
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
102+
args := argsI.(*customDeleteACLRequest)
103+
104+
api := rdb.NewAPI(core.ExtractClient(ctx))
105+
_, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{
106+
InstanceID: args.InstanceID,
107+
Region: args.Region,
108+
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
109+
RetryInterval: core.DefaultRetryInterval,
110+
})
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
return respI.(rdb.ListInstanceACLRulesResponse), nil
116+
}
117+
45118
return c
46119
}

internal/namespaces/rdb/v1/custom_acl_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func Test_AddACL(t *testing.T) {
1010
t.Run("Simple", core.Test(&core.TestConfig{
1111
Commands: GetCommands(),
1212
BeforeFunc: createInstance("PostgreSQL-12"),
13-
Cmd: "scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=4.2.3.4",
13+
Cmd: "scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=4.2.3.4 --wait",
1414
Check: core.TestCheckGolden(),
1515
AfterFunc: deleteInstance(),
1616
}))
@@ -21,9 +21,9 @@ func Test_DeleteACL(t *testing.T) {
2121
Commands: GetCommands(),
2222
BeforeFunc: core.BeforeFuncCombine(
2323
createInstance("PostgreSQL-12"),
24-
core.ExecBeforeCmd("scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=1.2.3.4"),
24+
core.ExecBeforeCmd("scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=1.2.3.4 --wait"),
2525
),
26-
Cmd: "scw rdb acl remove instance-id={{ .Instance.ID }} acl-rule-ips.0=1.2.3.4",
26+
Cmd: "scw rdb acl delete instance-id={{ .Instance.ID }} rules.0.ip=1.2.3.4 --wait",
2727
Check: core.TestCheckGolden(),
2828
AfterFunc: deleteInstance(),
2929
}))

0 commit comments

Comments
 (0)