Ejemplo n.º 1
0
        public async Task <MigrationBatchViewModel> CreateBatchAsync([FromBody] MigrationBatchViewModel batch)
        {
            Customer                    customer;
            CustomerPrincipal           principal;
            DateTime                    startTime;
            Dictionary <string, double> eventMetrics;
            Dictionary <string, string> eventProperties;
            List <MailboxEntity>        entries;
            MigrationBatchEntity        entity;
            string targetDeliveryDomain;

            batch.AssertNotNull(nameof(batch));

            try
            {
                startTime = DateTime.Now;
                principal = (CustomerPrincipal)HttpContext.Current.User;

                if (string.IsNullOrEmpty(batch.Id))
                {
                    batch.Id = Guid.NewGuid().ToString();
                }

                customer = await operations.GetCustomerAsync(principal.CustomerId);

                targetDeliveryDomain = $"{customer.CompanyProfile.Domain.Split('.')[0]}.{MigrationConstants.TargetDeliveryDomainSuffix}";

                entity = new MigrationBatchEntity(batch.EnvironmentId, batch.Id)
                {
                    CustomerId           = principal.CustomerId,
                    ETag                 = "*",
                    Name                 = batch.Name,
                    StartTime            = DateTime.Parse(batch.StartTime),
                    Started              = false,
                    TargetDeliveryDomain = targetDeliveryDomain
                };

                entries = batch.Mailboxes.Select(m => new MailboxEntity(batch.EnvironmentId, m.Guid)
                {
                    DisplayName        = m.DisplayName,
                    ETag               = "*",
                    Name               = m.Name,
                    MigrationBatchId   = batch.Id,
                    PrimarySmtpAddress = m.PrimarySmtpAddress,
                    SamAccountName     = m.SamAccountName,
                    UserPrincipalName  = m.UserPrincipalName
                }).ToList();

                await Service.Storage.WriteToTableAsync(MigrationConstants.MigrationBatchTable, entity);

                await Service.Storage.WriteBatchToTableAsync(MigrationConstants.MailboxTableName, entries);

                await Service.ServiceBus.WriteToQueueAsync(MigrationConstants.MigrationBatchQueueName, entity);

                // Capture the request for the customer summary for analysis.
                eventProperties = new Dictionary <string, string>
                {
                    { "Email", principal.Email },
                    { "EnvironmentId", batch.EnvironmentId },
                    { "MigrationBatchName", batch.Name },
                    { "PrincipalCustomerId", principal.CustomerId }
                };

                // Track the event measurements for analysis.
                eventMetrics = new Dictionary <string, double>
                {
                    { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds },
                    { "NumberOfMailboxes", batch.Mailboxes.Count }
                };

                Service.Telemetry.TrackEvent("/api/migrationbatch/create", eventProperties, eventMetrics);

                return(batch);
            }
            finally
            {
                entity          = null;
                eventMetrics    = null;
                eventProperties = null;
                principal       = null;
            }
        }
Ejemplo n.º 2
0
        public async Task <MigrationBatchesViewModel> DeleteMigrationBatchAsync([FromBody] MigrationBatchViewModel migrationBatchViewModel)
        {
            CustomerPrincipal           principal;
            DateTime                    startTime;
            Dictionary <string, double> eventMetrics;
            Dictionary <string, string> eventProperties;
            EnvironmentEntity           environment;
            List <MailboxEntity>        mailboxes;
            MigrationBatchEntity        entity;
            MigrationBatchesViewModel   migrationBatches;

            migrationBatchViewModel.AssertNotNull(nameof(migrationBatchViewModel));

            try
            {
                startTime = DateTime.Now;
                principal = (CustomerPrincipal)HttpContext.Current.User;

                environment = await Service.Storage.GetEntityAsync <EnvironmentEntity>(
                    MigrationConstants.EnvironmentTableName,
                    principal.CustomerId,
                    migrationBatchViewModel.EnvironmentId);

                entity = await Service.Storage.GetEntityAsync <MigrationBatchEntity>(
                    MigrationConstants.MigrationBatchTable,
                    migrationBatchViewModel.EnvironmentId,
                    migrationBatchViewModel.Id);

                if (environment == null || entity == null)
                {
                    throw new EntityNotFoundException(Resources.EntityNotFoundException);
                }

                mailboxes = await Service.Storage.GetEntitiesAsync <MailboxEntity>(
                    MigrationConstants.MailboxTableName,
                    m => m.PartitionKey.Equals(environment.RowKey) &&
                    !string.IsNullOrEmpty(m.MigrationBatchId) &&
                    m.MigrationBatchId.Equals(entity.RowKey));

                if (mailboxes?.Count > 0)
                {
                    foreach (MailboxEntity mailbox in mailboxes)
                    {
                        mailbox.MigrationBatchId = string.Empty;
                    }

                    await Service.Storage.WriteBatchToTableAsync(
                        MigrationConstants.MailboxTableName,
                        mailboxes);
                }

                await Service.ServiceBus.WriteToQueueAsync(
                    MigrationConstants.MigrationBatchDeleteQueueName,
                    entity);

                await Service.Storage.DeleteEntityAsync(
                    MigrationConstants.MigrationBatchTable,
                    entity);

                migrationBatches = await GetMigrationBatchesAsync(migrationBatchViewModel.EnvironmentId);

                // Capture the request for the customer summary for analysis.
                eventProperties = new Dictionary <string, string>
                {
                    { "Email", principal.Email },
                    { "EnvironmentId", migrationBatchViewModel.EnvironmentId },
                    { "MigrationBatchName", entity.Name },
                    { "PrincipalCustomerId", principal.CustomerId }
                };

                // Track the event measurements for analysis.
                eventMetrics = new Dictionary <string, double>
                {
                    { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds },
                    { "NumberOfBatches", migrationBatches.MigrationBatches.Count }
                };

                Service.Telemetry.TrackEvent("/api/migrationbatch/delete", eventProperties, eventMetrics);

                return(migrationBatches);
            }
            finally
            {
                entity          = null;
                environment     = null;
                eventMetrics    = null;
                eventProperties = null;
                mailboxes       = null;
                principal       = null;
            }
        }