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 (
"context"
"github.com/hashicorp/go-multierror"
"github.com/alcionai/corso/src/cli/config"
"github.com/alcionai/corso/src/cli/options"
. "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/path"
"github.com/alcionai/corso/src/pkg/repository"
"github.com/alcionai/corso/src/pkg/selectors"
"github.com/alcionai/corso/src/pkg/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@ -154,6 +157,62 @@ func handleDeleteCmd(cmd *cobra.Command, args []string) error {
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
// for the removal of an entry from the repository
func genericDeleteCommand(cmd *cobra.Command, bID, designation string, args []string) error {

View File

@ -3,7 +3,6 @@ package backup
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -12,8 +11,6 @@ import (
. "github.com/alcionai/corso/src/cli/print"
"github.com/alcionai/corso/src/cli/utils"
"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/fault"
"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)"))
}
var (
merrs *multierror.Error
bIDs []model.StableID
)
selectorSet := []selectors.Selector{}
for _, discSel := range sel.SplitByResourceOwner(users) {
bo, err := r.NewBackup(ctx, 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)
selectorSet = append(selectorSet, discSel.Selector)
}
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
return runBackups(
ctx,
r,
"Exchange", "user",
selectorSet,
)
}
func exchangeBackupCreateSelectors(userIDs, cats []string) *selectors.ExchangeBackup {

View File

@ -3,7 +3,6 @@ package backup
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -12,8 +11,6 @@ import (
. "github.com/alcionai/corso/src/cli/print"
"github.com/alcionai/corso/src/cli/utils"
"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/fault"
"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"))
}
var (
merrs *multierror.Error
bIDs []model.StableID
)
selectorSet := []selectors.Selector{}
for _, discSel := range sel.SplitByResourceOwner(users) {
bo, err := r.NewBackup(ctx, 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)
selectorSet = append(selectorSet, discSel.Selector)
}
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
return runBackups(
ctx,
r,
"OneDrive", "user",
selectorSet,
)
}
func validateOneDriveBackupCreateFlags(users []string) error {

View File

@ -3,7 +3,6 @@ package backup
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
@ -14,8 +13,6 @@ import (
"github.com/alcionai/corso/src/internal/connector"
"github.com/alcionai/corso/src/internal/connector/graph"
"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/fault"
"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"))
}
var (
merrs *multierror.Error
bIDs []model.StableID
)
selectorSet := []selectors.Selector{}
for _, discSel := range sel.SplitByResourceOwner(gc.GetSiteIDs()) {
bo, err := r.NewBackup(ctx, 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)
selectorSet = append(selectorSet, discSel.Selector)
}
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
return runBackups(
ctx,
r,
"SharePoint", "site",
selectorSet,
)
}
func validateSharePointBackupCreateFlags(sites, weburls, cats []string) error {