get repoID from config for events (#3449)

<!-- PR description-->

- Get repoID from config for events for uniformity across repo
- Set a default value if repoID is nil/blank

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

- [x]  No

#### Type of change

<!--- Please check the type of change your PR introduces: --->
- [ ] 🐛 Bugfix

#### Issue(s)

<!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. -->
* https://github.com/alcionai/corso/issues/3388

#### Test Plan

<!-- How will this be tested prior to merging.-->
- [ ] 💪 Manual
- [ ]  Unit test
- [ ] 💚 E2E
This commit is contained in:
neha_gupta 2023-05-22 20:34:56 +05:30 committed by GitHub
parent d1a4c68fea
commit 8bb9df8e75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 19 deletions

View File

@ -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"))
}

View File

@ -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")
}

View File

@ -52,6 +52,7 @@ const (
Service = "service"
StartTime = "start_time"
Status = "status"
RepoID = "not_found"
)
const (

View File

@ -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
}

View File

@ -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())
}