From a836e877af340b4e3318849fd5f8eb0d9ccf876f Mon Sep 17 00:00:00 2001 From: ashmrtn <3891298+ashmrtn@users.noreply.github.com> Date: Fri, 28 Jul 2023 10:47:42 -0700 Subject: [PATCH] Pull backup deletion logic into function (#3924) Make a separate function to handle backup deletion. Makes repo connection management a little bit easier since a simple defer will work now. Will also help keep things organized as we expand these tests to do other things like use the point-in-time function to check the deleted backups are still accessible, especially since that will require opening another connection to the repo --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [ ] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Supportability/Tests - [x] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) * #3799 #### Test Plan - [x] :muscle: Manual - [ ] :zap: Unit test - [ ] :green_heart: E2E --- src/cmd/longevity_test/longevity.go | 71 +++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/src/cmd/longevity_test/longevity.go b/src/cmd/longevity_test/longevity.go index 33de060f8..ef17b7e32 100644 --- a/src/cmd/longevity_test/longevity.go +++ b/src/cmd/longevity_test/longevity.go @@ -7,6 +7,7 @@ import ( "strconv" "time" + "github.com/alcionai/clues" "github.com/spf13/cobra" "github.com/alcionai/corso/src/cli/config" @@ -16,6 +17,51 @@ import ( "github.com/alcionai/corso/src/pkg/store" ) +// deleteBackups connects to the repository and deletes all backups for +// service that are at least deletionDays old. Returns the IDs of all backups +// that were deleted. +func deleteBackups( + ctx context.Context, + service path.ServiceType, + deletionDays int, +) ([]string, error) { + ctx = clues.Add(ctx, "cutoff_days", deletionDays) + + r, _, _, _, err := utils.GetAccountAndConnect(ctx, service, nil) + if err != nil { + return nil, clues.Wrap(err, "connecting to account").WithClues(ctx) + } + + defer r.Close(ctx) + + backups, err := r.BackupsByTag(ctx, store.Service(service)) + if err != nil { + return nil, clues.Wrap(err, "listing backups").WithClues(ctx) + } + + var ( + deleted []string + cutoff = time.Now().Add(-time.Hour * 24 * time.Duration(deletionDays)) + ) + + for _, backup := range backups { + if backup.StartAndEndTime.CompletedAt.Before(cutoff) { + if err := r.DeleteBackup(ctx, backup.ID.String()); err != nil { + return nil, clues.Wrap( + err, + "deleting backup"). + With("backup_id", backup.ID). + WithClues(ctx) + } + + deleted = append(deleted, backup.ID.String()) + logAndPrint(ctx, "Deleted backup %s", backup.ID.String()) + } + } + + return deleted, nil +} + func main() { var ( service path.ServiceType @@ -39,31 +85,16 @@ func main() { fatal(cc.Context(), "unknown service", nil) } - r, _, _, _, err := utils.GetAccountAndConnect(cc.Context(), service, nil) - if err != nil { - fatal(cc.Context(), "unable to connect account", err) - } - - defer r.Close(cc.Context()) - - backups, err := r.BackupsByTag(cc.Context(), store.Service(service)) - if err != nil { - fatal(cc.Context(), "unable to find backups", err) - } + ctx := clues.Add(cc.Context(), "service", service) days, err := strconv.Atoi(os.Getenv("DELETION_DAYS")) if err != nil { - fatal(cc.Context(), "invalid no of days provided", nil) + fatal(ctx, "invalid number of days provided", nil) } - for _, backup := range backups { - if backup.StartAndEndTime.CompletedAt.Before(time.Now().AddDate(0, 0, -days)) { - if err := r.DeleteBackup(cc.Context(), backup.ID.String()); err != nil { - fatal(cc.Context(), "deleting backup", err) - } - - logAndPrint(cc.Context(), "Deleted backup %s", backup.ID.String()) - } + _, err = deleteBackups(ctx, service, days) + if err != nil { + fatal(cc.Context(), "deleting backups", clues.Stack(err)) } }