Skip to content

Commit 0d06554

Browse files
authored
feat(rdb): acl set simplified [UI breaking-change] (#3597)
1 parent e2e9309 commit 0d06554

File tree

9 files changed

+421
-1552
lines changed

9 files changed

+421
-1552
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
Replace all the ACL rules of a Database Instance.
44

55
USAGE:
6-
scw rdb acl set [arg=value ...]
6+
scw rdb acl set <acl-rule-ips ...> [arg=value ...]
77

88
ARGS:
9-
instance-id UUID of the Database Instance where the ACL rules must be set
10-
[rules.{index}.ip]
11-
[rules.{index}.description]
12-
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
9+
acl-rule-ips IP addresses defined in the ACL rules of the Database Instance
10+
instance-id ID of the Database Instance
11+
[descriptions] Descriptions of the ACL rules
12+
[region=fr-par] Region to target. If none is passed will use default region from the config
1313

1414
FLAGS:
1515
-h, --help help for set

docs/commands/rdb.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,18 @@ Replace all the ACL rules of a Database Instance.
179179
**Usage:**
180180

181181
```
182-
scw rdb acl set [arg=value ...]
182+
scw rdb acl set <acl-rule-ips ...> [arg=value ...]
183183
```
184184

185185

186186
**Args:**
187187

188188
| Name | | Description |
189189
|------|---|-------------|
190-
| instance-id | Required | UUID of the Database Instance where the ACL rules must be set |
191-
| rules.{index}.ip | | |
192-
| rules.{index}.description | | |
193-
| 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 |
190+
| acl-rule-ips | | IP addresses defined in the ACL rules of the Database Instance |
191+
| instance-id | Required | ID of the Database Instance |
192+
| descriptions | | Descriptions of the ACL rules |
193+
| region | Default: `fr-par` | Region to target. If none is passed will use default region from the config |
194194

195195

196196

internal/core/cobra_utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
8282
argNameWithIndex = fmt.Sprintf("%s.%d", positionalArgSpec.Name, i)
8383
rawArgsWithPositional = rawArgsWithPositional.Add(argNameWithIndex, positionalArgs[i])
8484
}
85+
8586
result, err := run(ctx, cobraCmd, cmd, rawArgsWithPositional)
8687
if err != nil {
8788
return err

internal/namespaces/rdb/v1/custom_acl.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,60 @@ func aclDeleteBuilder(c *core.Command) *core.Command {
232232
return c
233233
}
234234

235+
type rdbACLSetCustomArgs struct {
236+
Region scw.Region
237+
InstanceID string
238+
ACLRuleIPs []scw.IPNet
239+
Descriptions []string
240+
}
241+
235242
func aclSetBuilder(c *core.Command) *core.Command {
243+
c.ArgsType = reflect.TypeOf(rdbACLSetCustomArgs{})
244+
c.ArgSpecs = core.ArgSpecs{
245+
{
246+
Name: "acl-rule-ips",
247+
Short: "IP addresses defined in the ACL rules of the Database Instance",
248+
Required: false,
249+
Positional: true,
250+
},
251+
{
252+
Name: "instance-id",
253+
Short: "ID of the Database Instance",
254+
Required: true,
255+
Positional: false,
256+
},
257+
{
258+
Name: "descriptions",
259+
Short: "Descriptions of the ACL rules",
260+
Required: false,
261+
Positional: false,
262+
},
263+
core.RegionArgSpec(),
264+
}
265+
c.AcceptMultiplePositionalArgs = true
266+
236267
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
237-
args := argsI.(*rdb.SetInstanceACLRulesRequest)
268+
args := argsI.(*rdbACLSetCustomArgs)
238269
client := core.ExtractClient(ctx)
239270
api := rdb.NewAPI(client)
240271

241-
rule, err := api.SetInstanceACLRules(args, scw.WithContext(ctx))
272+
aclRules := []*rdb.ACLRuleRequest(nil)
273+
for _, ip := range args.ACLRuleIPs {
274+
aclRules = append(aclRules, &rdb.ACLRuleRequest{
275+
IP: ip,
276+
Description: fmt.Sprintf("Allow %s", ip.String()),
277+
})
278+
}
279+
280+
for i, desc := range args.Descriptions {
281+
aclRules[i].Description = desc
282+
}
283+
284+
rule, err := api.SetInstanceACLRules(&rdb.SetInstanceACLRulesRequest{
285+
Region: args.Region,
286+
InstanceID: args.InstanceID,
287+
Rules: aclRules,
288+
}, scw.WithContext(ctx))
242289
if err != nil {
243290
return nil, fmt.Errorf("failed to set ACL rule: %w", err)
244291
}
@@ -252,7 +299,7 @@ func aclSetBuilder(c *core.Command) *core.Command {
252299
}
253300

254301
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
255-
args := argsI.(*rdb.SetInstanceACLRulesRequest)
302+
args := argsI.(*rdbACLSetCustomArgs)
256303
api := rdb.NewAPI(core.ExtractClient(ctx))
257304

258305
_, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{

internal/namespaces/rdb/v1/custom_acl_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package rdb_test
33
import (
44
"testing"
55

6-
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/rdb/v1"
7-
6+
"github.com/alecthomas/assert"
87
"github.com/scaleway/scaleway-cli/v2/internal/core"
8+
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/rdb/v1"
99
)
1010

1111
func Test_AddACL(t *testing.T) {
@@ -113,11 +113,13 @@ func Test_SetACL(t *testing.T) {
113113
t.Run("Simple", core.Test(&core.TestConfig{
114114
Commands: rdb.GetCommands(),
115115
BeforeFunc: createInstance("PostgreSQL-12"),
116-
Cmd: "scw rdb acl set rules.0.ip=1.2.3.4 instance-id={{ .Instance.ID }} --wait",
116+
Cmd: "scw rdb acl set 1.2.3.4 instance-id={{ .Instance.ID }} descriptions.0=something --wait",
117117
Check: core.TestCheckCombine(
118118
core.TestCheckGolden(),
119119
func(t *testing.T, ctx *core.CheckFuncCtx) {
120120
verifyACL(ctx, t, []string{"1.2.3.4/32"})
121+
acls := ctx.Result.(*rdb.CustomACLResult).Rules
122+
assert.Equal(t, "something", acls[0].Description)
121123
},
122124
),
123125
AfterFunc: deleteInstance(),
@@ -129,11 +131,22 @@ func Test_SetACL(t *testing.T) {
129131
createInstance("PostgreSQL-12"),
130132
core.ExecBeforeCmd("scw rdb acl add 1.2.3.4 192.168.1.0/32 10.10.10.10 instance-id={{ .Instance.ID }} --wait"),
131133
),
132-
Cmd: "scw rdb acl set rules.0.ip=1.2.3.4 rules.1.ip=192.168.1.0/31 rules.2.ip=11.11.11.11 instance-id={{ .Instance.ID }} --wait",
134+
Cmd: "scw rdb acl set 1.2.3.4 192.168.1.0/31 11.11.11.11 instance-id={{ .Instance.ID }} descriptions.0=first descriptions.1=second descriptions.2=third --wait",
133135
Check: core.TestCheckCombine(
134136
core.TestCheckGolden(),
135137
func(t *testing.T, ctx *core.CheckFuncCtx) {
136138
verifyACL(ctx, t, []string{"1.2.3.4/32", "192.168.1.0/31", "11.11.11.11/32"})
139+
acls := ctx.Result.(*rdb.CustomACLResult).Rules
140+
for _, acl := range acls {
141+
switch acl.IP.String() {
142+
case "1.2.3.4/32":
143+
assert.Equal(t, "first", acl.Description)
144+
case "192.168.1.0/31":
145+
assert.Equal(t, "second", acl.Description)
146+
case "11.11.11.11/32":
147+
assert.Equal(t, "third", acl.Description)
148+
}
149+
}
137150
},
138151
),
139152
AfterFunc: deleteInstance(),

0 commit comments

Comments
 (0)