Skip to content

Commit e350e72

Browse files
committed
fix test
1 parent c0e07f7 commit e350e72

File tree

1 file changed

+39
-14
lines changed

1 file changed

+39
-14
lines changed

internal/namespaces/rdb/v1/custom_acl.go

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ type rdbACLCustomArgs struct {
2424
ACLRuleIPs []scw.IPNet
2525
}
2626

27+
type rdbACLAddCustomArgs struct {
28+
Region scw.Region
29+
InstanceID string
30+
ACLRuleIPs []scw.IPNet
31+
Description string
32+
Descriptions []string
33+
}
34+
2735
type rdbACLAddPosArgs struct {
2836
Region scw.Region
2937
InstanceID string
@@ -51,7 +59,7 @@ func rdbACLCustomResultMarshalerFunc(i any, opt *human.MarshalOpt) (string, erro
5159
}
5260

5361
func aclAddBuilder(c *core.Command) *core.Command {
54-
c.ArgsType = reflect.TypeOf(rdbACLAddPosArgs{})
62+
c.ArgsType = reflect.TypeOf(rdbACLAddCustomArgs{})
5563
c.ArgSpecs = core.ArgSpecs{
5664
{
5765
Name: "acl-rule-ips",
@@ -67,14 +75,19 @@ func aclAddBuilder(c *core.Command) *core.Command {
6775
},
6876
{
6977
Name: "description",
70-
Short: "Description of the ACL rule. Indexes are not yet supported so the description will be applied to all the rules of the command.",
78+
Short: "Description of the ACL rule. If multiple IPs are provided, this description will be applied to all rules unless specific descriptions are provided.",
79+
Required: false,
80+
Positional: false,
81+
},
82+
{
83+
Name: "descriptions",
84+
Short: "Descriptions of the ACL rules",
7185
Required: false,
7286
Positional: false,
7387
},
7488
core.RegionArgSpec(),
7589
}
76-
// Keep legacy behavior: one positional per run; multiple IPs trigger multiple runs
77-
// Do not enable AcceptMultiplePositionalArgs so cobra will run once per positional
90+
c.AcceptMultiplePositionalArgs = true
7891

7992
c.Interceptor = func(ctx context.Context, argsI any, runner core.CommandRunner) (any, error) {
8093
respI, err := runner(ctx, argsI)
@@ -86,30 +99,42 @@ func aclAddBuilder(c *core.Command) *core.Command {
8699
}
87100

88101
c.Run = func(ctx context.Context, argsI any) (i any, e error) {
89-
args := argsI.(*rdbACLAddPosArgs)
102+
args := argsI.(*rdbACLAddCustomArgs)
90103
client := core.ExtractClient(ctx)
91104
api := rdb.NewAPI(client)
92105

93-
desc := args.Description
94-
if desc == "" {
95-
desc = "Allow " + args.ACLRuleIPs.String()
106+
// Build rules with general and specific descriptions
107+
rules := make([]*rdb.ACLRuleRequest, 0, len(args.ACLRuleIPs))
108+
for i, ip := range args.ACLRuleIPs {
109+
description := args.Description
110+
if description == "" {
111+
description = "Allow " + ip.String()
112+
}
113+
if i < len(args.Descriptions) && args.Descriptions[i] != "" {
114+
description = args.Descriptions[i]
115+
}
116+
rules = append(rules, &rdb.ACLRuleRequest{
117+
IP: ip,
118+
Description: description,
119+
})
96120
}
97121

98122
rule, err := api.AddInstanceACLRules(&rdb.AddInstanceACLRulesRequest{
99123
Region: args.Region,
100124
InstanceID: args.InstanceID,
101-
Rules: []*rdb.ACLRuleRequest{{
102-
IP: args.ACLRuleIPs,
103-
Description: desc,
104-
}},
125+
Rules: rules,
105126
}, scw.WithContext(ctx))
106127
if err != nil {
107128
return nil, fmt.Errorf("failed to add ACL rule: %w", err)
108129
}
109130

110131
// Create success message
111132
var message string
112-
message = fmt.Sprintf("ACL rule %s successfully added", args.ACLRuleIPs.String())
133+
if len(args.ACLRuleIPs) == 1 {
134+
message = fmt.Sprintf("ACL rule %s successfully added", args.ACLRuleIPs[0].String())
135+
} else {
136+
message = fmt.Sprintf("%d ACL rules successfully added", len(args.ACLRuleIPs))
137+
}
113138

114139
return &CustomACLResult{
115140
Rules: rule.Rules,
@@ -120,7 +145,7 @@ func aclAddBuilder(c *core.Command) *core.Command {
120145
}
121146

122147
c.WaitFunc = func(ctx context.Context, argsI, respI any) (any, error) {
123-
args := argsI.(*rdbACLAddPosArgs)
148+
args := argsI.(*rdbACLAddCustomArgs)
124149
api := rdb.NewAPI(core.ExtractClient(ctx))
125150

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

0 commit comments

Comments
 (0)