Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Groups: Add GetTransitiveMembers #289

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions msgraph/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,41 @@ func (c *GroupsClient) ListMembers(ctx context.Context, id string) (*[]string, i
return &ret, status, nil
}

// GetTransitiveMembers retrieves all nested members of the specified Group.
// id is the object ID of the group.
func (c *GroupsClient) GetTransitiveMembers(ctx context.Context, groupId string, query odata.Query) (*[]User, int, error) {
query.Expand = odata.Expand{Relationship: "*"}

resp, status, _, err := c.BaseClient.Get(ctx, GetHttpRequestInput{
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
OData: query,
ValidStatusCodes: []int{http.StatusOK},
Uri: Uri{
Entity: fmt.Sprintf("/groups/%s/transitiveMembers", groupId),
},
})

if err != nil {
return nil, status, fmt.Errorf("GroupsClient.BaseClient.Get(): %v", err)
}

defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, status, fmt.Errorf("io.ReadAll(): %v", err)
}

var data struct {
Users []User `json:"value"`
}

if err = json.Unmarshal(respBody, &data); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}

return &data.Users, status, nil
}

// ListTransitiveMembers retrieves a flat list of all nested members of the specified Group.
// id is the object ID of the group.
func (c *GroupsClient) ListTransitiveMembers(ctx context.Context, id string) (*[]string, int, error) {
Expand Down
23 changes: 21 additions & 2 deletions msgraph/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ func TestGroupsClient(t *testing.T) {
testGroupsClient_GetOwner(t, c, *group.ID(), (*owners)[0])

members := testGroupsClient_ListMembers(t, c, *group.ID())
transitiveMembers := testGroupsClient_ListTransitiveMembers(t, c, *group.ID())
listedTransitiveMembers := testGroupsClient_ListTransitiveMembers(t, c, *group.ID())
transitiveMembers := testGroupsClient_GetTransitiveMembers(t, c, *group.ID())
testGroupsClient_GetMember(t, c, *group.ID(), (*members)[0])
testGroupsClient_GetMember(t, c, *group.ID(), (*transitiveMembers)[0])
testGroupsClient_GetMember(t, c, *group.ID(), (*listedTransitiveMembers)[0])
testGroupsClient_GetMember(t, c, *group.ID(), *(*transitiveMembers)[0].ID())
testGroupsClient_GetMembers(t, c, *group.ID(), odata.Query{})

group.DisplayName = utils.StringPtr(fmt.Sprintf("test-updated-group-%s", c.RandomString))
Expand Down Expand Up @@ -242,6 +244,23 @@ func testGroupsClient_ListMembers(t *testing.T, c *test.Test, id string) (member
return
}

func testGroupsClient_GetTransitiveMembers(t *testing.T, c *test.Test, id string) (members *[]msgraph.User) {
members, status, err := c.GroupsClient.GetTransitiveMembers(c.Context, id, odata.Query{})
if err != nil {
t.Fatalf("GroupsClient.GetTransitiveMembers(): %v", err)
}
if status != 200 {
t.Fatalf("GroupsClient.GetTransitiveMembers(): invalid status: %d", status)
}
if members == nil {
t.Fatal("GroupsClient.GetTransitiveMembers(): members was nil")
}
if len(*members) == 0 {
t.Fatal("GroupsClient.GetTransitiveMembers(): members was empty")
}
return
}

func testGroupsClient_ListTransitiveMembers(t *testing.T, c *test.Test, id string) (members *[]string) {
members, status, err := c.GroupsClient.ListTransitiveMembers(c.Context, id)
if err != nil {
Expand Down
Loading