Skip to content

Commit

Permalink
Merge pull request #236 from favoretti/favoretti/getMembers
Browse files Browse the repository at this point in the history
Add `GetMembers` to groups client
  • Loading branch information
manicminer authored Mar 24, 2023
2 parents f55dc50 + 989ea2e commit 985377d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-azure-helpers v0.52.0 h1:2xIjsPpDbZf5dFLEdqOy5akfdnh8M/6mQtVWhkpM3hM=
github.com/hashicorp/go-azure-helpers v0.52.0/go.mod h1:lsykLR4KjTUO7MiRmNWiTiX8QQtw3ILjyOvT0f5h3rw=
github.com/hashicorp/go-azure-sdk v0.20230222.1094704-0.20230223111607-1ec177dfc234 h1:Z2u0mU3UcLffUa1vf1TEKPALUCHyqM3EYHH5D25Gf7A=
github.com/hashicorp/go-azure-sdk v0.20230222.1094704-0.20230223111607-1ec177dfc234/go.mod h1:bilGy5T79PVRkSDDWvTLP0L/vB1L8+Aj1WlMnp5jUqA=
github.com/hashicorp/go-azure-sdk v0.20230222.1112900 h1:vnsNTndyHAkbhGqoWfGIKa8Fhs8UM9gcsBYWJj1zJ5c=
github.com/hashicorp/go-azure-sdk v0.20230222.1112900/go.mod h1:bilGy5T79PVRkSDDWvTLP0L/vB1L8+Aj1WlMnp5jUqA=
github.com/hashicorp/go-azure-sdk v0.20230223.1132808 h1:3RTwCTiY42qQjN8rzi4QABWzYyMEp8ZqdWf7K7XzAXo=
github.com/hashicorp/go-azure-sdk v0.20230223.1132808/go.mod h1:lU3F9c+qXc7iqMayTNmUP/VDc0H6f95mkhv4u35zV8I=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
Expand Down
36 changes: 36 additions & 0 deletions msgraph/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,42 @@ func (c *GroupsClient) GetMember(ctx context.Context, groupId, memberId string)
return &data.Id, status, nil
}

// GetMembers retrieves all member of the specified Group, configurable by an OData Query.
// groupId is the object ID of the group.
func (c *GroupsClient) GetMembers(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/members", 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
}

// AddMembers adds new members to a Group.
// First populate the `members` field, then call this method
func (c *GroupsClient) AddMembers(ctx context.Context, group *Group) (int, error) {
Expand Down
15 changes: 15 additions & 0 deletions msgraph/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestGroupsClient(t *testing.T) {
transitiveMembers := testGroupsClient_ListTransitiveMembers(t, c, *group.ID())
testGroupsClient_GetMember(t, c, *group.ID(), (*members)[0])
testGroupsClient_GetMember(t, c, *group.ID(), (*transitiveMembers)[0])
testGroupsClient_GetMembers(t, c, *group.ID(), odata.Query{})

group.DisplayName = utils.StringPtr(fmt.Sprintf("test-updated-group-%s", c.RandomString))
testGroupsClient_Update(t, c, *group)
Expand Down Expand Up @@ -272,6 +273,20 @@ func testGroupsClient_GetMember(t *testing.T, c *test.Test, groupId string, memb
return
}

func testGroupsClient_GetMembers(t *testing.T, c *test.Test, groupId string, query odata.Query) (members *[]msgraph.User) {
members, status, err := c.GroupsClient.GetMembers(c.Context, groupId, query)
if err != nil {
t.Fatalf("GroupsClient.GetMembers(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("GroupsClient.GetMembesr(): invalid status: %d", status)
}
if members == nil {
t.Fatal("GroupsClient.GetMembers(): members was nil")
}
return
}

func testGroupsClient_AddMembers(t *testing.T, c *test.Test, g *msgraph.Group) {
status, err := c.GroupsClient.AddMembers(c.Context, g)
if err != nil {
Expand Down

0 comments on commit 985377d

Please sign in to comment.