Skip to content

Commit b91eb46

Browse files
committed
feat(rdb): acl: set with custom args but not positional yet
1 parent 50c4ff4 commit b91eb46

File tree

8 files changed

+1031
-1480
lines changed

8 files changed

+1031
-1480
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
@@ -187,10 +187,9 @@ scw rdb acl set [arg=value ...]
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.{index} | | IP addresses defined in the ACL rules of the Database Instance |
191+
| instance-id | Required | ID of the Database Instance |
192+
| region | Default: `fr-par` | Region to target. If none is passed will use default region from the config |
194193

195194

196195

internal/namespaces/rdb/v1/custom_acl.go

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

235+
type rdbACLSetCustomArgs struct {
236+
Region scw.Region
237+
InstanceID string
238+
ACLRulesIPs []scw.IPNet
239+
}
240+
235241
func aclSetBuilder(c *core.Command) *core.Command {
242+
c.ArgsType = reflect.TypeOf(rdbACLSetCustomArgs{})
243+
c.ArgSpecs = core.ArgSpecs{
244+
{
245+
Name: "acl-rule-ips.{index}",
246+
Short: "IP addresses defined in the ACL rules of the Database Instance",
247+
Required: false,
248+
Positional: false,
249+
},
250+
{
251+
Name: "instance-id",
252+
Short: "ID of the Database Instance",
253+
Required: true,
254+
Positional: false,
255+
},
256+
core.RegionArgSpec(),
257+
}
258+
236259
c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
237-
args := argsI.(*rdb.SetInstanceACLRulesRequest)
260+
args := argsI.(*rdbACLSetCustomArgs)
238261
client := core.ExtractClient(ctx)
239262
api := rdb.NewAPI(client)
240263

241-
rule, err := api.SetInstanceACLRules(args, scw.WithContext(ctx))
264+
aclRules := []*rdb.ACLRuleRequest(nil)
265+
for _, ip := range args.ACLRulesIPs {
266+
aclRules = append(aclRules, &rdb.ACLRuleRequest{
267+
IP: ip,
268+
Description: fmt.Sprintf("Allow %s", ip.String()),
269+
})
270+
}
271+
272+
rule, err := api.SetInstanceACLRules(&rdb.SetInstanceACLRulesRequest{
273+
Region: args.Region,
274+
InstanceID: args.InstanceID,
275+
Rules: aclRules,
276+
}, scw.WithContext(ctx))
242277
if err != nil {
243278
return nil, fmt.Errorf("failed to set ACL rule: %w", err)
244279
}
@@ -252,7 +287,7 @@ func aclSetBuilder(c *core.Command) *core.Command {
252287
}
253288

254289
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
255-
args := argsI.(*rdb.SetInstanceACLRulesRequest)
290+
args := argsI.(*rdbACLSetCustomArgs)
256291
api := rdb.NewAPI(core.ExtractClient(ctx))
257292

258293
_, 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
@@ -113,7 +113,7 @@ 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 acl-rule-ips.0=1.2.3.4 instance-id={{ .Instance.ID }} --wait",
117117
Check: core.TestCheckCombine(
118118
core.TestCheckGolden(),
119119
func(t *testing.T, ctx *core.CheckFuncCtx) {
@@ -129,7 +129,7 @@ func Test_SetACL(t *testing.T) {
129129
createInstance("PostgreSQL-12"),
130130
core.ExecBeforeCmd("scw rdb acl add 1.2.3.4 192.168.1.0/32 10.10.10.10 instance-id={{ .Instance.ID }} --wait"),
131131
),
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",
132+
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",
133133
Check: core.TestCheckCombine(
134134
core.TestCheckGolden(),
135135
func(t *testing.T, ctx *core.CheckFuncCtx) {

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

Lines changed: 582 additions & 748 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 10559 tcp inbound allow IP allowed
6-
11.11.11.11/32 10559 tcp inbound allow IP allowed
7-
192.168.1.0/31 10559 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": 10559,
14-
"protocol": "tcp",
15-
"direction": "inbound",
16-
"action": "allow",
17-
"description": "IP allowed"
18-
},
19-
{
20-
"ip": "11.11.11.11/32",
21-
"port": 10559,
22-
"protocol": "tcp",
23-
"direction": "inbound",
24-
"action": "allow",
25-
"description": "IP allowed"
26-
},
27-
{
28-
"ip": "192.168.1.0/31",
29-
"port": 10559,
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)