extract scope from qp (#1510)

## Description

Since scopes are service specific, we cannot
easily house them within the graph QueryParam
struct, unless we bloat the struct with all types.
Alternatively, we could add a generic "scope"
with parsers, much like the Selector itself.  But
really, the most simple solution is to only pass
the scope within the tree of service funcs that
use it.

## Type of change

- [x] 🐹 Refactor

## Issue(s)

* #1506

## Test Plan

- [x]  Unit test
This commit is contained in:
Keepers 2022-11-17 15:08:37 -07:00 committed by GitHub
parent f1de0eb2b7
commit 93ad16dc30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 50 deletions

View File

@ -114,23 +114,17 @@ func (gc *GraphConnector) createExchangeCollections(
collections := make(map[string]*exchange.Collection) collections := make(map[string]*exchange.Collection)
qp := graph.QueryParams{ qp := graph.QueryParams{
User: user, Category: scope.Category().PathType(),
Scope: scope, ResourceOwner: user,
FailFast: gc.failFast, FailFast: gc.failFast,
Credentials: gc.credentials, Credentials: gc.credentials,
} }
itemCategory := qp.Scope.Category().PathType() foldersComplete, closer := observe.MessageWithCompletion(fmt.Sprintf("∙ %s - %s:", qp.Category, user))
foldersComplete, closer := observe.MessageWithCompletion(fmt.Sprintf("∙ %s - %s:", itemCategory.String(), user))
defer closer() defer closer()
defer close(foldersComplete) defer close(foldersComplete)
resolver, err := exchange.PopulateExchangeContainerResolver( resolver, err := exchange.PopulateExchangeContainerResolver(ctx, qp)
ctx,
qp,
qp.Scope.Category().PathType(),
)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "getting folder cache") return nil, errors.Wrap(err, "getting folder cache")
} }
@ -140,7 +134,8 @@ func (gc *GraphConnector) createExchangeCollections(
qp, qp,
collections, collections,
gc.UpdateStatus, gc.UpdateStatus,
resolver) resolver,
scope)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "filling collections") return nil, errors.Wrap(err, "filling collections")
@ -264,18 +259,13 @@ func (gc *GraphConnector) createSharePointCollections(
collections := make(map[string]*sharepoint.Collection) collections := make(map[string]*sharepoint.Collection)
qp := graph.QueryParams{ qp := graph.QueryParams{
// TODO: Resource owner, not user/site. Category: scope.Category().PathType(),
User: site, ResourceOwner: site,
// TODO: generic scope handling in query params.
// - or, break scope out of QP.
// Scope: scope,
FailFast: gc.failFast, FailFast: gc.failFast,
Credentials: gc.credentials, Credentials: gc.credentials,
} }
itemCategory := qp.Scope.Category().PathType() foldersComplete, closer := observe.MessageWithCompletion(fmt.Sprintf("∙ %s - %s:", qp.Category, site))
foldersComplete, closer := observe.MessageWithCompletion(fmt.Sprintf("∙ %s - %s:", itemCategory.String(), site))
defer closer() defer closer()
defer close(foldersComplete) defer close(foldersComplete)

View File

@ -137,7 +137,6 @@ func DeleteContactFolder(ctx context.Context, gs graph.Service, user, folderID s
func PopulateExchangeContainerResolver( func PopulateExchangeContainerResolver(
ctx context.Context, ctx context.Context,
qp graph.QueryParams, qp graph.QueryParams,
category path.CategoryType,
) (graph.ContainerResolver, error) { ) (graph.ContainerResolver, error) {
var ( var (
res graph.ContainerResolver res graph.ContainerResolver
@ -149,30 +148,30 @@ func PopulateExchangeContainerResolver(
return nil, err return nil, err
} }
switch category { switch qp.Category {
case path.EmailCategory: case path.EmailCategory:
res = &mailFolderCache{ res = &mailFolderCache{
userID: qp.User, userID: qp.ResourceOwner,
gs: service, gs: service,
} }
cacheRoot = rootFolderAlias cacheRoot = rootFolderAlias
case path.ContactsCategory: case path.ContactsCategory:
res = &contactFolderCache{ res = &contactFolderCache{
userID: qp.User, userID: qp.ResourceOwner,
gs: service, gs: service,
} }
cacheRoot = DefaultContactFolder cacheRoot = DefaultContactFolder
case path.EventsCategory: case path.EventsCategory:
res = &eventCalendarCache{ res = &eventCalendarCache{
userID: qp.User, userID: qp.ResourceOwner,
gs: service, gs: service,
} }
cacheRoot = DefaultCalendar cacheRoot = DefaultCalendar
default: default:
return nil, fmt.Errorf("ContainerResolver not present for %s type", category) return nil, fmt.Errorf("ContainerResolver not present for %s type", qp.Category)
} }
if err := res.Populate(ctx, cacheRoot); err != nil { if err := res.Populate(ctx, cacheRoot); err != nil {
@ -182,8 +181,13 @@ func PopulateExchangeContainerResolver(
return res, nil return res, nil
} }
func pathAndMatch(qp graph.QueryParams, category path.CategoryType, c graph.CachedContainer) (path.Path, bool) { func pathAndMatch(
qp graph.QueryParams,
c graph.CachedContainer,
scope selectors.ExchangeScope,
) (path.Path, bool) {
var ( var (
category = scope.Category().PathType()
directory string directory string
pb = c.Path() pb = c.Path()
) )
@ -195,7 +199,7 @@ func pathAndMatch(qp graph.QueryParams, category path.CategoryType, c graph.Cach
dirPath, err := pb.ToDataLayerExchangePathForCategory( dirPath, err := pb.ToDataLayerExchangePathForCategory(
qp.Credentials.AzureTenantID, qp.Credentials.AzureTenantID,
qp.User, qp.ResourceOwner,
category, category,
false, false,
) )
@ -211,11 +215,11 @@ func pathAndMatch(qp graph.QueryParams, category path.CategoryType, c graph.Cach
switch category { switch category {
case path.EmailCategory: case path.EmailCategory:
return dirPath, qp.Scope.Matches(selectors.ExchangeMailFolder, directory) return dirPath, scope.Matches(selectors.ExchangeMailFolder, directory)
case path.ContactsCategory: case path.ContactsCategory:
return dirPath, qp.Scope.Matches(selectors.ExchangeContactFolder, directory) return dirPath, scope.Matches(selectors.ExchangeContactFolder, directory)
case path.EventsCategory: case path.EventsCategory:
return dirPath, qp.Scope.Matches(selectors.ExchangeEventCalendar, directory) return dirPath, scope.Matches(selectors.ExchangeEventCalendar, directory)
default: default:
return nil, false return nil, false
} }

View File

@ -13,6 +13,7 @@ import (
"github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/connector/graph"
"github.com/alcionai/corso/src/internal/connector/support" "github.com/alcionai/corso/src/internal/connector/support"
"github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/selectors"
) )
// FilterContainersAndFillCollections is a utility function // FilterContainersAndFillCollections is a utility function
@ -26,21 +27,21 @@ func FilterContainersAndFillCollections(
collections map[string]*Collection, collections map[string]*Collection,
statusUpdater support.StatusUpdater, statusUpdater support.StatusUpdater,
resolver graph.ContainerResolver, resolver graph.ContainerResolver,
scope selectors.ExchangeScope,
) error { ) error {
var ( var (
category = qp.Scope.Category().PathType() collectionType = CategoryToOptionIdentifier(scope.Category().PathType())
collectionType = CategoryToOptionIdentifier(category)
errs error errs error
) )
for _, c := range resolver.Items() { for _, c := range resolver.Items() {
dirPath, ok := pathAndMatch(qp, category, c) dirPath, ok := pathAndMatch(qp, c, scope)
if ok { if ok {
// Create only those that match // Create only those that match
service, err := createService(qp.Credentials, qp.FailFast) service, err := createService(qp.Credentials, qp.FailFast)
if err != nil { if err != nil {
errs = support.WrapAndAppend( errs = support.WrapAndAppend(
qp.User+" FilterContainerAndFillCollection", qp.ResourceOwner+" FilterContainerAndFillCollection",
err, err,
errs) errs)
@ -50,7 +51,7 @@ func FilterContainersAndFillCollections(
} }
edc := NewCollection( edc := NewCollection(
qp.User, qp.ResourceOwner,
dirPath, dirPath,
collectionType, collectionType,
service, service,
@ -61,10 +62,10 @@ func FilterContainersAndFillCollections(
} }
for directoryID, col := range collections { for directoryID, col := range collections {
fetchFunc, err := getFetchIDFunc(category) fetchFunc, err := getFetchIDFunc(scope.Category().PathType())
if err != nil { if err != nil {
errs = support.WrapAndAppend( errs = support.WrapAndAppend(
qp.User, qp.ResourceOwner,
err, err,
errs) errs)
@ -75,10 +76,10 @@ func FilterContainersAndFillCollections(
continue continue
} }
jobs, err := fetchFunc(ctx, col.service, qp.User, directoryID) jobs, err := fetchFunc(ctx, col.service, qp.ResourceOwner, directoryID)
if err != nil { if err != nil {
errs = support.WrapAndAppend( errs = support.WrapAndAppend(
qp.User, qp.ResourceOwner,
err, err,
errs, errs,
) )

View File

@ -7,12 +7,11 @@ import (
"github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/path" "github.com/alcionai/corso/src/pkg/path"
"github.com/alcionai/corso/src/pkg/selectors"
) )
type QueryParams struct { type QueryParams struct {
User string Category path.CategoryType
Scope selectors.ExchangeScope ResourceOwner string
Credentials account.M365Config Credentials account.M365Config
FailFast bool FailFast bool
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/alcionai/corso/src/internal/connector/graph" "github.com/alcionai/corso/src/internal/connector/graph"
"github.com/alcionai/corso/src/internal/connector/support" "github.com/alcionai/corso/src/internal/connector/support"
"github.com/alcionai/corso/src/pkg/selectors"
) )
// FilterContainersAndFillCollections is a utility function // FilterContainersAndFillCollections is a utility function
@ -17,6 +18,7 @@ func FilterContainersAndFillCollections(
collections map[string]*Collection, collections map[string]*Collection,
statusUpdater support.StatusUpdater, statusUpdater support.StatusUpdater,
resolver graph.ContainerResolver, resolver graph.ContainerResolver,
scope selectors.SharePointScope,
) error { ) error {
return nil return nil
} }

View File

@ -16,6 +16,7 @@ const (
CorsoCLIRepoTests = "CORSO_COMMAND_LINE_REPO_TESTS" CorsoCLIRepoTests = "CORSO_COMMAND_LINE_REPO_TESTS"
CorsoCLIRestoreTests = "CORSO_COMMAND_LINE_RESTORE_TESTS" CorsoCLIRestoreTests = "CORSO_COMMAND_LINE_RESTORE_TESTS"
CorsoCLITests = "CORSO_COMMAND_LINE_TESTS" CorsoCLITests = "CORSO_COMMAND_LINE_TESTS"
CorsoConnectorCreateCollectionTests = "CORSO_CONNECTOR_CREATE_COLLECTION_TESTS"
CorsoConnectorCreateExchangeCollectionTests = "CORSO_CONNECTOR_CREATE_EXCHANGE_COLLECTION_TESTS" CorsoConnectorCreateExchangeCollectionTests = "CORSO_CONNECTOR_CREATE_EXCHANGE_COLLECTION_TESTS"
CorsoConnectorCreateSharePointCollectionTests = "CORSO_CONNECTOR_CREATE_SHAREPOINT_COLLECTION_TESTS" CorsoConnectorCreateSharePointCollectionTests = "CORSO_CONNECTOR_CREATE_SHAREPOINT_COLLECTION_TESTS"
CorsoConnectorDataCollectionTests = "CORSO_CONNECTOR_DATA_COLLECTION_TESTS" CorsoConnectorDataCollectionTests = "CORSO_CONNECTOR_DATA_COLLECTION_TESTS"