public async Task Process()

in Common/Migration/Phase1/PreProcessors/IdentityPreProcessor.cs [43:115]


        public async Task Process(IBatchMigrationContext batchContext)
        {
            object identityObject = null;
            string identityValue = null;
            HashSet<string> identitiesToProcess = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

            foreach (var sourceWorkItem in batchContext.SourceWorkItems)
            {
                foreach (var field in context.IdentityFields)
                {
                    if (sourceWorkItem.Fields.TryGetValueIgnoringCase(field, out identityObject))
                    {
                        identityValue = (string)identityObject;
                        if (!string.IsNullOrEmpty(identityValue)
                            && identityValue.Contains("<") && identityValue.Contains(">") && (identityValue.Contains("@")))
                        {
                            // parse out email address from the combo string
                            identityValue = identityValue.Substring(identityValue.LastIndexOf("<") + 1, identityValue.LastIndexOf(">") - identityValue.LastIndexOf("<") - 1);

                            if (!identitiesToProcess.Contains(identityValue)
                                && !this.context.ValidatedIdentities.Contains(identityValue)
                                && !this.context.InvalidIdentities.Contains(identityValue))
                            {
                                Logger.LogTrace(LogDestination.File, $"Found identity {identityValue} in batch {batchContext.BatchId} which has not yet been validated for the target account");
                                identitiesToProcess.Add(identityValue);
                            }
                        }
                    }
                }
            }

            Logger.LogTrace(LogDestination.File, $"Adding {identitiesToProcess.Count} identities to the account for batch {batchContext.BatchId}");
            foreach (var identity in identitiesToProcess)
            {
                try
                {
                    var createUserResult = await RetryHelper.RetryAsync(async () =>
                    {
                        return await graphClient.CreateUserAsync(new GraphUserPrincipalNameCreationContext()
                        {
                            PrincipalName = identity
                        });
                    }, 5);

                    // using identity from createUserResult since the identity could be in a mangled format that ReadIdentities does not support
                    var identities = await RetryHelper.RetryAsync(async () =>
                    {
                        return await identityHttpClient.ReadIdentitiesAsync(IdentitySearchFilter.MailAddress, createUserResult.MailAddress);
                    }, 5);

                    if (identities.Count == 0)
                    {
                        Logger.LogWarning(LogDestination.File, $"Unable to add identity {identity} to the target account for batch {batchContext.BatchId}");
                        context.InvalidIdentities.Add(identity);
                    }
                    else
                    {
                        var assignResult = await RetryHelper.RetryAsync(async () =>
                        {
                            return await licensingHttpClient.AssignAvailableEntitlementAsync(identities[0].Id, dontNotifyUser: true);
                        }, 5);
                        context.ValidatedIdentities.Add(identity);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogWarning(LogDestination.File, ex, $"Unable to add identity {identity} to the target account for batch {batchContext.BatchId}");
                    context.InvalidIdentities.Add(identity);
                }
            }

            Logger.LogTrace(LogDestination.File, $"Completed adding {identitiesToProcess.Count} identities to the account for batch {batchContext.BatchId}");
        }