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): rename ACL add/delete arguments #2571

Merged
merged 15 commits into from
Dec 5, 2022
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
1 change: 1 addition & 0 deletions cmd/scw/testdata/test-all-usage-rdb-acl-add-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ARGS:

FLAGS:
-h, --help help for add
-w, --wait wait until the acl is ready

GLOBAL FLAGS:
-c, --config string The path to the config file
Expand Down
7 changes: 4 additions & 3 deletions cmd/scw/testdata/test-all-usage-rdb-acl-delete-usage.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ USAGE:
scw rdb acl delete [arg=value ...]

ARGS:
instance-id UUID of the instance you want to delete an ACL rules from
acl-rule-ips.{index} ACL rules IP present on the instance
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
instance-id UUID of the instance you want to delete an ACL rules from
rules.{index}.ip ACL rules IP present on the instance
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)

FLAGS:
-h, --help help for delete
-w, --wait wait until the acl is ready

GLOBAL FLAGS:
-c, --config string The path to the config file
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/rdb.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ scw rdb acl delete [arg=value ...]
| Name | | Description |
|------|---|-------------|
| instance-id | Required | UUID of the instance you want to delete an ACL rules from |
| acl-rule-ips.{index} | Required | ACL rules IP present on the instance |
| rules.{index}.ip | Required | ACL rules IP present on the instance |
| 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 |


Expand Down
77 changes: 75 additions & 2 deletions internal/namespaces/rdb/v1/custom_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package rdb

import (
"context"
"reflect"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/human"
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

var (
Expand All @@ -17,30 +19,101 @@ var (
)

func aclAddBuilder(c *core.Command) *core.Command {
type customAddACLRequest struct {
*rdb.AddInstanceACLRulesRequest
Rules []*rdb.ACLRuleRequest
}

c.ArgSpecs.GetByName("rules.{index}.ip").Name = "rules.{index}.ip"
c.ArgSpecs.GetByName("rules.{index}.description").Name = "rules.{index}.description"

c.ArgsType = reflect.TypeOf(customAddACLRequest{})

c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
aclAddResponseI, err := runner(ctx, argsI)
args := argsI.(*customAddACLRequest)

request := args.AddInstanceACLRulesRequest
request.Rules = args.Rules

aclAddResponseI, err := runner(ctx, request)
if err != nil {
return nil, err
}

aclAddResponse := aclAddResponseI.(*rdb.AddInstanceACLRulesResponse)
return aclAddResponse.Rules, nil
}

c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
args := argsI.(*customAddACLRequest)

api := rdb.NewAPI(core.ExtractClient(ctx))
_, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: args.InstanceID,
Region: args.Region,
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
return nil, err
}

return respI.([]*rdb.ACLRule), nil
}

return c
}

func aclDeleteBuilder(c *core.Command) *core.Command {
type deleteRules struct {
IP string `json:"ip"`
}

type customDeleteACLRequest struct {
*rdb.DeleteInstanceACLRulesRequest
Rules []deleteRules
}

c.ArgSpecs.GetByName("acl-rule-ips.{index}").Name = "rules.{index}.ip"

c.ArgsType = reflect.TypeOf(customDeleteACLRequest{})

c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
aclDeleteResponseI, err := runner(ctx, argsI)
args := argsI.(*customDeleteACLRequest)

request := args.DeleteInstanceACLRulesRequest
for _, ip := range args.Rules {
request.ACLRuleIPs = append(request.ACLRuleIPs, ip.IP)
}

aclDeleteResponseI, err := runner(ctx, request)
if err != nil {
return nil, err
}

aclDeleteResponse := aclDeleteResponseI.(*rdb.DeleteInstanceACLRulesResponse)
return rdb.ListInstanceACLRulesResponse{
Rules: aclDeleteResponse.Rules,
TotalCount: uint32(len(aclDeleteResponse.Rules)),
}, nil
}

c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
args := argsI.(*customDeleteACLRequest)

api := rdb.NewAPI(core.ExtractClient(ctx))
_, err := api.WaitForInstance(&rdb.WaitForInstanceRequest{
InstanceID: args.InstanceID,
Region: args.Region,
Timeout: scw.TimeDurationPtr(instanceActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
return nil, err
}

return respI.(rdb.ListInstanceACLRulesResponse), nil
}

return c
}
6 changes: 3 additions & 3 deletions internal/namespaces/rdb/v1/custom_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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",
Cmd: "scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=4.2.3.4 --wait",
Check: core.TestCheckGolden(),
AfterFunc: deleteInstance(),
}))
Expand All @@ -21,9 +21,9 @@ func Test_DeleteACL(t *testing.T) {
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"),
core.ExecBeforeCmd("scw rdb acl add instance-id={{ .Instance.ID }} rules.0.ip=1.2.3.4 --wait"),
),
Cmd: "scw rdb acl remove instance-id={{ .Instance.ID }} acl-rule-ips.0=1.2.3.4",
Cmd: "scw rdb acl delete instance-id={{ .Instance.ID }} rules.0.ip=1.2.3.4 --wait",
Check: core.TestCheckGolden(),
AfterFunc: deleteInstance(),
}))
Expand Down
Loading