Skip to content

Commit d17d4e7

Browse files
jremy42dependabot[bot]estellesoulard
authored
feat(rdb): add created date filters to backup list (#5199)
Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: esoulard <[email protected]>
1 parent 325075f commit d17d4e7

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

cmd/scw/testdata/test-all-usage-rdb-backup-list-usage.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ ARGS:
1111
[instance-id] UUID of the Database Instance
1212
[project-id] Project ID of the Project the database backups belong to
1313
[organization-id] Organization ID of the Organization the database backups belong to
14+
[created-before] Only list backups created before this date. Supports absolute RFC3339 timestamps and relative times (see `scw help date`).
15+
[created-after] Only list backups created after this date. Supports absolute RFC3339 timestamps and relative times (see `scw help date`).
1416
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw | all)
1517

1618
FLAGS:

docs/commands/rdb.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ scw rdb backup list [arg=value ...]
335335
| instance-id | | UUID of the Database Instance |
336336
| project-id | | Project ID of the Project the database backups belong to |
337337
| organization-id | | Organization ID of the Organization the database backups belong to |
338+
| created-before | | Only list backups created before this date. Supports absolute RFC3339 timestamps and relative times (see `scw help date`). |
339+
| created-after | | Only list backups created after this date. Supports absolute RFC3339 timestamps and relative times (see `scw help date`). |
338340
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw`, `all` | Region to target. If none is passed will use default region from the config |
339341

340342

internal/namespaces/rdb/v1/custom_backup.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,89 @@ func backupRestoreBuilder(c *core.Command) *core.Command {
148148
}
149149

150150
func backupListBuilder(c *core.Command) *core.Command {
151+
type backupListArgs struct {
152+
Name *string
153+
OrderBy rdb.ListDatabaseBackupsRequestOrderBy
154+
InstanceID *string
155+
ProjectID *string
156+
OrganizationID *string
157+
Region scw.Region
158+
CreatedAfter *time.Time
159+
CreatedBefore *time.Time
160+
}
161+
162+
c.ArgsType = reflect.TypeOf(backupListArgs{})
163+
164+
// Insert created-after / created-before date filters before region, keeping
165+
// existing arguments (name, order-by, instance-id, project-id, organization-id)
166+
// unchanged and still passed to the API request.
167+
c.ArgSpecs.AddBefore("region", &core.ArgSpec{
168+
Name: "created-before",
169+
Short: "Only list backups created before this date. " +
170+
"Supports absolute RFC3339 timestamps and relative times (see `scw help date`).",
171+
Required: false,
172+
})
173+
c.ArgSpecs.AddBefore("region", &core.ArgSpec{
174+
Name: "created-after",
175+
Short: "Only list backups created after this date. " +
176+
"Supports absolute RFC3339 timestamps and relative times (see `scw help date`).",
177+
Required: false,
178+
})
179+
180+
c.Run = func(ctx context.Context, argsI any) (any, error) {
181+
args := argsI.(*backupListArgs)
182+
183+
client := core.ExtractClient(ctx)
184+
api := rdb.NewAPI(client)
185+
186+
req := &rdb.ListDatabaseBackupsRequest{
187+
Name: args.Name,
188+
OrderBy: args.OrderBy,
189+
InstanceID: args.InstanceID,
190+
ProjectID: args.ProjectID,
191+
OrganizationID: args.OrganizationID,
192+
Region: args.Region,
193+
}
194+
195+
opts := []scw.RequestOption{scw.WithAllPages()}
196+
if req.Region == scw.Region(core.AllLocalities) {
197+
opts = append(opts, scw.WithRegions(api.Regions()...))
198+
req.Region = ""
199+
}
200+
201+
resp, err := api.ListDatabaseBackups(req, opts...)
202+
if err != nil {
203+
return nil, err
204+
}
205+
206+
backups := resp.DatabaseBackups
207+
208+
// Client-side filtering on CreatedAt using parsed absolute or relative dates.
209+
if args.CreatedAfter != nil || args.CreatedBefore != nil {
210+
filtered := make([]*rdb.DatabaseBackup, 0, len(backups))
211+
for _, b := range backups {
212+
createdAt := b.CreatedAt
213+
214+
if args.CreatedAfter != nil {
215+
if createdAt == nil || createdAt.Before(*args.CreatedAfter) {
216+
continue
217+
}
218+
}
219+
220+
if args.CreatedBefore != nil {
221+
if createdAt == nil || createdAt.After(*args.CreatedBefore) {
222+
continue
223+
}
224+
}
225+
226+
filtered = append(filtered, b)
227+
}
228+
backups = filtered
229+
}
230+
231+
return backups, nil
232+
}
233+
151234
type customBackup struct {
152235
ID string `json:"ID"`
153236
InstanceID string `json:"instance_ID"`

0 commit comments

Comments
 (0)