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

Support to assign license for group #288

Closed
Closed
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
38 changes: 38 additions & 0 deletions msgraph/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,3 +747,41 @@ func (c *GroupsClient) ListAdministrativeUnitMemberships(ctx context.Context, id

return &data.Members, status, nil
}

func (c *GroupsClient) AssignLicense(ctx context.Context, groupId string, addLicenses *[]GroupAssignedLicense, removeLicenses *[]string) (*Group, int, error) {
var status int

body, err := json.Marshal(struct {
AddLicenses *[]GroupAssignedLicense `json:"addLicenses"`
RemoveLicenses *[]string `json:"removeLicenses"`
}{
AddLicenses: addLicenses,
RemoveLicenses: removeLicenses,
})
if err != nil {
return nil, status, fmt.Errorf("json.Marshal(): %v", err)
}

resp, status, _, err := c.BaseClient.Post(ctx, PostHttpRequestInput{
Body: body,
ValidStatusCodes: []int{http.StatusAccepted},
Uri: Uri{
Entity: fmt.Sprintf("/groups/%s/assignLicense", groupId),
},
})
if err != nil {
return nil, status, fmt.Errorf("GroupsClient.BaseClient.Post(): %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 group Group
if err := json.Unmarshal(respBody, &group); err != nil {
return nil, status, fmt.Errorf("json.Unmarshal(): %v", err)
}

return &group, status, nil
}
29 changes: 29 additions & 0 deletions msgraph/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func TestGroupsClient(t *testing.T) {
testGroupsClient_AddMembers(t, c, group)
testGroupsClient_RemoveMembers(t, c, *group.ID(), &([]string{c.Claims.ObjectId}))

testGroupsClient_AssignLicense(t, c, *group.ID(), &[]msgraph.GroupAssignedLicense{
{
DisabledPlans: &[]string{},
SkuId: utils.StringPtr("90d8b3f8-712e-4f7b-aa1e-62e7ae6cbe96"), // SMB_APPS
Copy link
Contributor Author

@kenchan0130 kenchan0130 Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GroupsClient.AssignLicense(): GroupsClient.BaseClient.Post(): unexpected status 400 with OData error: Request_BadRequest: License 90d8b3f8-712e-4f7b-aa1e-62e7ae6cbe96 does not correspond to a valid company License.

Please let me know the appropriate SKUS ID as I did not know the proper ID to use for the test.

ref: https://learn.microsoft.com/en-us/entra/identity/users/licensing-service-plan-reference

},
}, &[]string{})
testGroupsClient_AssignLicense(t, c, *group.ID(), &[]msgraph.GroupAssignedLicense{}, &[]string{
"90d8b3f8-712e-4f7b-aa1e-62e7ae6cbe96", // SMB_APPS
})

testGroupsClient_List(t, c)
testGroupsClient_Delete(t, c, *group.ID())
testUsersClient_Delete(t, c, *user.ID())
Expand Down Expand Up @@ -369,3 +379,22 @@ func testGroupsClient_RestoreDeleted(t *testing.T, c *test.Test, id string) {
t.Fatal("GroupsClient.RestoreDeleted(): group IDs do not match")
}
}

func testGroupsClient_AssignLicense(t *testing.T, c *test.Test, id string, addLicenses *[]msgraph.GroupAssignedLicense, removeLicenses *[]string) {
group, status, err := c.GroupsClient.AssignLicense(c.Context, id, addLicenses, removeLicenses)
if err != nil {
t.Fatalf("GroupsClient.AssignLicense(): %v", err)
}
if status < 200 || status >= 300 {
t.Fatalf("GroupsClient.AssignLicense(): invalid status: %d", status)
}
if group == nil {
t.Fatal("GroupsClient.AssignLicense(): group was nil")
}
if group.ID() == nil {
t.Fatal("GroupsClient.AssignLicense(): group.ID was nil")
}
if *group.ID() != id {
t.Fatal("GroupsClient.AssignLicense(): group IDs do not match")
}
}
Loading