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 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 diff --git a/docs/commands/redis.md b/docs/commands/redis.md index 10ce5b3185..cfea4838e8 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 | 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) | + + + ## 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 +}