Skip to content

Commit 8da30ab

Browse files
committed
feat(rdb): acl: set with custom args but not positional yet
1 parent d0ac1b1 commit 8da30ab

File tree

8 files changed

+582
-1095
lines changed

8 files changed

+582
-1095
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ USAGE:
66
scw rdb acl set [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.{index}] IP addresses defined in the ACL rules of the Database Instance
10+
instance-id ID of the Database Instance
11+
[region=fr-par] Region to target. If none is passed will use default region from the config
1312

1413
FLAGS:
1514
-h, --help help for set

docs/commands/rdb.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,9 @@ scw rdb acl set [arg=value ...]
185185

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

193192

194193

internal/namespaces/rdb/v1/custom_acl.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,48 @@ func aclDeleteBuilder(c *core.Command) *core.Command {
245245
return c
246246
}
247247

248+
type rdbACLSetCustomArgs struct {
249+
Region scw.Region
250+
InstanceID string
251+
ACLRulesIPs []scw.IPNet
252+
}
253+
248254
func aclSetBuilder(c *core.Command) *core.Command {
255+
c.ArgsType = reflect.TypeOf(rdbACLSetCustomArgs{})
256+
c.ArgSpecs = core.ArgSpecs{
257+
{
258+
Name: "acl-rule-ips.{index}",
259+
Short: "IP addresses defined in the ACL rules of the Database Instance",
260+
Required: false,
261+
Positional: false,
262+
},
263+
{
264+
Name: "instance-id",
265+
Short: "ID of the Database Instance",
266+
Required: true,
267+
Positional: false,
268+
},
269+
core.RegionArgSpec(),
270+
}
271+
249272
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
250-
args := argsI.(*rdb.SetInstanceACLRulesRequest)
273+
args := argsI.(*rdbACLSetCustomArgs)
251274
client := core.ExtractClient(ctx)
252275
api := rdb.NewAPI(client)
253276

254-
rule, err := api.SetInstanceACLRules(args, scw.WithContext(ctx))
277+
aclRules := []*rdb.ACLRuleRequest(nil)
278+
for _, ip := range args.ACLRulesIPs {
279+
aclRules = append(aclRules, &rdb.ACLRuleRequest{
280+
IP: ip,
281+
Description: fmt.Sprintf("Allow %s", ip.String()),
282+
})
283+
}
284+
285+
rule, err := api.SetInstanceACLRules(&rdb.SetInstanceACLRulesRequest{
286+
Region: args.Region,
287+
InstanceID: args.InstanceID,
288+
Rules: aclRules,
289+
}, scw.WithContext(ctx))
255290
if err != nil {
256291
return nil, fmt.Errorf("failed to set ACL rule: %w", err)
257292
}
@@ -265,7 +300,7 @@ func aclSetBuilder(c *core.Command) *core.Command {
265300
}
266301

267302
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
268-
args := argsI.(*rdb.SetInstanceACLRulesRequest)
303+
args := argsI.(*rdbACLSetCustomArgs)
269304
api := rdb.NewAPI(core.ExtractClient(ctx))
270305

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

internal/namespaces/rdb/v1/custom_acl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func Test_SetACL(t *testing.T) {
8585
t.Run("Simple", core.Test(&core.TestConfig{
8686
Commands: GetCommands(),
8787
BeforeFunc: createInstance("PostgreSQL-12"),
88-
Cmd: "scw rdb acl set rules.0.ip=1.2.3.4 instance-id={{ .Instance.ID }} --wait",
88+
Cmd: "scw rdb acl set acl-rule-ips.0=1.2.3.4 instance-id={{ .Instance.ID }} --wait",
8989
Check: core.TestCheckCombine(
9090
core.TestCheckGolden(),
9191
func(t *testing.T, ctx *core.CheckFuncCtx) {
@@ -101,7 +101,7 @@ func Test_SetACL(t *testing.T) {
101101
createInstance("PostgreSQL-12"),
102102
core.ExecBeforeCmd("scw rdb acl add 1.2.3.4 192.168.1.0/32 10.10.10.10 instance-id={{ .Instance.ID }} --wait"),
103103
),
104-
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",
104+
Cmd: "scw rdb acl set acl-rule-ips.0=1.2.3.4 acl-rule-ips.1=192.168.1.0/31 acl-rule-ips.2=11.11.11.11 instance-id={{ .Instance.ID }} --wait",
105105
Check: core.TestCheckCombine(
106106
core.TestCheckGolden(),
107107
func(t *testing.T, ctx *core.CheckFuncCtx) {

internal/namespaces/rdb/v1/testdata/test-set-acl-multiple.cassette.yaml

Lines changed: 325 additions & 553 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,12 @@
1-
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2-
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3-
✅ ACL rules successfully set.
4-
IP PORT PROTOCOL DIRECTION ACTION DESCRIPTION
5-
1.2.3.4/32 13729 tcp inbound allow IP allowed
6-
11.11.11.11/32 13729 tcp inbound allow IP allowed
7-
192.168.1.0/31 13729 tcp inbound allow IP allowed
8-
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
1+
🎲🎲🎲 EXIT CODE: 1 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
Unknown argument 'acl-rule-ips.0'
4+
5+
Hint:
6+
Valid arguments are: acl-rule-ips.{index}, instance-id, region
7+
🟥🟥🟥 JSON STDERR 🟥🟥🟥
98
{
10-
"Rules": [
11-
{
12-
"ip": "1.2.3.4/32",
13-
"port": 13729,
14-
"protocol": "tcp",
15-
"direction": "inbound",
16-
"action": "allow",
17-
"description": "IP allowed"
18-
},
19-
{
20-
"ip": "11.11.11.11/32",
21-
"port": 13729,
22-
"protocol": "tcp",
23-
"direction": "inbound",
24-
"action": "allow",
25-
"description": "IP allowed"
26-
},
27-
{
28-
"ip": "192.168.1.0/31",
29-
"port": 13729,
30-
"protocol": "tcp",
31-
"direction": "inbound",
32-
"action": "allow",
33-
"description": "IP allowed"
34-
}
35-
],
36-
"Success": {
37-
"message": "ACL rules successfully set",
38-
"details": ""
39-
}
9+
"message": "unknown argument 'acl-rule-ips.0'",
10+
"error": {},
11+
"hint": "Valid arguments are: acl-rule-ips.{index}, instance-id, region"
4012
}

0 commit comments

Comments
 (0)