Keepers 3cc29649ed
Add GetUserInfo to services/m365 (#2991)
Adds an interface for GetUserInfo, which gets
corso-specific metadata for a single user.  Also
does some refactoring around discovery for
better interface consistency, both as variables
and as access layers.

---

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

- [x]  No

#### Type of change

- [x] 🌻 Feature

#### Test Plan

- [x]  Unit test
- [x] 💚 E2E
2023-04-07 00:36:50 +00:00

126 lines
3.0 KiB
Go

package discovery
import (
"context"
"github.com/alcionai/clues"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/internal/connector/discovery/api"
"github.com/alcionai/corso/src/internal/connector/graph"
"github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/fault"
)
// ---------------------------------------------------------------------------
// interfaces
// ---------------------------------------------------------------------------
type getter interface {
GetByID(context.Context, string) (models.Userable, error)
}
type GetInfoer interface {
GetInfo(context.Context, string) (*api.UserInfo, error)
}
type getWithInfoer interface {
getter
GetInfoer
}
type getAller interface {
GetAll(ctx context.Context, errs *fault.Bus) ([]models.Userable, error)
}
// ---------------------------------------------------------------------------
// helpers
// ---------------------------------------------------------------------------
func apiClient(ctx context.Context, acct account.Account) (api.Client, error) {
m365, err := acct.M365Config()
if err != nil {
return api.Client{}, clues.Wrap(err, "retrieving m365 account configuration").WithClues(ctx)
}
client, err := api.NewClient(m365)
if err != nil {
return api.Client{}, clues.Wrap(err, "creating api client").WithClues(ctx)
}
return client, nil
}
// ---------------------------------------------------------------------------
// users
// ---------------------------------------------------------------------------
// Users fetches all users in the tenant.
func Users(
ctx context.Context,
ga getAller,
errs *fault.Bus,
) ([]models.Userable, error) {
users, err := ga.GetAll(ctx, errs)
if err != nil {
return nil, clues.Wrap(err, "getting all users")
}
return users, nil
}
func User(
ctx context.Context,
gwi getWithInfoer,
userID string,
) (models.Userable, *api.UserInfo, error) {
u, err := gwi.GetByID(ctx, userID)
if err != nil {
if graph.IsErrUserNotFound(err) {
return nil, nil, clues.New("resource owner not found within tenant").With("user_id", userID)
}
return nil, nil, clues.Wrap(err, "getting user")
}
ui, err := gwi.GetInfo(ctx, userID)
if err != nil {
return nil, nil, clues.Wrap(err, "getting user info")
}
return u, ui, nil
}
// UserInfo produces extensible user info: metadata that is relevant
// or identified in Corso, but not in m365.
func UserInfo(
ctx context.Context,
gi GetInfoer,
userID string,
) (*api.UserInfo, error) {
ui, err := gi.GetInfo(ctx, userID)
if err != nil {
return nil, clues.Wrap(err, "getting user info")
}
return ui, nil
}
// ---------------------------------------------------------------------------
// sites
// ---------------------------------------------------------------------------
// Sites fetches all sharepoint sites in the tenant
func Sites(
ctx context.Context,
acct account.Account,
errs *fault.Bus,
) ([]models.Siteable, error) {
client, err := apiClient(ctx, acct)
if err != nil {
return nil, err
}
return client.Sites().GetAll(ctx, errs)
}