/// <summary> /// Load 1 or more Groups members from Azure and create in memory groups for them /// </summary> /// <param name="azureGraph"></param> /// <param name="thisGroupAsSet"></param> /// <param name="groupToRetrieve"></param> /// <returns></returns> private async Task GenerateGroupsMembersList_ProcessGroups( GraphServiceClient azureGraph, IGraphServiceGroupsCollectionPage thisGroupAsSet, ProvisionConfigExternalDirectorySync.ISynchronizeGroupToGroup groupToRetrieve) { //Degenerate case: No data here... if ((thisGroupAsSet == null) || (thisGroupAsSet.CurrentPage.Count < 1)) { return; } //============================================================================================ //Get all the groups from the current page of Azure results, and then get any subsequent pages //============================================================================================ do { //Process a;l the groups on this results page var currentPage = thisGroupAsSet.CurrentPage; var currentPage_ItemCount = currentPage.Count; //---------------------------------------------------------------------- //Loop through all the Azure Groups in the current returned page //---------------------------------------------------------------------- for (var idxGroup = 0; idxGroup < currentPage_ItemCount; idxGroup++) { await GenerateGroupsMembersList_ProcessSingleGroup(azureGraph, currentPage[idxGroup], groupToRetrieve); } //----------------------------------------------------------------------- //Advance to the next page (if there is one) //----------------------------------------------------------------------- var requestNextPage = thisGroupAsSet.NextPageRequest; if (requestNextPage != null) { thisGroupAsSet = await requestNextPage.GetAsync(); } else { thisGroupAsSet = null; } } while (thisGroupAsSet != null); //Until we have no more Azure Group Pages to look through }
/// <summary> /// Process a single top level Azure AD group /// </summary> /// <param name="azureGraph"></param> /// <param name="azureGroup"></param> /// <param name="groupSyncInstructions"></param> /// <returns></returns> private async Task GenerateGroupsMembersList_ProcessSingleGroup(GraphServiceClient azureGraph, Group azureGroup, ProvisionConfigExternalDirectorySync.ISynchronizeGroupToGroup groupSyncInstructions) { //---------------------------------------------------------------------------------------------------------------------------------------------- //See if there is an additional 'contains' filter we need to apply to the result //---------------------------------------------------------------------------------------------------------------------------------------------- if (!string.IsNullOrWhiteSpace(groupSyncInstructions.FilterSourceGroupNameContains)) { //If the Azure Group does not contain the specified Contains fitering term, then skip it if (!azureGroup.DisplayName.Contains(groupSyncInstructions.FilterSourceGroupNameContains)) { _statusLogs.AddStatus("Skipping groups sync members of group: '" + azureGroup.DisplayName + "', becuase the group name does not contain the filter term '" + groupSyncInstructions.FilterSourceGroupNameContains + "'"); return; } } _statusLogs.AddStatus("Loading members of Azure group '" + azureGroup.DisplayName + "' for sync group '" + groupSyncInstructions.SourceGroupName + "'"); //============================================================== //Get/Create the membership manager for the group //============================================================== var singleGroupMembershipManager = SetManagerForGroups.GetManagerForGroup_CreateIfNeeded( groupSyncInstructions.GenerateTargetGroupName(azureGroup.DisplayName), groupSyncInstructions.GrantLicenseInstructions, groupSyncInstructions.GrantLicenseRole); var thiGroupId = azureGroup.Id; //https://docs.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=http //UNDONE: Improve performance by filtering down to just USERS and SUB-GROUPS var thisGroupsMembers = await azureGraph.Groups[thiGroupId].Members.Request().GetAsync(); //TEST: Test paging by forcing small page size //var thisGroupsMembers = await azureGraph.Groups[thiGroupId].Members.Request().Top(2).GetAsync(); //Get all the users in the group and sub-groups await AzureRecurseGroupsGenerateGroupMembersList(azureGraph, thisGroupsMembers, singleGroupMembershipManager); }