move graphService to graph as Service (#1790)

## Description

Relocates the graphService struct to graph as
the Service struct.  Replaces GC's embedded
graphService with a graph.Servicer reference.

## Type of change

- [x] 🐹 Trivial/Minor

## Issue(s)

* #1725

## Test Plan

- [x]  Unit test
- [x] 💚 E2E
This commit is contained in:
Keepers 2022-12-13 18:39:00 -07:00 committed by GitHub
parent 8f8bf14ea2
commit 8a29c52cdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 43 additions and 52 deletions

View File

@ -81,7 +81,7 @@ func handleGetCommand(cmd *cobra.Command, args []string) error {
return err return err
} }
err = runDisplayM365JSON(ctx, gc.Service()) err = runDisplayM365JSON(ctx, gc.Service)
if err != nil { if err != nil {
return Only(ctx, errors.Wrapf(err, "unable to create mock from M365: %s", m365ID)) return Only(ctx, errors.Wrapf(err, "unable to create mock from M365: %s", m365ID))
} }

View File

@ -195,7 +195,7 @@ func purgeFolders(
Infof(ctx, "Container: %s", data) Infof(ctx, "Container: %s", data)
// get them folders // get them folders
fs, err := getter(gc.Service(), uid, prefix) fs, err := getter(gc.Service, uid, prefix)
if err != nil { if err != nil {
return Only(ctx, errors.Wrapf(err, "retrieving %s folders", data)) return Only(ctx, errors.Wrapf(err, "retrieving %s folders", data))
} }
@ -227,7 +227,7 @@ func purgeFolders(
Infof(ctx, "∙ Deleting [%s]", displayName) Infof(ctx, "∙ Deleting [%s]", displayName)
err = deleter(gc.Service(), uid, fld) err = deleter(gc.Service, uid, fld)
if err != nil { if err != nil {
err = errors.Wrapf(err, "!! Error") err = errors.Wrapf(err, "!! Error")
errs = multierror.Append(errs, err) errs = multierror.Append(errs, err)

View File

@ -55,7 +55,7 @@ func (gc *GraphConnector) DataCollections(
sels, sels,
gc.GetSiteIDs(), gc.GetSiteIDs(),
gc.credentials.AzureTenantID, gc.credentials.AzureTenantID,
gc, gc.Service,
gc, gc,
ctrlOpts) ctrlOpts)
if err != nil { if err != nil {
@ -265,7 +265,7 @@ func (gc *GraphConnector) OneDriveDataCollections(
user, user,
onedrive.OneDriveSource, onedrive.OneDriveSource,
odFolderMatcher{scope}, odFolderMatcher{scope},
gc, gc.Service,
gc.UpdateStatus, gc.UpdateStatus,
ctrlOpts, ctrlOpts,
).Get(ctx) ).Get(ctx)

View File

@ -195,7 +195,7 @@ func (suite *ConnectorDataCollectionIntegrationSuite) TestSharePointDataCollecti
test.getSelector(t), test.getSelector(t),
[]string{suite.site}, []string{suite.site},
connector.credentials.AzureTenantID, connector.credentials.AzureTenantID,
connector, connector.Service,
connector, connector,
control.Options{}) control.Options{})
require.NoError(t, err) require.NoError(t, err)

View File

@ -25,6 +25,28 @@ type QueryParams struct {
Credentials account.M365Config Credentials account.M365Config
} }
var _ Servicer = &Service{}
type Service struct {
adapter *msgraphsdk.GraphRequestAdapter
client *msgraphsdk.GraphServiceClient
}
func NewService(adapter *msgraphsdk.GraphRequestAdapter) *Service {
return &Service{
adapter: adapter,
client: msgraphsdk.NewGraphServiceClient(adapter),
}
}
func (s Service) Adapter() *msgraphsdk.GraphRequestAdapter {
return s.adapter
}
func (s Service) Client() *msgraphsdk.GraphServiceClient {
return s.client
}
type Servicer interface { type Servicer interface {
// Client() returns msgraph Service client that can be used to process and execute // Client() returns msgraph Service client that can be used to process and execute
// the majority of the queries to the M365 Backstore // the majority of the queries to the M365 Backstore

View File

@ -9,7 +9,6 @@ import (
"sync" "sync"
"github.com/microsoft/kiota-abstractions-go/serialization" "github.com/microsoft/kiota-abstractions-go/serialization"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -37,7 +36,7 @@ import (
// GraphRequestAdapter from the msgraph-sdk-go. Additional fields are for // GraphRequestAdapter from the msgraph-sdk-go. Additional fields are for
// bookkeeping and interfacing with other component. // bookkeeping and interfacing with other component.
type GraphConnector struct { type GraphConnector struct {
graphService Service graph.Servicer
tenant string tenant string
Users map[string]string // key<email> value<id> Users map[string]string // key<email> value<id>
Sites map[string]string // key<???> value<???> Sites map[string]string // key<???> value<???>
@ -52,27 +51,6 @@ type GraphConnector struct {
status support.ConnectorOperationStatus // contains the status of the last run status status support.ConnectorOperationStatus // contains the status of the last run status
} }
// Service returns the GC's embedded graph.Servicer
func (gc *GraphConnector) Service() graph.Servicer {
return gc.graphService
}
var _ graph.Servicer = &graphService{}
type graphService struct {
client msgraphsdk.GraphServiceClient
adapter msgraphsdk.GraphRequestAdapter
failFast bool // if true service will exit sequence upon encountering an error
}
func (gs graphService) Client() *msgraphsdk.GraphServiceClient {
return &gs.client
}
func (gs graphService) Adapter() *msgraphsdk.GraphRequestAdapter {
return &gs.adapter
}
type resource int type resource int
const ( const (
@ -95,12 +73,12 @@ func NewGraphConnector(ctx context.Context, acct account.Account, r resource) (*
credentials: m365, credentials: m365,
} }
aService, err := gc.createService() gService, err := gc.createService()
if err != nil { if err != nil {
return nil, errors.Wrap(err, "creating service connection") return nil, errors.Wrap(err, "creating service connection")
} }
gc.graphService = *aService gc.Service = gService
// TODO(ashmrtn): When selectors only encapsulate a single resource owner that // TODO(ashmrtn): When selectors only encapsulate a single resource owner that
// is not a wildcard don't populate users or sites when making the connector. // is not a wildcard don't populate users or sites when making the connector.
@ -122,26 +100,17 @@ func NewGraphConnector(ctx context.Context, acct account.Account, r resource) (*
} }
// createService constructor for graphService component // createService constructor for graphService component
func (gc *GraphConnector) createService() (*graphService, error) { func (gc *GraphConnector) createService() (*graph.Service, error) {
adapter, err := graph.CreateAdapter( adapter, err := graph.CreateAdapter(
gc.credentials.AzureTenantID, gc.credentials.AzureTenantID,
gc.credentials.AzureClientID, gc.credentials.AzureClientID,
gc.credentials.AzureClientSecret, gc.credentials.AzureClientSecret,
) )
if err != nil { if err != nil {
return nil, err return &graph.Service{}, err
} }
connector := graphService{ return graph.NewService(adapter), nil
adapter: *adapter,
client: *msgraphsdk.NewGraphServiceClient(adapter),
}
return &connector, nil
}
func (gs *graphService) EnableFailFast() {
gs.failFast = true
} }
// setTenantUsers queries the M365 to identify the users in the // setTenantUsers queries the M365 to identify the users in the
@ -151,7 +120,7 @@ func (gc *GraphConnector) setTenantUsers(ctx context.Context) error {
ctx, end := D.Span(ctx, "gc:setTenantUsers") ctx, end := D.Span(ctx, "gc:setTenantUsers")
defer end() defer end()
users, err := discovery.Users(ctx, gc, gc.tenant) users, err := discovery.Users(ctx, gc.Service, gc.tenant)
if err != nil { if err != nil {
return err return err
} }
@ -186,7 +155,7 @@ func (gc *GraphConnector) setTenantSites(ctx context.Context) error {
sites, err := getResources( sites, err := getResources(
ctx, ctx,
gc.graphService, gc.Service,
gc.tenant, gc.tenant,
sharepoint.GetAllSitesForTenant, sharepoint.GetAllSitesForTenant,
models.CreateSiteCollectionResponseFromDiscriminatorValue, models.CreateSiteCollectionResponseFromDiscriminatorValue,
@ -315,11 +284,11 @@ func (gc *GraphConnector) RestoreDataCollections(
switch selector.Service { switch selector.Service {
case selectors.ServiceExchange: case selectors.ServiceExchange:
status, err = exchange.RestoreExchangeDataCollections(ctx, gc.graphService, dest, dcs, deets) status, err = exchange.RestoreExchangeDataCollections(ctx, gc.Service, dest, dcs, deets)
case selectors.ServiceOneDrive: case selectors.ServiceOneDrive:
status, err = onedrive.RestoreCollections(ctx, gc, dest, dcs, deets) status, err = onedrive.RestoreCollections(ctx, gc.Service, dest, dcs, deets)
case selectors.ServiceSharePoint: case selectors.ServiceSharePoint:
status, err = sharepoint.RestoreCollections(ctx, gc, dest, dcs, deets) status, err = sharepoint.RestoreCollections(ctx, gc.Service, dest, dcs, deets)
default: default:
err = errors.Errorf("restore data from service %s not supported", selector.Service.String()) err = errors.Errorf("restore data from service %s not supported", selector.Service.String())
} }

View File

@ -174,7 +174,7 @@ func (suite *GraphConnectorIntegrationSuite) TestSetTenantUsers() {
service, err := newConnector.createService() service, err := newConnector.createService()
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
newConnector.graphService = *service newConnector.Service = service
suite.Empty(len(newConnector.Users)) suite.Empty(len(newConnector.Users))
err = newConnector.setTenantUsers(ctx) err = newConnector.setTenantUsers(ctx)
@ -197,7 +197,7 @@ func (suite *GraphConnectorIntegrationSuite) TestSetTenantSites() {
service, err := newConnector.createService() service, err := newConnector.createService()
require.NoError(suite.T(), err) require.NoError(suite.T(), err)
newConnector.graphService = *service newConnector.Service = service
suite.Equal(0, len(newConnector.Sites)) suite.Equal(0, len(newConnector.Sites))
err = newConnector.setTenantSites(ctx) err = newConnector.setTenantSites(ctx)
@ -413,7 +413,7 @@ func (suite *GraphConnectorIntegrationSuite) TestRestoreAndBackup() {
driveID := mustGetDefaultDriveID( driveID := mustGetDefaultDriveID(
suite.T(), suite.T(),
ctx, ctx,
suite.connector, suite.connector.Service,
suite.user, suite.user,
) )

View File

@ -25,7 +25,7 @@ func Users(ctx context.Context, m365Account account.Account) ([]*User, error) {
return nil, errors.Wrap(err, "could not initialize M365 graph connection") return nil, errors.Wrap(err, "could not initialize M365 graph connection")
} }
users, err := discovery.Users(ctx, gc.Service(), m365Account.ID()) users, err := discovery.Users(ctx, gc.Service, m365Account.ID())
if err != nil { if err != nil {
return nil, err return nil, err
} }