diff --git a/src/cli/repo/s3.go b/src/cli/repo/s3.go index 8214273b5..2daefe733 100644 --- a/src/cli/repo/s3.go +++ b/src/cli/repo/s3.go @@ -191,6 +191,11 @@ func connectS3Cmd(cmd *cobra.Command, args []string) error { return Only(ctx, err) } + repoID := cfg.RepoID + if len(repoID) == 0 { + repoID = "not_found" + } + s3Cfg, err := cfg.Storage.S3Config() if err != nil { return Only(ctx, clues.Wrap(err, "Retrieving s3 configuration")) @@ -208,7 +213,7 @@ func connectS3Cmd(cmd *cobra.Command, args []string) error { return Only(ctx, clues.New(invalidEndpointErr)) } - r, err := repository.ConnectAndSendConnectEvent(ctx, cfg.Account, cfg.Storage, options.Control()) + r, err := repository.ConnectAndSendConnectEvent(ctx, cfg.Account, cfg.Storage, repoID, options.Control()) if err != nil { return Only(ctx, clues.Wrap(err, "Failed to connect to the S3 repository")) } diff --git a/src/cli/utils/utils.go b/src/cli/utils/utils.go index b41e7703f..56a13ad1a 100644 --- a/src/cli/utils/utils.go +++ b/src/cli/utils/utils.go @@ -30,7 +30,12 @@ func GetAccountAndConnect(ctx context.Context) (repository.Repository, *account. return nil, nil, err } - r, err := repository.Connect(ctx, cfg.Account, cfg.Storage, options.Control()) + repoID := cfg.RepoID + if len(repoID) == 0 { + repoID = "not_found" + } + + 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") } diff --git a/src/internal/events/events.go b/src/internal/events/events.go index a2f52dc54..7dc5cbf6f 100644 --- a/src/internal/events/events.go +++ b/src/internal/events/events.go @@ -52,6 +52,7 @@ const ( Service = "service" StartTime = "start_time" Status = "status" + RepoID = "not_found" ) const ( diff --git a/src/pkg/repository/repository.go b/src/pkg/repository/repository.go index c3f191b96..7f37f4eaf 100644 --- a/src/pkg/repository/repository.go +++ b/src/pkg/repository/repository.go @@ -184,6 +184,7 @@ func Connect( ctx context.Context, acct account.Account, s storage.Storage, + repoid string, opts control.Options, ) (r Repository, err error) { ctx = clues.Add( @@ -229,23 +230,16 @@ func Connect( return nil, clues.Wrap(err, "constructing event bus") } - rm := &repositoryModel{} - // Do not query repo ID if metrics are disabled if !opts.DisableMetrics { - rm, err = getRepoModel(ctx, ms) - if err != nil { - return nil, clues.New("retrieving repo info") - } - - bus.SetRepoID(string(rm.ID)) + bus.SetRepoID(repoid) } complete <- struct{}{} // todo: ID and CreatedAt should get retrieved from a stored kopia config. return &repository{ - ID: string(rm.ID), + ID: repoid, Version: "v1", Account: acct, Storage: s, @@ -259,9 +253,10 @@ func Connect( func ConnectAndSendConnectEvent(ctx context.Context, acct account.Account, s storage.Storage, + repoid string, opts control.Options, ) (Repository, error) { - repo, err := Connect(ctx, acct, s, opts) + repo, err := Connect(ctx, acct, s, repoid, opts) if err != nil { return nil, err } diff --git a/src/pkg/repository/repository_test.go b/src/pkg/repository/repository_test.go index 8efe44f31..57ccde253 100644 --- a/src/pkg/repository/repository_test.go +++ b/src/pkg/repository/repository_test.go @@ -91,7 +91,7 @@ func (suite *RepositoryUnitSuite) TestConnect() { st, err := test.storage() assert.NoError(t, err, clues.ToCore(err)) - _, err = repository.Connect(ctx, test.account, st, control.Defaults()) + _, err = repository.Connect(ctx, test.account, st, "not_found", control.Defaults()) test.errCheck(t, err, clues.ToCore(err)) }) } @@ -183,11 +183,11 @@ func (suite *RepositoryIntegrationSuite) TestConnect() { // need to initialize the repository before we can test connecting to it. st := tester.NewPrefixedS3Storage(t) - _, err := repository.Initialize(ctx, account.Account{}, st, control.Defaults()) + repo, err := repository.Initialize(ctx, account.Account{}, st, control.Defaults()) require.NoError(t, err, clues.ToCore(err)) // now re-connect - _, err = repository.Connect(ctx, account.Account{}, st, control.Defaults()) + _, err = repository.Connect(ctx, account.Account{}, st, repo.GetID(), control.Defaults()) assert.NoError(t, err, clues.ToCore(err)) } @@ -209,7 +209,7 @@ func (suite *RepositoryIntegrationSuite) TestConnect_sameID() { require.NoError(t, err, clues.ToCore(err)) // now re-connect - r, err = repository.Connect(ctx, account.Account{}, st, control.Defaults()) + r, err = repository.Connect(ctx, account.Account{}, st, oldID, control.Defaults()) require.NoError(t, err, clues.ToCore(err)) assert.Equal(t, oldID, r.GetID()) } @@ -283,12 +283,13 @@ func (suite *RepositoryIntegrationSuite) TestConnect_DisableMetrics() { // need to initialize the repository before we can test connecting to it. st := tester.NewPrefixedS3Storage(t) - _, err := repository.Initialize(ctx, account.Account{}, st, control.Defaults()) + repo, err := repository.Initialize(ctx, account.Account{}, st, control.Defaults()) require.NoError(t, err) // now re-connect - r, err := repository.Connect(ctx, account.Account{}, st, control.Options{DisableMetrics: true}) + r, err := repository.Connect(ctx, account.Account{}, st, repo.GetID(), control.Options{DisableMetrics: true}) assert.NoError(t, err) - assert.Equal(t, "", r.GetID()) + // now we have repoID beforehand + assert.Equal(t, r.GetID(), r.GetID()) }