Skip to content

Commit

Permalink
feat(rdb): add nice human marshalling for add/delete rules (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Aug 24, 2020
1 parent cf96cc5 commit cf95c6a
Show file tree
Hide file tree
Showing 7 changed files with 1,251 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/namespaces/rdb/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func GetCommands() *core.Commands {
instanceConnectCommand(),
backupWaitCommand(),
))
cmds.MustFind("rdb", "acl", "add").Override(aclAddBuilder)
cmds.MustFind("rdb", "acl", "delete").Override(aclDeleteBuilder)

cmds.MustFind("rdb", "backup", "create").Override(backupCreateBuilder)
cmds.MustFind("rdb", "backup", "export").Override(backupExportBuilder)
cmds.MustFind("rdb", "backup", "restore").Override(backupRestoreBuilder)
Expand Down
32 changes: 32 additions & 0 deletions internal/namespaces/rdb/v1/custom_acl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package rdb

import (
"context"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/human"
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
)
Expand All @@ -12,3 +15,32 @@ var (
rdb.ACLRuleActionDeny: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "deny"},
}
)

func aclAddBuilder(c *core.Command) *core.Command {
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
aclAddResponseI, err := runner(ctx, argsI)
if err != nil {
return nil, err
}
aclAddResponse := aclAddResponseI.(*rdb.AddInstanceACLRulesResponse)
return aclAddResponse.Rules, nil
}

return c
}

func aclDeleteBuilder(c *core.Command) *core.Command {
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
aclDeleteResponseI, err := runner(ctx, argsI)
if err != nil {
return nil, err
}
aclDeleteResponse := aclDeleteResponseI.(*rdb.DeleteInstanceACLRulesResponse)
return rdb.ListInstanceACLRulesResponse{
Rules: aclDeleteResponse.Rules,
TotalCount: uint32(len(aclDeleteResponse.Rules)),
}, nil
}

return c
}
30 changes: 30 additions & 0 deletions internal/namespaces/rdb/v1/custom_acl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package rdb

import (
"testing"

"github.com/scaleway/scaleway-cli/internal/core"
)

func Test_AddACL(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: createInstance("PostgreSQL-12"),
Cmd: "scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=4.2.3.4",
Check: core.TestCheckGolden(),
AfterFunc: deleteInstance(),
}))
}

func Test_DeleteACL(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createInstance("PostgreSQL-12"),
core.ExecBeforeCmd("scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=1.2.3.4"),
),
Cmd: "scw rdb acl remove instance-id={{ .Instance.ID }} acl-rule-ips.0=1.2.3.4",
Check: core.TestCheckGolden(),
AfterFunc: deleteInstance(),
}))
}
Loading

0 comments on commit cf95c6a

Please sign in to comment.