|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "reflect" |
| 8 | + |
| 9 | + "github.com/scaleway/scaleway-cli/v2/core" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/api/redis/v1" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +func redisACLUpdateCommand() *core.Command { |
| 15 | + return &core.Command{ |
| 16 | + Short: "Update an ACL rule for a Redis™ Database Instance (network rule)", |
| 17 | + 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.", |
| 18 | + Namespace: "redis", |
| 19 | + Resource: "acl", |
| 20 | + Verb: "update", |
| 21 | + ArgsType: reflect.TypeOf(redisUpdateACLRuleRequest{}), |
| 22 | + ArgSpecs: core.ArgSpecs{ |
| 23 | + { |
| 24 | + Name: "cluster-id", |
| 25 | + Short: "UUID of the Redis cluster", |
| 26 | + Required: true, |
| 27 | + Positional: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + Name: "acl-id", |
| 31 | + Short: "UUID of the ACL rule to update", |
| 32 | + Required: true, |
| 33 | + Positional: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + Name: "ip-cidr", |
| 37 | + Short: "New IPv4 network address of the rule (optional, defaults to current)", |
| 38 | + Required: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + Name: "description", |
| 42 | + Short: "New description of the rule (optional, defaults to current)", |
| 43 | + Required: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + Name: "zone", |
| 47 | + 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)", |
| 48 | + Required: true, |
| 49 | + }, |
| 50 | + }, |
| 51 | + Run: func(ctx context.Context, argsI any) (any, error) { |
| 52 | + args := argsI.(*redisUpdateACLRuleRequest) |
| 53 | + api := redis.NewAPI(core.ExtractClient(ctx)) |
| 54 | + |
| 55 | + // 1. Get the existing rule |
| 56 | + rule, err := api.GetACLRule(&redis.GetACLRuleRequest{ |
| 57 | + ACLID: args.ACLID, |
| 58 | + Zone: scw.Zone(args.Zone), |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to get ACL rule: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + // 2. Delete the existing rule |
| 65 | + _, err = api.DeleteACLRule(&redis.DeleteACLRuleRequest{ |
| 66 | + ACLID: args.ACLID, |
| 67 | + Zone: scw.Zone(args.Zone), |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to delete ACL rule: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + waitReq := &redis.WaitForClusterRequest{ |
| 74 | + ClusterID: args.ClusterID, |
| 75 | + Zone: scw.Zone(args.Zone), |
| 76 | + Timeout: scw.TimeDurationPtr(redisActionTimeout), |
| 77 | + } |
| 78 | + _, err = api.WaitForCluster(waitReq) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("failed to wait for cluster to be ready: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + // 3. Add a new rule with updated fields |
| 84 | + ipCIDR := args.IPCidr |
| 85 | + if ipCIDR == "" { |
| 86 | + ipCIDR = rule.IPCidr.String() |
| 87 | + } |
| 88 | + desc := args.Description |
| 89 | + if desc == "" { |
| 90 | + desc = *rule.Description |
| 91 | + } |
| 92 | + _, ipnet, err := net.ParseCIDR(ipCIDR) |
| 93 | + if err != nil { |
| 94 | + return nil, fmt.Errorf("invalid ip-cidr: %w", err) |
| 95 | + } |
| 96 | + scwIPNet := scw.IPNet{IPNet: *ipnet} |
| 97 | + addResp, err := api.AddACLRules(&redis.AddACLRulesRequest{ |
| 98 | + ClusterID: args.ClusterID, |
| 99 | + Zone: scw.Zone(args.Zone), |
| 100 | + ACLRules: []*redis.ACLRuleSpec{ |
| 101 | + { |
| 102 | + IPCidr: scwIPNet, |
| 103 | + Description: desc, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }) |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("failed to add updated ACL rule: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + _, err = api.WaitForCluster(waitReq) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("failed to wait for cluster to be ready: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + return addResp.ACLRules, nil |
| 117 | + }, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +type redisUpdateACLRuleRequest struct { |
| 122 | + ClusterID string |
| 123 | + ACLID string |
| 124 | + IPCidr string |
| 125 | + Description string |
| 126 | + Zone string |
| 127 | +} |
0 commit comments