Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rdb): acl set simplified [UI breaking-change] #3597

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/scw/testdata/test-all-usage-rdb-acl-set-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Replace all the ACL rules of a Database Instance.

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

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

FLAGS:
-h, --help help for set
Expand Down
10 changes: 5 additions & 5 deletions docs/commands/rdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,18 @@ Replace all the ACL rules of a Database Instance.
**Usage:**

```
scw rdb acl set [arg=value ...]
scw rdb acl set <acl-rule-ips ...> [arg=value ...]
```


**Args:**

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



Expand Down
1 change: 1 addition & 0 deletions internal/core/cobra_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
argNameWithIndex = fmt.Sprintf("%s.%d", positionalArgSpec.Name, i)
rawArgsWithPositional = rawArgsWithPositional.Add(argNameWithIndex, positionalArgs[i])
}

result, err := run(ctx, cobraCmd, cmd, rawArgsWithPositional)
if err != nil {
return err
Expand Down
53 changes: 50 additions & 3 deletions internal/namespaces/rdb/v1/custom_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,60 @@ func aclDeleteBuilder(c *core.Command) *core.Command {
return c
}

type rdbACLSetCustomArgs struct {
Region scw.Region
InstanceID string
ACLRuleIPs []scw.IPNet
Descriptions []string
}

func aclSetBuilder(c *core.Command) *core.Command {
c.ArgsType = reflect.TypeOf(rdbACLSetCustomArgs{})
c.ArgSpecs = core.ArgSpecs{
{
Name: "acl-rule-ips",
Short: "IP addresses defined in the ACL rules of the Database Instance",
Required: false,
Positional: true,
},
{
Name: "instance-id",
Short: "ID of the Database Instance",
Required: true,
Positional: false,
},
{
Name: "descriptions",
Short: "Descriptions of the ACL rules",
Required: false,
Positional: false,
},
core.RegionArgSpec(),
}
c.AcceptMultiplePositionalArgs = true

c.Run = func(ctx context.Context, argsI interface{}) (i interface{}, e error) {
args := argsI.(*rdb.SetInstanceACLRulesRequest)
args := argsI.(*rdbACLSetCustomArgs)
client := core.ExtractClient(ctx)
api := rdb.NewAPI(client)

rule, err := api.SetInstanceACLRules(args, scw.WithContext(ctx))
aclRules := []*rdb.ACLRuleRequest(nil)
for _, ip := range args.ACLRuleIPs {
aclRules = append(aclRules, &rdb.ACLRuleRequest{
IP: ip,
Description: fmt.Sprintf("Allow %s", ip.String()),
})
}

for i, desc := range args.Descriptions {
aclRules[i].Description = desc
}

rule, err := api.SetInstanceACLRules(&rdb.SetInstanceACLRulesRequest{
Region: args.Region,
InstanceID: args.InstanceID,
Rules: aclRules,
}, scw.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("failed to set ACL rule: %w", err)
}
Expand All @@ -252,7 +299,7 @@ func aclSetBuilder(c *core.Command) *core.Command {
}

c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
args := argsI.(*rdb.SetInstanceACLRulesRequest)
args := argsI.(*rdbACLSetCustomArgs)
api := rdb.NewAPI(core.ExtractClient(ctx))

_, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{
Expand Down
21 changes: 17 additions & 4 deletions internal/namespaces/rdb/v1/custom_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package rdb_test
import (
"testing"

"github.com/scaleway/scaleway-cli/v2/internal/namespaces/rdb/v1"

"github.com/alecthomas/assert"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/rdb/v1"
)

func Test_AddACL(t *testing.T) {
Expand Down Expand Up @@ -113,11 +113,13 @@ func Test_SetACL(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: rdb.GetCommands(),
BeforeFunc: createInstance("PostgreSQL-12"),
Cmd: "scw rdb acl set rules.0.ip=1.2.3.4 instance-id={{ .Instance.ID }} --wait",
Cmd: "scw rdb acl set 1.2.3.4 instance-id={{ .Instance.ID }} descriptions.0=something --wait",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
func(t *testing.T, ctx *core.CheckFuncCtx) {
verifyACL(ctx, t, []string{"1.2.3.4/32"})
acls := ctx.Result.(*rdb.CustomACLResult).Rules
assert.Equal(t, "something", acls[0].Description)
},
),
AfterFunc: deleteInstance(),
Expand All @@ -129,11 +131,22 @@ func Test_SetACL(t *testing.T) {
createInstance("PostgreSQL-12"),
core.ExecBeforeCmd("scw rdb acl add 1.2.3.4 192.168.1.0/32 10.10.10.10 instance-id={{ .Instance.ID }} --wait"),
),
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",
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",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
func(t *testing.T, ctx *core.CheckFuncCtx) {
verifyACL(ctx, t, []string{"1.2.3.4/32", "192.168.1.0/31", "11.11.11.11/32"})
acls := ctx.Result.(*rdb.CustomACLResult).Rules
for _, acl := range acls {
switch acl.IP.String() {
case "1.2.3.4/32":
assert.Equal(t, "first", acl.Description)
case "192.168.1.0/31":
assert.Equal(t, "second", acl.Description)
case "11.11.11.11/32":
assert.Equal(t, "third", acl.Description)
}
}
},
),
AfterFunc: deleteInstance(),
Expand Down
Loading
Loading