Exemple #1
0
        private async Task <List <AzureADUser> > GetUsersForEachGroup(IEnumerable <AzureADGroup> groups, Guid runId)
        {
            if (!groups.Any())
            {
                return(null);
            }

            var toReturn = new List <AzureADUser>();

            foreach (var group in groups)
            {
                if (await _graphGroupRepository.GroupExists(group.ObjectId))
                {
                    await _log.LogMessageAsync(new LogMessage { RunId = runId, Message = $"Reading users from the group with ID {group.ObjectId}." });

                    var users = await _graphGroupRepository.GetUsersInGroupTransitively(group.ObjectId);

                    var newUsers = users.Except(toReturn).ToArray();
                    await _log.LogMessageAsync(new LogMessage { RunId = runId, Message = $"Got {users.Count} users from the group with ID {group.ObjectId}. " +
                                                                                         $"The group contains {users.Count - newUsers.Length} users who have already been read from earlier groups." });

                    toReturn.AddRange(newUsers);
                }
                else
                {
                    await _log.LogMessageAsync(new LogMessage { RunId = runId, Message = $"Group with ID {group.ObjectId} doesn't exist. Stopping sync and marking as error." });

                    return(null);
                }
            }

            return(toReturn);
        }
        public async Task <PolicyResult <bool> > GroupExistsAsync(Guid objectId, Guid runId)
        {
            // make this fresh every time because the lambda has to capture the run ID
            _graphRetryPolicy = Policy.Handle <SocketException>().WaitAndRetryAsync(NumberOfGraphRetries, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                                                                                    onRetry: async(ex, count) =>
            {
                await _log.LogMessageAsync(new LogMessage
                {
                    Message = $"Got a transient SocketException. Retrying. This was try {count} out of {NumberOfGraphRetries}.\n" + ex.ToString(),
                    RunId   = runId
                });
            });

            return(await _graphRetryPolicy.ExecuteAndCaptureAsync(() => _graphGroupRepository.GroupExists(objectId)));
        }