Updated MS Graph plugin to use larger page sizes

Signed-off-by: Alex Crome <afscrome@users.noreply.github.com>
This commit is contained in:
Alex Crome
2023-01-22 21:56:39 +00:00
parent 58d9145eef
commit c5b119ad9c
4 changed files with 64 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-msgraph': patch
---
Increased default page size to 999 (from 100) to reduce the number of calls made to the Microsoft Graph API.
@@ -348,8 +348,16 @@ export class MicrosoftGraphClient {
* @param groupId - The unique identifier for the `Group` resource
*
*/
async *getGroupMembers(groupId: string): AsyncIterable<GroupMember> {
yield* this.requestCollection<GroupMember>(`groups/${groupId}/members`);
async *getGroupMembers(
groupId: string,
query?: ODataQuery,
queryMode?: 'basic' | 'advanced',
): AsyncIterable<GroupMember> {
yield* this.requestCollection<GroupMember>(
`groups/${groupId}/members`,
query,
queryMode,
);
}
/**
@@ -140,6 +140,7 @@ describe('read microsoft graph', () => {
expect(client.getUsers).toHaveBeenCalledWith(
{
filter: 'accountEnabled eq true',
top: 999,
},
undefined,
);
@@ -186,6 +187,7 @@ describe('read microsoft graph', () => {
expect(client.getUsers).toHaveBeenCalledWith(
{
filter: 'accountEnabled eq true',
top: 999,
},
'advanced',
);
@@ -228,6 +230,7 @@ describe('read microsoft graph', () => {
{
expand: 'manager',
filter: 'accountEnabled eq true',
top: 999,
},
undefined,
);
@@ -278,11 +281,14 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq true',
top: 999,
},
undefined,
);
expect(client.getGroupMembers).toHaveBeenCalledTimes(1);
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid');
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid', {
top: 999,
});
expect(client.getUserProfile).toHaveBeenCalledTimes(1);
expect(client.getUserProfile).toHaveBeenCalledWith('userid', {
@@ -338,11 +344,14 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq true',
top: 999,
},
'advanced',
);
expect(client.getGroupMembers).toHaveBeenCalledTimes(1);
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid');
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid', {
top: 999,
});
expect(client.getUserProfile).toHaveBeenCalledTimes(1);
expect(client.getUserProfile).toHaveBeenCalledWith('userid', {
@@ -390,11 +399,14 @@ describe('read microsoft graph', () => {
{
expand: 'member',
filter: 'securityEnabled eq true',
top: 999,
},
undefined,
);
expect(client.getGroupMembers).toHaveBeenCalledTimes(1);
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid');
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid', {
top: 999,
});
expect(client.getUserProfile).toHaveBeenCalledTimes(1);
expect(client.getUserProfile).toHaveBeenCalledWith('userid', {
@@ -527,11 +539,14 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq false',
top: 999,
},
undefined,
);
expect(client.getGroupMembers).toHaveBeenCalledTimes(1);
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid');
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid', {
top: 999,
});
// TODO: Loading groups photos doesn't work right now as Microsoft Graph
// doesn't allows this yet
// expect(client.getGroupPhotoWithSizeLimit).toBeCalledTimes(1);
@@ -603,11 +618,14 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq false',
top: 999,
},
'advanced',
);
expect(client.getGroupMembers).toHaveBeenCalledTimes(1);
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid');
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid', {
top: 999,
});
// TODO: Loading groups photos doesn't work right now as Microsoft Graph
// doesn't allows this yet
// expect(client.getGroupPhotoWithSizeLimit).toBeCalledTimes(1);
@@ -680,11 +698,14 @@ describe('read microsoft graph', () => {
{
expand: 'member',
filter: 'securityEnabled eq false',
top: 999,
},
undefined,
);
expect(client.getGroupMembers).toHaveBeenCalledTimes(1);
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid');
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid', {
top: 999,
});
// TODO: Loading groups photos doesn't work right now as Microsoft Graph
// doesn't allows this yet
// expect(client.getGroupPhotoWithSizeLimit).toBeCalledTimes(1);
@@ -750,11 +771,14 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq true',
top: 999,
},
undefined,
);
expect(client.getGroupMembers).toHaveBeenCalledTimes(1);
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid');
expect(client.getGroupMembers).toHaveBeenCalledWith('groupid', {
top: 999,
});
});
});
@@ -887,6 +911,7 @@ describe('read microsoft graph', () => {
expect(client.getUsers).toHaveBeenCalledWith(
{
filter: undefined,
top: 999,
},
undefined,
);
@@ -894,6 +919,7 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq false',
top: 999,
},
undefined,
);
@@ -925,6 +951,7 @@ describe('read microsoft graph', () => {
{
expand: 'manager',
filter: 'accountEnabled eq true',
top: 999,
},
undefined,
);
@@ -932,6 +959,7 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq false',
top: 999,
},
undefined,
);
@@ -960,6 +988,7 @@ describe('read microsoft graph', () => {
expect(client.getUsers).toHaveBeenCalledWith(
{
select: ['mail'],
top: 999,
},
undefined,
);
@@ -994,12 +1023,14 @@ describe('read microsoft graph', () => {
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'name eq backstage-group',
top: 999,
},
undefined,
);
expect(client.getGroups).toHaveBeenCalledWith(
{
filter: 'securityEnabled eq false',
top: 999,
},
undefined,
);
@@ -37,6 +37,8 @@ import {
UserTransformer,
} from './types';
const PAGE_SIZE = 999;
/**
* The default implementation of the transformation from a graph user entry to
* a User entity.
@@ -107,6 +109,7 @@ export async function readMicrosoftGraphUsers(
filter: options.userFilter,
expand: options.userExpand,
select: options.userSelect,
top: PAGE_SIZE,
},
options.queryMode,
)) {
@@ -172,13 +175,16 @@ export async function readMicrosoftGraphUsersInGroups(
expand: options.groupExpand,
search: options.userGroupMemberSearch,
filter: options.userGroupMemberFilter,
top: PAGE_SIZE,
},
options.queryMode,
)) {
// Process all groups in parallel, otherwise it can take quite some time
userGroupMemberPromises.push(
limiter(async () => {
for await (const member of client.getGroupMembers(group.id!)) {
for await (const member of client.getGroupMembers(group.id!, {
top: PAGE_SIZE,
})) {
if (!member.id) {
continue;
}
@@ -380,6 +386,7 @@ export async function readMicrosoftGraphGroups(
search: options?.groupSearch,
filter: options?.groupFilter,
select: options?.groupSelect,
top: PAGE_SIZE,
},
options?.queryMode,
)) {
@@ -401,7 +408,9 @@ export async function readMicrosoftGraphGroups(
return;
}
for await (const member of client.getGroupMembers(group.id!)) {
for await (const member of client.getGroupMembers(group.id!, {
top: PAGE_SIZE,
})) {
if (!member.id) {
continue;
}