diff --git a/msgraph/administrative_units.go b/msgraph/administrative_units.go index aae17677..bc35feb3 100644 --- a/msgraph/administrative_units.go +++ b/msgraph/administrative_units.go @@ -241,15 +241,15 @@ func (c *AdministrativeUnitsClient) GetMember(ctx context.Context, administrativ return &data.Id, status, nil } -func (c *AdministrativeUnitsClient) CreateGroup(ctx context.Context, administrativeUnitId string, group *Group) (int, error) { +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 status, fmt.Errorf("json.Marshal(): %v", err) + return nil, status, fmt.Errorf("json.Marshal(): %v", err) } - _, status, _, err = c.BaseClient.Post(ctx, PostHttpRequestInput{ + response, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{ Body: body, ConsistencyFailureFunc: RetryOn404ConsistencyFailureFunc, ValidStatusCodes: []int{http.StatusCreated}, @@ -259,9 +259,21 @@ func (c *AdministrativeUnitsClient) CreateGroup(ctx context.Context, administrat }, }) if err != nil { - return status, fmt.Errorf("AdministrativeUnitsClient.BaseClient.Post(): %v", err) + return nil, status, fmt.Errorf("AdministrativeUnitsClient.BaseClient.Post(): %v", err) } - return status, nil + + 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. diff --git a/msgraph/administrative_units_test.go b/msgraph/administrative_units_test.go index 9898e453..54b67f35 100644 --- a/msgraph/administrative_units_test.go +++ b/msgraph/administrative_units_test.go @@ -11,6 +11,53 @@ import ( "github.com/manicminer/hamilton/odata" ) +//func TestCreateGroup(t *testing.T) { +//client := test.NewTest(t) +//defer client.CancelFunc() + +//ctx := context.Background() +//auId := "a36b436e-7b28-4e51-a1c1-8553d3e2cd5b" +//var odataId odata.Id = "directoryObjects('3c2e469a-bfdb-4240-ab3c-81c167a05172')" +//var odataType odata.Type = "#microsoft.graph.servicePrincipal" +//ownerString := "3c2e469a-bfdb-4240-ab3c-81c167a05172" +//owner := msgraph.DirectoryObject{ +//ODataId: &odataId, +//ODataType: &odataType, +//Id: &ownerString, +//} +//groupName := "hansi-test" +//var groupDescription msgraph.StringNullWhenEmpty = "hansi-test" +//groupTypes := []string{"Unified"} +//behavior := []msgraph.GroupResourceBehaviorOption{ +//msgraph.GroupResourceBehaviorOptionWelcomeEmailDisabled, +//msgraph.GroupResourceBehaviorOptionSubscribeNewGroupMembers, +//msgraph.GroupResourceBehaviorOptionSubscribeMembersToCalendarEventsDisabled, +//} +//visibility := msgraph.GroupVisibilityPrivate +//falsePointer := false +//truePointer := true +//group := msgraph.Group{ +//Description: &groupDescription, +//DisplayName: &groupName, +//GroupTypes: &groupTypes, +//IsAssignableToRole: &falsePointer, +//MailEnabled: &truePointer, +//MailNickname: &groupName, +//Owners: &msgraph.Owners{ +//owner, +//}, +//ResourceBehaviorOptions: &behavior, +//SecurityEnabled: &truePointer, +//Visibility: &visibility, +//} +//returnValue, err := client.AdministrativeUnitsClient.CreateGroup(ctx, auId, &group) +//if err != nil { +//t.Fatalf("AdministrativeUnit.CreateGroup():%s", err) +//} +//println(returnValue) + +//} + func TestAdministrativeUnitsClient(t *testing.T) { c := test.NewTest(t) defer c.CancelFunc() @@ -41,6 +88,18 @@ 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()})) + directoryRoleTemplates := testDirectoryRoleTemplatesClient_List(t, c) var helpdeskAdministratorRoleId string for _, template := range *directoryRoleTemplates { @@ -177,6 +236,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 < 200 || status >= 300 { + 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 {