add GroupByID to services (#4396)

Adds a groupByID call to services, and
adds CallConfig to the group by id api fn.

---

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

- [x]  No

#### Type of change

- [x] 🌻 Feature

#### Issue(s)

* #3988

#### Test Plan

- [x] 💚 E2E
This commit is contained in:
Keepers 2023-09-28 16:36:38 -06:00 committed by GitHub
parent c3f94fd7f7
commit e5647a809d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 75 additions and 18 deletions

View File

@ -55,7 +55,10 @@ func ProduceBackupCollections(
"group_id", clues.Hide(bpc.ProtectedResource.ID()),
"group_name", clues.Hide(bpc.ProtectedResource.Name()))
group, err := ac.Groups().GetByID(ctx, bpc.ProtectedResource.ID())
group, err := ac.Groups().GetByID(
ctx,
bpc.ProtectedResource.ID(),
api.CallConfig{})
if err != nil {
return nil, nil, false, clues.Wrap(err, "getting group").WithClues(ctx)
}

View File

@ -7,18 +7,15 @@ import (
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/pkg/filters"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)
type getByIDer interface {
GetByID(ctx context.Context, identifier string) (models.Groupable, error)
}
func IsServiceEnabled(
ctx context.Context,
gbi getByIDer,
gbi api.GetByIDer[models.Groupable],
resource string,
) (bool, error) {
resp, err := gbi.GetByID(ctx, resource)
resp, err := gbi.GetByID(ctx, resource, api.CallConfig{})
if err != nil {
return false, clues.Wrap(err, "getting group").WithClues(ctx)
}

View File

@ -12,6 +12,7 @@ import (
"github.com/alcionai/corso/src/internal/m365/graph"
"github.com/alcionai/corso/src/internal/tester"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)
type EnabledUnitSuite struct {
@ -22,14 +23,18 @@ func TestEnabledUnitSuite(t *testing.T) {
suite.Run(t, &EnabledUnitSuite{Suite: tester.NewUnitSuite(t)})
}
var _ getByIDer = mockGBI{}
var _ api.GetByIDer[models.Groupable] = mockGBI{}
type mockGBI struct {
group models.Groupable
err error
}
func (m mockGBI) GetByID(ctx context.Context, identifier string) (models.Groupable, error) {
func (m mockGBI) GetByID(
ctx context.Context,
identifier string,
_ api.CallConfig,
) (models.Groupable, error) {
return m.group, m.err
}
@ -56,13 +61,13 @@ func (suite *EnabledUnitSuite) TestIsServiceEnabled() {
table := []struct {
name string
mock func(context.Context) getByIDer
mock func(context.Context) api.GetByIDer[models.Groupable]
expect assert.BoolAssertionFunc
expectErr assert.ErrorAssertionFunc
}{
{
name: "ok",
mock: func(ctx context.Context) getByIDer {
mock: func(ctx context.Context) api.GetByIDer[models.Groupable] {
return mockGBI{
group: unified,
}
@ -72,7 +77,7 @@ func (suite *EnabledUnitSuite) TestIsServiceEnabled() {
},
{
name: "non-unified group",
mock: func(ctx context.Context) getByIDer {
mock: func(ctx context.Context) api.GetByIDer[models.Groupable] {
return mockGBI{
group: nonUnified,
}
@ -82,7 +87,7 @@ func (suite *EnabledUnitSuite) TestIsServiceEnabled() {
},
{
name: "group not found",
mock: func(ctx context.Context) getByIDer {
mock: func(ctx context.Context) api.GetByIDer[models.Groupable] {
return mockGBI{
err: graph.Stack(ctx, odErrMsg(string(graph.RequestResourceNotFound), "message")),
}
@ -92,7 +97,7 @@ func (suite *EnabledUnitSuite) TestIsServiceEnabled() {
},
{
name: "arbitrary error",
mock: func(ctx context.Context) getByIDer {
mock: func(ctx context.Context) api.GetByIDer[models.Groupable] {
return mockGBI{
err: assert.AnError,
}

View File

@ -126,3 +126,15 @@ func (c Client) Get(
type CallConfig struct {
Expand []string
}
// ---------------------------------------------------------------------------
// common interfaces
// ---------------------------------------------------------------------------
type GetByIDer[T any] interface {
GetByID(
ctx context.Context,
identifier string,
cc CallConfig,
) (T, error)
}

View File

@ -102,6 +102,7 @@ const filterGroupByDisplayNameQueryTmpl = "displayName eq '%s'"
func (c Groups) GetByID(
ctx context.Context,
identifier string,
_ CallConfig, // matching standards
) (models.Groupable, error) {
service, err := c.Service()
if err != nil {
@ -234,9 +235,9 @@ func IsTeam(ctx context.Context, mg models.Groupable) bool {
func (c Groups) GetIDAndName(
ctx context.Context,
groupID string,
_ CallConfig, // not currently supported
cc CallConfig,
) (string, string, error) {
s, err := c.GetByID(ctx, groupID)
s, err := c.GetByID(ctx, groupID, cc)
if err != nil {
return "", "", err
}

View File

@ -121,7 +121,7 @@ func (suite *GroupsIntgSuite) TestGroups_GetByID() {
groupsAPI = suite.its.ac.Groups()
)
grp, err := groupsAPI.GetByID(ctx, groupID)
grp, err := groupsAPI.GetByID(ctx, groupID, api.CallConfig{})
require.NoError(t, err, clues.ToCore(err))
table := []struct {
@ -157,7 +157,7 @@ func (suite *GroupsIntgSuite) TestGroups_GetByID() {
ctx, flush := tester.NewContext(t)
defer flush()
_, err := groupsAPI.GetByID(ctx, test.id)
_, err := groupsAPI.GetByID(ctx, test.id, api.CallConfig{})
test.expectErr(t, err, clues.ToCore(err))
})
}

View File

@ -28,6 +28,27 @@ type Group struct {
IsTeam bool
}
// GroupByID retrieves a specific group.
func GroupByID(
ctx context.Context,
acct account.Account,
id string,
) (*Group, error) {
ac, err := makeAC(ctx, acct, path.GroupsService)
if err != nil {
return nil, clues.Stack(err).WithClues(ctx)
}
cc := api.CallConfig{}
g, err := ac.Groups().GetByID(ctx, id, cc)
if err != nil {
return nil, clues.Stack(err)
}
return parseGroup(ctx, g)
}
// GroupsCompat returns a list of groups in the specified M365 tenant.
func GroupsCompat(ctx context.Context, acct account.Account) ([]*Group, error) {
errs := fault.New(true)

View File

@ -41,6 +41,24 @@ func (suite *GroupsIntgSuite) SetupSuite() {
suite.acct = tconfig.NewM365Account(t)
}
func (suite *GroupsIntgSuite) TestGroupByID() {
t := suite.T()
ctx, flush := tester.NewContext(t)
defer flush()
graph.InitializeConcurrencyLimiter(ctx, true, 4)
gid := tconfig.M365TeamID(t)
group, err := m365.GroupByID(ctx, suite.acct, gid)
require.NoError(t, err, clues.ToCore(err))
require.NotNil(t, group)
assert.Equal(t, gid, group.ID, "must match expected id")
assert.NotEmpty(t, group.DisplayName)
}
func (suite *GroupsIntgSuite) TestGroups() {
t := suite.T()