From 66ff1efc47bf3ab3df9dc042b57f31cc146cbab9 Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 8 Dec 2023 12:28:02 -0800 Subject: [PATCH] Adding new call to services middleware (#4823) As the tittle suggests. Adding this call to the middleware used by clients. --- #### Does this PR need a docs update or release note? - [ ] :white_check_mark: Yes, it's included - [ ] :clock1: Yes, but in a later PR - [x] :no_entry: No #### Type of change - [x] :sunflower: Feature - [ ] :bug: Bugfix - [ ] :world_map: Documentation - [ ] :robot: Supportability/Tests - [ ] :computer: CI/Deployment - [ ] :broom: Tech Debt/Cleanup #### Issue(s) * # #### Test Plan - [ ] :muscle: Manual - [x] :zap: Unit test - [x] :green_heart: E2E - added CI test --- src/pkg/services/m365/groups.go | 33 ++++++++++++++++++++++++++++ src/pkg/services/m365/groups_test.go | 16 ++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/pkg/services/m365/groups.go b/src/pkg/services/m365/groups.go index 5c608a661..a19161d23 100644 --- a/src/pkg/services/m365/groups.go +++ b/src/pkg/services/m365/groups.go @@ -41,6 +41,23 @@ func (c client) GroupByID( return parseGroup(ctx, g) } +// TeamById retrieves a specific group. +// when the caller knows the group is a team. +// will fail if the group is not a team. +func (c client) TeamByID( + ctx context.Context, + id string, +) (*Group, error) { + cc := api.CallConfig{} + + g, err := c.ac.Groups().GetTeamByID(ctx, id, cc) + if err != nil { + return nil, clues.Stack(err) + } + + return parseGroupFromTeamable(ctx, g) +} + // GroupsCompat returns a list of groups in the specified M365 tenant. func (c client) GroupsCompat(ctx context.Context) ([]*Group, error) { errs := fault.New(true) @@ -123,6 +140,22 @@ func parseGroup(ctx context.Context, mg models.Groupable) (*Group, error) { return u, nil } +// parseGroup extracts information from `models.Teamable` we care about +func parseGroupFromTeamable(ctx context.Context, mg models.Teamable) (*Group, error) { + if mg.GetDisplayName() == nil { + return nil, clues.New("group missing display name"). + With("group_id", ptr.Val(mg.GetId())) + } + + u := &Group{ + ID: ptr.Val(mg.GetId()), + DisplayName: ptr.Val(mg.GetDisplayName()), + IsTeam: true, + } + + return u, nil +} + // GroupsMap retrieves an id-name cache of all groups in the tenant. func (c client) GroupsMap( ctx context.Context, diff --git a/src/pkg/services/m365/groups_test.go b/src/pkg/services/m365/groups_test.go index 447e99391..806abac06 100644 --- a/src/pkg/services/m365/groups_test.go +++ b/src/pkg/services/m365/groups_test.go @@ -84,6 +84,22 @@ func (suite *GroupsIntgSuite) TestGroupByID_ByEmail() { assert.Equal(t, groupByEmail, group, "must be the same group as the one gotten by id") } +func (suite *GroupsIntgSuite) TestTeamByID() { + t := suite.T() + + ctx, flush := tester.NewContext(t) + defer flush() + + gid := tconfig.M365TeamID(t) + + group, err := suite.cli.TeamByID(ctx, 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) TestGroupByID_notFound() { t := suite.T()