Factor out common code for running backups in CLI layer (#2737)

#### Does this PR need a docs update or release note?

- [ ]  Yes, it's included
- [ ] 🕐 Yes, but in a later PR
- [x]  No

#### Type of change

- [ ] 🌻 Feature
- [ ] 🐛 Bugfix
- [ ] 🗺️ Documentation
- [ ] 🤖 Test
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

#### Test Plan

- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2023-03-13 09:05:05 -07:00 committed by GitHub
parent 69c824fc74
commit c0fc821976
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 129 deletions

View File

@ -3,6 +3,8 @@ package backup
import ( import (
"context" "context"
"github.com/hashicorp/go-multierror"
"github.com/alcionai/corso/src/cli/config" "github.com/alcionai/corso/src/cli/config"
"github.com/alcionai/corso/src/cli/options" "github.com/alcionai/corso/src/cli/options"
. "github.com/alcionai/corso/src/cli/print" . "github.com/alcionai/corso/src/cli/print"
@ -13,6 +15,7 @@ import (
"github.com/alcionai/corso/src/pkg/backup" "github.com/alcionai/corso/src/pkg/backup"
"github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/repository" "github.com/alcionai/corso/src/pkg/repository"
"github.com/alcionai/corso/src/pkg/selectors"
"github.com/alcionai/corso/src/pkg/store" "github.com/alcionai/corso/src/pkg/store"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -154,6 +157,62 @@ func handleDeleteCmd(cmd *cobra.Command, args []string) error {
return cmd.Help() return cmd.Help()
} }
func runBackups(
ctx context.Context,
r repository.Repository,
serviceName, resourceOwnerType string,
selectorSet []selectors.Selector,
) error {
var (
merrs *multierror.Error
bIDs []model.StableID
)
for _, discSel := range selectorSet {
bo, err := r.NewBackup(ctx, discSel)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to initialize %s backup for %s %s",
serviceName,
resourceOwnerType,
discSel.DiscreteOwner,
))
continue
}
err = bo.Run(ctx)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to run %s backup for %s %s",
serviceName,
resourceOwnerType,
discSel.DiscreteOwner,
))
continue
}
bIDs = append(bIDs, bo.Results.BackupID)
}
bups, ferrs := r.Backups(ctx, bIDs)
// TODO: print/log recoverable errors
if ferrs.Failure() != nil {
return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage"))
}
backup.PrintAll(ctx, bups)
if e := merrs.ErrorOrNil(); e != nil {
return Only(ctx, e)
}
return nil
}
// genericDeleteCommand is a helper function that all services can use // genericDeleteCommand is a helper function that all services can use
// for the removal of an entry from the repository // for the removal of an entry from the repository
func genericDeleteCommand(cmd *cobra.Command, bID, designation string, args []string) error { func genericDeleteCommand(cmd *cobra.Command, bID, designation string, args []string) error {

View File

@ -3,7 +3,6 @@ package backup
import ( import (
"context" "context"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -12,8 +11,6 @@ import (
. "github.com/alcionai/corso/src/cli/print" . "github.com/alcionai/corso/src/cli/print"
"github.com/alcionai/corso/src/cli/utils" "github.com/alcionai/corso/src/cli/utils"
"github.com/alcionai/corso/src/internal/data" "github.com/alcionai/corso/src/internal/data"
"github.com/alcionai/corso/src/internal/model"
"github.com/alcionai/corso/src/pkg/backup"
"github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/path"
@ -275,50 +272,18 @@ func createExchangeCmd(cmd *cobra.Command, args []string) error {
return Only(ctx, errors.Wrap(err, "Failed to retrieve M365 user(s)")) return Only(ctx, errors.Wrap(err, "Failed to retrieve M365 user(s)"))
} }
var ( selectorSet := []selectors.Selector{}
merrs *multierror.Error
bIDs []model.StableID
)
for _, discSel := range sel.SplitByResourceOwner(users) { for _, discSel := range sel.SplitByResourceOwner(users) {
bo, err := r.NewBackup(ctx, discSel.Selector) selectorSet = append(selectorSet, discSel.Selector)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to initialize Exchange backup for user %s",
discSel.DiscreteOwner,
))
continue
}
err = bo.Run(ctx)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to run Exchange backup for user %s",
discSel.DiscreteOwner,
))
continue
}
bIDs = append(bIDs, bo.Results.BackupID)
} }
bups, ferrs := r.Backups(ctx, bIDs) return runBackups(
// TODO: print/log recoverable errors ctx,
if ferrs.Failure() != nil { r,
return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage")) "Exchange", "user",
} selectorSet,
)
backup.PrintAll(ctx, bups)
if e := merrs.ErrorOrNil(); e != nil {
return Only(ctx, e)
}
return nil
} }
func exchangeBackupCreateSelectors(userIDs, cats []string) *selectors.ExchangeBackup { func exchangeBackupCreateSelectors(userIDs, cats []string) *selectors.ExchangeBackup {

View File

@ -3,7 +3,6 @@ package backup
import ( import (
"context" "context"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -12,8 +11,6 @@ import (
. "github.com/alcionai/corso/src/cli/print" . "github.com/alcionai/corso/src/cli/print"
"github.com/alcionai/corso/src/cli/utils" "github.com/alcionai/corso/src/cli/utils"
"github.com/alcionai/corso/src/internal/data" "github.com/alcionai/corso/src/internal/data"
"github.com/alcionai/corso/src/internal/model"
"github.com/alcionai/corso/src/pkg/backup"
"github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/path"
@ -194,50 +191,18 @@ func createOneDriveCmd(cmd *cobra.Command, args []string) error {
return Only(ctx, errors.Wrap(err, "Failed to retrieve M365 users")) return Only(ctx, errors.Wrap(err, "Failed to retrieve M365 users"))
} }
var ( selectorSet := []selectors.Selector{}
merrs *multierror.Error
bIDs []model.StableID
)
for _, discSel := range sel.SplitByResourceOwner(users) { for _, discSel := range sel.SplitByResourceOwner(users) {
bo, err := r.NewBackup(ctx, discSel.Selector) selectorSet = append(selectorSet, discSel.Selector)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to initialize OneDrive backup for user %s",
discSel.DiscreteOwner,
))
continue
}
err = bo.Run(ctx)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to run OneDrive backup for user %s",
discSel.DiscreteOwner,
))
continue
}
bIDs = append(bIDs, bo.Results.BackupID)
} }
bups, ferrs := r.Backups(ctx, bIDs) return runBackups(
// TODO: print/log recoverable errors ctx,
if ferrs.Failure() != nil { r,
return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage")) "OneDrive", "user",
} selectorSet,
)
backup.PrintAll(ctx, bups)
if e := merrs.ErrorOrNil(); e != nil {
return Only(ctx, e)
}
return nil
} }
func validateOneDriveBackupCreateFlags(users []string) error { func validateOneDriveBackupCreateFlags(users []string) error {

View File

@ -3,7 +3,6 @@ package backup
import ( import (
"context" "context"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
@ -14,8 +13,6 @@ import (
"github.com/alcionai/corso/src/internal/connector" "github.com/alcionai/corso/src/internal/connector"
"github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/connector/graph"
"github.com/alcionai/corso/src/internal/data" "github.com/alcionai/corso/src/internal/data"
"github.com/alcionai/corso/src/internal/model"
"github.com/alcionai/corso/src/pkg/backup"
"github.com/alcionai/corso/src/pkg/backup/details" "github.com/alcionai/corso/src/pkg/backup/details"
"github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/path"
@ -241,50 +238,18 @@ func createSharePointCmd(cmd *cobra.Command, args []string) error {
return Only(ctx, errors.Wrap(err, "Retrieving up sharepoint sites by ID and WebURL")) return Only(ctx, errors.Wrap(err, "Retrieving up sharepoint sites by ID and WebURL"))
} }
var ( selectorSet := []selectors.Selector{}
merrs *multierror.Error
bIDs []model.StableID
)
for _, discSel := range sel.SplitByResourceOwner(gc.GetSiteIDs()) { for _, discSel := range sel.SplitByResourceOwner(gc.GetSiteIDs()) {
bo, err := r.NewBackup(ctx, discSel.Selector) selectorSet = append(selectorSet, discSel.Selector)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to initialize SharePoint backup for site %s",
discSel.DiscreteOwner,
))
continue
}
err = bo.Run(ctx)
if err != nil {
merrs = multierror.Append(merrs, errors.Wrapf(
err,
"Failed to run SharePoint backup for site %s",
discSel.DiscreteOwner,
))
continue
}
bIDs = append(bIDs, bo.Results.BackupID)
} }
bups, ferrs := r.Backups(ctx, bIDs) return runBackups(
// TODO: print/log recoverable errors ctx,
if ferrs.Failure() != nil { r,
return Only(ctx, errors.Wrap(ferrs.Failure(), "Unable to retrieve backup results from storage")) "SharePoint", "site",
} selectorSet,
)
backup.PrintAll(ctx, bups)
if e := merrs.ErrorOrNil(); e != nil {
return Only(ctx, e)
}
return nil
} }
func validateSharePointBackupCreateFlags(sites, weburls, cats []string) error { func validateSharePointBackupCreateFlags(sites, weburls, cats []string) error {