From e7baca88dc00fa86b57fcffe22aaa63741f27308 Mon Sep 17 00:00:00 2001 From: protobuf-ci-cd Date: Thu, 26 Jun 2025 15:41:51 +0200 Subject: [PATCH 1/5] feat(redis): update acl rules --- docs/commands/autocomplete.md | 2 +- docs/commands/redis.md | 24 ++++ internal/namespaces/redis/v1/custom.go | 2 + internal/namespaces/redis/v1/custom_acl.go | 127 +++++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 internal/namespaces/redis/v1/custom_acl.go diff --git a/docs/commands/autocomplete.md b/docs/commands/autocomplete.md index 95f1777fb1..bb92628259 100644 --- a/docs/commands/autocomplete.md +++ b/docs/commands/autocomplete.md @@ -45,7 +45,7 @@ scw autocomplete script [arg=value ...] | Name | | Description | |------|---|-------------| -| shell | Default: `/bin/bash` | | +| shell | Default: `/bin/zsh` | | | basename | Default: `` | | diff --git a/docs/commands/redis.md b/docs/commands/redis.md index 10ce5b3185..185e257f49 100644 --- a/docs/commands/redis.md +++ b/docs/commands/redis.md @@ -7,6 +7,7 @@ This API allows you to manage your Managed Databases for Redis™. - [Delete an ACL rule for a cluster](#delete-an-acl-rule-for-a-cluster) - [Get an ACL rule](#get-an-acl-rule) - [Set ACL rules for a cluster](#set-acl-rules-for-a-cluster) + - [Update an ACL rule for a Redis™ Database Instance (network rule)](#update-an-acl-rule-for-a-redis™-database-instance-(network-rule)) - [Cluster management commands](#cluster-management-commands) - [Create a Redis™ Database Instance](#create-a-redis™-database-instance) - [Delete a Redis™ Database Instance](#delete-a-redis™-database-instance) @@ -123,6 +124,29 @@ scw redis acl set [arg=value ...] +### Update an ACL rule for a Redis™ Database Instance (network rule) + +Update an ACL rule (IP/description) for a Redis™ Database Instance (Redis™ cluster). This command simulates an update by fetching, deleting, and re-adding the rule. + +**Usage:** + +``` +scw redis acl update [arg=value ...] +``` + + +**Args:** + +| Name | | Description | +|------|---|-------------| +| cluster-id | Required | UUID of the Redis cluster | +| acl-id | Required | UUID of the ACL rule to update | +| ip-cidr | | New IPv4 network address of the rule (optional, defaults to current) | +| description | | New description of the rule (optional, defaults to current) | +| zone | Default: `fr-par-1`
One of: `fr-par-1`, `fr-par-2`, `nl-ams-1`, `nl-ams-2`, `pl-waw-1`, `pl-waw-2` | Zone to target. If none is passed will use default zone from the config | + + + ## Cluster management commands A Redis™ Database Instance, also known as a Redis™ cluster, consists of either one standalone node or a cluster composed of three to six nodes. The cluster uses partitioning to split the keyspace. Each partition is replicated and can be reassigned or elected as the primary when necessary. Standalone mode creates a standalone database provisioned on a single node. diff --git a/internal/namespaces/redis/v1/custom.go b/internal/namespaces/redis/v1/custom.go index f3579a3221..50c46b3ec7 100644 --- a/internal/namespaces/redis/v1/custom.go +++ b/internal/namespaces/redis/v1/custom.go @@ -20,5 +20,7 @@ func GetCommands() *core.Commands { cmds.MustFind("redis", "setting", "add").Override(redisSettingAddBuilder) cmds.MustFind("redis", "cluster", "migrate").Override(redisClusterMigrateBuilder) + cmds.Merge(core.NewCommands(redisACLUpdateCommand())) + return cmds } diff --git a/internal/namespaces/redis/v1/custom_acl.go b/internal/namespaces/redis/v1/custom_acl.go new file mode 100644 index 0000000000..6a9fbf1508 --- /dev/null +++ b/internal/namespaces/redis/v1/custom_acl.go @@ -0,0 +1,127 @@ +package redis + +import ( + "context" + "fmt" + "net" + "reflect" + + "github.com/scaleway/scaleway-cli/v2/core" + "github.com/scaleway/scaleway-sdk-go/api/redis/v1" + "github.com/scaleway/scaleway-sdk-go/scw" +) + +func redisACLUpdateCommand() *core.Command { + return &core.Command{ + Short: "Update an ACL rule for a Redis™ Database Instance (network rule)", + Long: "Update an ACL rule (IP/description) for a Redis™ Database Instance (Redis™ cluster). This command simulates an update by fetching, deleting, and re-adding the rule.", + Namespace: "redis", + Resource: "acl", + Verb: "update", + ArgsType: reflect.TypeOf(redisUpdateACLRuleRequest{}), + ArgSpecs: core.ArgSpecs{ + { + Name: "cluster-id", + Short: "UUID of the Redis cluster", + Required: true, + Positional: false, + }, + { + Name: "acl-id", + Short: "UUID of the ACL rule to update", + Required: true, + Positional: true, + }, + { + Name: "ip-cidr", + Short: "New IPv4 network address of the rule (optional, defaults to current)", + Required: false, + }, + { + Name: "description", + Short: "New description of the rule (optional, defaults to current)", + Required: false, + }, + { + Name: "zone", + Short: "Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | nl-ams-1 | nl-ams-2 | pl-waw-1 | pl-waw-2)", + Required: true, + }, + }, + Run: func(ctx context.Context, argsI any) (any, error) { + args := argsI.(*redisUpdateACLRuleRequest) + api := redis.NewAPI(core.ExtractClient(ctx)) + + // 1. Get the existing rule + rule, err := api.GetACLRule(&redis.GetACLRuleRequest{ + ACLID: args.ACLID, + Zone: scw.Zone(args.Zone), + }) + if err != nil { + return nil, fmt.Errorf("failed to get ACL rule: %w", err) + } + + // 2. Delete the existing rule + _, err = api.DeleteACLRule(&redis.DeleteACLRuleRequest{ + ACLID: args.ACLID, + Zone: scw.Zone(args.Zone), + }) + if err != nil { + return nil, fmt.Errorf("failed to delete ACL rule: %w", err) + } + + waitReq := &redis.WaitForClusterRequest{ + ClusterID: args.ClusterID, + Zone: scw.Zone(args.Zone), + Timeout: scw.TimeDurationPtr(redisActionTimeout), + } + _, err = api.WaitForCluster(waitReq) + if err != nil { + return nil, fmt.Errorf("failed to wait for cluster to be ready: %w", err) + } + + // 3. Add a new rule with updated fields + ipCIDR := args.IPCidr + if ipCIDR == "" { + ipCIDR = rule.IPCidr.String() + } + desc := args.Description + if desc == "" { + desc = *rule.Description + } + _, ipnet, err := net.ParseCIDR(ipCIDR) + if err != nil { + return nil, fmt.Errorf("invalid ip-cidr: %w", err) + } + scwIPNet := scw.IPNet{IPNet: *ipnet} + addResp, err := api.AddACLRules(&redis.AddACLRulesRequest{ + ClusterID: args.ClusterID, + Zone: scw.Zone(args.Zone), + ACLRules: []*redis.ACLRuleSpec{ + { + IPCidr: scwIPNet, + Description: desc, + }, + }, + }) + if err != nil { + return nil, fmt.Errorf("failed to add updated ACL rule: %w", err) + } + + _, err = api.WaitForCluster(waitReq) + if err != nil { + return nil, fmt.Errorf("failed to wait for cluster to be ready: %w", err) + } + + return addResp.ACLRules, nil + }, + } +} + +type redisUpdateACLRuleRequest struct { + ClusterID string + ACLID string + IPCidr string + Description string + Zone string +} From 94a28b54384a3da29740d1e26974dabca28039f1 Mon Sep 17 00:00:00 2001 From: protobuf-ci-cd Date: Thu, 26 Jun 2025 15:54:31 +0200 Subject: [PATCH 2/5] update test all usage --- cmd/scw/testdata/test-all-usage-redis-acl-usage.golden | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/scw/testdata/test-all-usage-redis-acl-usage.golden b/cmd/scw/testdata/test-all-usage-redis-acl-usage.golden index 8d836fecba..8322342abf 100644 --- a/cmd/scw/testdata/test-all-usage-redis-acl-usage.golden +++ b/cmd/scw/testdata/test-all-usage-redis-acl-usage.golden @@ -10,6 +10,7 @@ AVAILABLE COMMANDS: delete Delete an ACL rule for a cluster get Get an ACL rule set Set ACL rules for a cluster + update Update an ACL rule for a Redis™ Database Instance (network rule) FLAGS: -h, --help help for acl From 1e75740c431948da8ce72bde879c870e37c0c470 Mon Sep 17 00:00:00 2001 From: protobuf-ci-cd Date: Thu, 26 Jun 2025 15:59:15 +0200 Subject: [PATCH 3/5] fix autocomplete --- docs/commands/autocomplete.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/commands/autocomplete.md b/docs/commands/autocomplete.md index bb92628259..95f1777fb1 100644 --- a/docs/commands/autocomplete.md +++ b/docs/commands/autocomplete.md @@ -45,7 +45,7 @@ scw autocomplete script [arg=value ...] | Name | | Description | |------|---|-------------| -| shell | Default: `/bin/zsh` | | +| shell | Default: `/bin/bash` | | | basename | Default: `` | | From 7669d361e270c0b0ce392998a19ba84f2b6a8007 Mon Sep 17 00:00:00 2001 From: protobuf-ci-cd Date: Thu, 26 Jun 2025 16:06:04 +0200 Subject: [PATCH 4/5] fix doc --- docs/commands/redis.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/commands/redis.md b/docs/commands/redis.md index 185e257f49..cfea4838e8 100644 --- a/docs/commands/redis.md +++ b/docs/commands/redis.md @@ -143,7 +143,7 @@ scw redis acl update [arg=value ...] | acl-id | Required | UUID of the ACL rule to update | | ip-cidr | | New IPv4 network address of the rule (optional, defaults to current) | | description | | New description of the rule (optional, defaults to current) | -| zone | Default: `fr-par-1`
One of: `fr-par-1`, `fr-par-2`, `nl-ams-1`, `nl-ams-2`, `pl-waw-1`, `pl-waw-2` | Zone to target. If none is passed will use default zone from the config | +| zone | Required | Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | nl-ams-1 | nl-ams-2 | pl-waw-1 | pl-waw-2) | From f63a343178d023cff16e10c39d7c4d5a81dadc3c Mon Sep 17 00:00:00 2001 From: protobuf-ci-cd Date: Mon, 30 Jun 2025 15:47:58 +0200 Subject: [PATCH 5/5] add test all usage --- ...st-all-usage-redis-acl-update-usage.golden | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 cmd/scw/testdata/test-all-usage-redis-acl-update-usage.golden diff --git a/cmd/scw/testdata/test-all-usage-redis-acl-update-usage.golden b/cmd/scw/testdata/test-all-usage-redis-acl-update-usage.golden new file mode 100644 index 0000000000..89743947c1 --- /dev/null +++ b/cmd/scw/testdata/test-all-usage-redis-acl-update-usage.golden @@ -0,0 +1,22 @@ +🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲 +🟥🟥🟥 STDERR️️ 🟥🟥🟥️ +Update an ACL rule (IP/description) for a Redis™ Database Instance (Redis™ cluster). This command simulates an update by fetching, deleting, and re-adding the rule. + +USAGE: + scw redis acl update [arg=value ...] + +ARGS: + cluster-id UUID of the Redis cluster + acl-id UUID of the ACL rule to update + [ip-cidr] New IPv4 network address of the rule (optional, defaults to current) + [description] New description of the rule (optional, defaults to current) + zone Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | nl-ams-1 | nl-ams-2 | pl-waw-1 | pl-waw-2) + +FLAGS: + -h, --help help for update + +GLOBAL FLAGS: + -c, --config string The path to the config file + -D, --debug Enable debug mode + -o, --output string Output format: json or human, see 'scw help output' for more info (default "human") + -p, --profile string The config profile to use