Skip to content

Commit

Permalink
Merge pull request #206 from SwissGipfel/main
Browse files Browse the repository at this point in the history
Provide a function to create a group directly into an administrative unit
  • Loading branch information
manicminer authored Jan 27, 2023
2 parents fc6529a + 5dc77b2 commit f6c2d35
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
35 changes: 35 additions & 0 deletions msgraph/administrative_units.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,41 @@ func (c *AdministrativeUnitsClient) GetMember(ctx context.Context, administrativ
return &data.Id, status, nil
}

func (c *AdministrativeUnitsClient) CreateGroup(ctx context.Context, administrativeUnitId string, group *Group) (*Group, int, error) {
var status int
odataTypeGroup := odata.TypeGroup
group.ODataType = &odataTypeGroup
body, err := json.Marshal(group)
if err != nil {
return nil, status, fmt.Errorf("json.Marshal(): %v", err)
}
response, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc,
ValidStatusCodes: []int{http.StatusCreated},
Uri: Uri{
Entity: fmt.Sprintf("/administrativeUnits/%s/members", administrativeUnitId),
HasTenantId: true,
},
})
if err != nil {
return nil, status, fmt.Errorf("AdministrativeUnitsClient.BaseClient.Post(): %v", err)
}

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

var newGroup Group
if err := json.Unmarshal(responseBody, &newGroup); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}

return &newGroup, status, nil
}

// AddMembers adds new members to a AdministrativeUnit.
func (c *AdministrativeUnitsClient) AddMembers(ctx context.Context, administrativeUnitId string, members *Members) (int, error) {
var status int
Expand Down
25 changes: 25 additions & 0 deletions msgraph/administrative_units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package msgraph_test

import (
"fmt"
"net/http"
"strings"
"testing"

Expand Down Expand Up @@ -41,6 +42,19 @@ func TestAdministrativeUnitsClient(t *testing.T) {
testAdministrativeUnitsClient_GetMember(t, c, *administrativeUnit.ID, *user.ID())
testAdministrativeUnitsClient_RemoveMembers(t, c, *administrativeUnit.ID, &([]string{*user.ID()}))

self := testDirectoryObjectsClient_Get(t, c, c.Claims.ObjectId)
group := msgraph.Group{
DisplayName: utils.StringPtr("test-group"),
MailEnabled: utils.BoolPtr(false),
MailNickname: utils.StringPtr(fmt.Sprintf("test-group-%s", c.RandomString)),
SecurityEnabled: utils.BoolPtr(true),
Owners: &msgraph.Owners{*self},
Members: &msgraph.Members{*self},
}
createdGroup := testAdministrativeUnitsClient_CreateGroup(t, c, *administrativeUnit.ID, &group)
testAdministrativeUnitsClient_RemoveMembers(t, c, *administrativeUnit.ID, &([]string{*createdGroup.ID()}))
testGroup_Delete(t, c, createdGroup)

directoryRoleTemplates := testDirectoryRoleTemplatesClient_List(t, c)
var helpdeskAdministratorRoleId string
for _, template := range *directoryRoleTemplates {
Expand Down Expand Up @@ -177,6 +191,17 @@ func testAdministrativeUnitsClient_RemoveMembers(t *testing.T, c *test.Test, adm
}
}

func testAdministrativeUnitsClient_CreateGroup(t *testing.T, c *test.Test, administrativeUnitId string, g *msgraph.Group) (group *msgraph.Group) {
group, status, err := c.AdministrativeUnitsClient.CreateGroup(c.Context, administrativeUnitId, g)
if err != nil {
t.Fatalf("AdministrativeUnitsClient.CreateGroup(): %v", err)
}
if status != http.StatusCreated {
t.Fatalf("AdministrativeUnitsClient.CreateGroup(): invalid status: %d", status)
}
return group
}

func testAdministrativeUnitsClient_ListScopedRoleMembers(t *testing.T, c *test.Test, administrativeUnitId string) (memberships *[]msgraph.ScopedRoleMembership) {
memberships, status, err := c.AdministrativeUnitsClient.ListScopedRoleMembers(c.Context, administrativeUnitId, odata.Query{})
if err != nil {
Expand Down

0 comments on commit f6c2d35

Please sign in to comment.