diff --git a/src/cli/backup/backup.go b/src/cli/backup/backup.go index 25a6d22e7..c721e4c3f 100644 --- a/src/cli/backup/backup.go +++ b/src/cli/backup/backup.go @@ -279,7 +279,7 @@ func genericDeleteCommand(cmd *cobra.Command, bID, designation string, args []st ctx := clues.Add(cmd.Context(), "delete_backup_id", bID) - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } @@ -300,7 +300,7 @@ func genericDeleteCommand(cmd *cobra.Command, bID, designation string, args []st func genericListCommand(cmd *cobra.Command, bID string, service path.ServiceType, args []string) error { ctx := cmd.Context() - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } diff --git a/src/cli/backup/exchange.go b/src/cli/backup/exchange.go index ded194a05..af71c6a30 100644 --- a/src/cli/backup/exchange.go +++ b/src/cli/backup/exchange.go @@ -153,7 +153,7 @@ func createExchangeCmd(cmd *cobra.Command, args []string) error { return err } - r, acct, err := utils.GetAccountAndConnect(ctx) + r, acct, err := utils.AccountConnectAndWriteRepoConfig(ctx) if err != nil { return Only(ctx, err) } @@ -262,7 +262,7 @@ func detailsExchangeCmd(cmd *cobra.Command, args []string) error { ctx := cmd.Context() opts := utils.MakeExchangeOpts(cmd) - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } diff --git a/src/cli/backup/onedrive.go b/src/cli/backup/onedrive.go index dca460de0..b47acd496 100644 --- a/src/cli/backup/onedrive.go +++ b/src/cli/backup/onedrive.go @@ -134,7 +134,7 @@ func createOneDriveCmd(cmd *cobra.Command, args []string) error { return err } - r, acct, err := utils.GetAccountAndConnect(ctx) + r, acct, err := utils.AccountConnectAndWriteRepoConfig(ctx) if err != nil { return Only(ctx, err) } @@ -220,7 +220,7 @@ func detailsOneDriveCmd(cmd *cobra.Command, args []string) error { ctx := cmd.Context() opts := utils.MakeOneDriveOpts(cmd) - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } diff --git a/src/cli/backup/sharepoint.go b/src/cli/backup/sharepoint.go index 7f48d4c33..2197252ea 100644 --- a/src/cli/backup/sharepoint.go +++ b/src/cli/backup/sharepoint.go @@ -150,7 +150,7 @@ func createSharePointCmd(cmd *cobra.Command, args []string) error { return err } - r, acct, err := utils.GetAccountAndConnect(ctx) + r, acct, err := utils.AccountConnectAndWriteRepoConfig(ctx) if err != nil { return Only(ctx, err) } @@ -312,7 +312,7 @@ func detailsSharePointCmd(cmd *cobra.Command, args []string) error { ctx := cmd.Context() opts := utils.MakeSharePointOpts(cmd) - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } diff --git a/src/cli/repo/repo.go b/src/cli/repo/repo.go index 5f768cb8b..6d36d1608 100644 --- a/src/cli/repo/repo.go +++ b/src/cli/repo/repo.go @@ -121,7 +121,7 @@ func handleMaintenanceCmd(cmd *cobra.Command, args []string) error { return err } - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return print.Only(ctx, err) } diff --git a/src/cli/repo/s3.go b/src/cli/repo/s3.go index 2daefe733..feba087a8 100644 --- a/src/cli/repo/s3.go +++ b/src/cli/repo/s3.go @@ -13,6 +13,7 @@ import ( "github.com/alcionai/corso/src/cli/options" . "github.com/alcionai/corso/src/cli/print" "github.com/alcionai/corso/src/cli/utils" + "github.com/alcionai/corso/src/internal/events" "github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/repository" "github.com/alcionai/corso/src/pkg/storage" @@ -193,7 +194,7 @@ func connectS3Cmd(cmd *cobra.Command, args []string) error { repoID := cfg.RepoID if len(repoID) == 0 { - repoID = "not_found" + repoID = events.RepoIDNotFound } s3Cfg, err := cfg.Storage.S3Config() diff --git a/src/cli/restore/exchange.go b/src/cli/restore/exchange.go index f4390ef4c..514e6102c 100644 --- a/src/cli/restore/exchange.go +++ b/src/cli/restore/exchange.go @@ -89,7 +89,7 @@ func restoreExchangeCmd(cmd *cobra.Command, args []string) error { return err } - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } diff --git a/src/cli/restore/onedrive.go b/src/cli/restore/onedrive.go index 008ac18fd..85b159370 100644 --- a/src/cli/restore/onedrive.go +++ b/src/cli/restore/onedrive.go @@ -90,7 +90,7 @@ func restoreOneDriveCmd(cmd *cobra.Command, args []string) error { return err } - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } diff --git a/src/cli/restore/sharepoint.go b/src/cli/restore/sharepoint.go index c9b47b6bc..a52f5bb2a 100644 --- a/src/cli/restore/sharepoint.go +++ b/src/cli/restore/sharepoint.go @@ -95,7 +95,7 @@ func restoreSharePointCmd(cmd *cobra.Command, args []string) error { return err } - r, _, err := utils.GetAccountAndConnect(ctx) + r, _, _, err := utils.GetAccountAndConnect(ctx) if err != nil { return Only(ctx, err) } diff --git a/src/cli/utils/utils.go b/src/cli/utils/utils.go index 56a13ad1a..e0b4c5276 100644 --- a/src/cli/utils/utils.go +++ b/src/cli/utils/utils.go @@ -24,23 +24,53 @@ const ( Wildcard = "*" ) -func GetAccountAndConnect(ctx context.Context) (repository.Repository, *account.Account, error) { +func GetAccountAndConnect(ctx context.Context) (repository.Repository, *storage.Storage, *account.Account, error) { cfg, err := config.GetConfigRepoDetails(ctx, true, nil) if err != nil { - return nil, nil, err + return nil, nil, nil, err } repoID := cfg.RepoID if len(repoID) == 0 { - repoID = "not_found" + repoID = events.RepoIDNotFound } r, err := repository.Connect(ctx, cfg.Account, cfg.Storage, repoID, options.Control()) if err != nil { - return nil, nil, clues.Wrap(err, "Failed to connect to the "+cfg.Storage.Provider.String()+" repository") + return nil, nil, nil, clues.Wrap(err, "connecting to the "+cfg.Storage.Provider.String()+" repository") } - return r, &cfg.Account, nil + return r, &cfg.Storage, &cfg.Account, nil +} + +func AccountConnectAndWriteRepoConfig(ctx context.Context) (repository.Repository, *account.Account, error) { + r, stg, acc, err := GetAccountAndConnect(ctx) + if err != nil { + logger.CtxErr(ctx, err).Info("getting and connecting account") + return nil, nil, err + } + + s3Config, err := stg.S3Config() + if err != nil { + logger.CtxErr(ctx, err).Info("getting storage configuration") + return nil, nil, err + } + + m365Config, err := acc.M365Config() + if err != nil { + logger.CtxErr(ctx, err).Info("getting m365 configuration") + return nil, nil, err + } + + // repo config is already set while repo connect and init. This is just to confirm correct values. + // So won't fail is the write fails + err = config.WriteRepoConfig(ctx, s3Config, m365Config, r.GetID()) + if err != nil { + logger.CtxErr(ctx, err).Info("writing to repository configuration") + return nil, nil, err + } + + return r, acc, nil } // CloseRepo handles closing a repo. diff --git a/src/internal/events/events.go b/src/internal/events/events.go index 7dc5cbf6f..baa2c2117 100644 --- a/src/internal/events/events.go +++ b/src/internal/events/events.go @@ -52,7 +52,9 @@ const ( Service = "service" StartTime = "start_time" Status = "status" - RepoID = "not_found" + + // default values for keys + RepoIDNotFound = "not_found" ) const ( diff --git a/src/pkg/repository/repository.go b/src/pkg/repository/repository.go index c8e30829a..8bb99fef1 100644 --- a/src/pkg/repository/repository.go +++ b/src/pkg/repository/repository.go @@ -230,6 +230,15 @@ func Connect( return nil, clues.Wrap(err, "constructing event bus") } + if repoid == events.RepoIDNotFound { + rm, err := getRepoModel(ctx, ms) + if err != nil { + return nil, clues.New("retrieving repo info") + } + + repoid = string(rm.ID) + } + // Do not query repo ID if metrics are disabled if !opts.DisableMetrics { bus.SetRepoID(repoid)