예제 #1
0
        public async Task DeleteQueues(DataCleanupParameters parameters)
        {
            _log.LogDebug("Queue deletion starting.");

            try
            {
                QueueServiceClient queueServiceClient = _queueManager.CreateQueueServiceClient(parameters.StorageConnectionString);
                List <string>      queues             = await _queueManager.GetQueues(queueServiceClient);

                var queueDeleteTasks = new List <Task>();
                if (queues != null)
                {
                    foreach (var queue in queues)
                    {
                        queueDeleteTasks.Add(_queueManager.DeleteQueue(queueServiceClient, queue));
                    }
                    await Task.WhenAll(queueDeleteTasks);
                }

                _log.LogDebug($"Queue deletion completed! Removed {queueDeleteTasks.Count} queues.");
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Exception encountered in DeleteQueues method.");
                throw;
            }

            return;
        }
예제 #2
0
        public async Task DataCleanup_DeleteTables_NoTablesExist_ShouldWork()
        {
            // Setup
            CloudTableClient tableClient = TableHelper.CreateCloudTableClient(StorageConnectionString);

            var parameters = new DataCleanupParameters()
            {
                StorageConnectionString = StorageConnectionString
            };
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");

            // - Removes pre-existing tables
            IEnumerable <CloudTable> tableResult1 = tableClient.ListTables();

            if (tableResult1.Count() != 0)
            {
                HttpResponseMessage response1 = await _clientFixture.GetClient().PostAsync(_UrlDeleteTables, content);

                response1.StatusCode.Should().Be(HttpStatusCode.OK);
                tableResult1 = tableClient.ListTables();
                tableResult1.Should().HaveCount(0);
            }

            // Act
            HttpResponseMessage response2 = await _clientFixture.GetClient().PostAsync(_UrlDeleteTables, content);

            // Verify
            response2.StatusCode.Should().Be(HttpStatusCode.OK);
            IEnumerable <CloudTable> tableResult2 = tableClient.ListTables();

            tableResult2.Should().HaveCount(0);
        }
예제 #3
0
        public void ServiceLayer_DeleteDomainTopic_DataLayerException_ShouldRethrow(bool getClientThrowsException, bool deleteThrowsException)
        {
            // Setup
            _eventGridManager.ThrowExceptionOnGetClient = getClientThrowsException;
            _eventGridManager.ThrowExceptionOnDelete    = deleteThrowsException;

            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                _queueManager,
                _tableManager,
                _log);

            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();

            parameters.DomainTopicName = "domaintopicname";
            Func <Task> act = async() => await serviceLayer.DeleteDomainTopic(parameters);

            // Act
            act.Should().Throw <RequestFailedException>().WithMessage(_eventGridManager.ExceptionMessage);

            // Verify
            _log.Messages.Should().HaveCount(2);
            _log.Messages[0].Message.Should().Be($"Deleting domain topic {parameters.DomainTopicName}");
            _log.Messages[1].Message.Should().Be("Exception encountered in DeleteDomainTopic method.");
        }
예제 #4
0
        public async Task DeleteDomainTopic(DataCleanupParameters parameters)
        {
            _log.LogDebug($"Deleting domain topic {parameters.DomainTopicName}");

            try
            {
                EventGridManagementClient eventGridManagementClient = await _eventGridManager.GetEventGridManagementClient(
                    parameters.SubscriptionId,
                    parameters.ServicePrincipalClientId,
                    parameters.ServicePrincipalClientKey,
                    string.Concat(@"https://login.windows.net/", parameters.ServicePrincipalTenantId),
                    @"https://management.azure.com/");

                await _eventGridManager.DeleteDomainTopic(
                    eventGridManagementClient,
                    parameters.ResourceGroupName,
                    parameters.EventGridName,
                    parameters.DomainTopicName);
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Exception encountered in DeleteDomainTopic method.");
                throw;
            }

            _log.LogDebug($"Domain topic deletion completed! {parameters.DomainTopicName}");

            return;
        }
예제 #5
0
        public async Task DeleteTables(DataCleanupParameters parameters)
        {
            _log.LogDebug("Table deletion starting.");

            try
            {
                CloudTableClient  tableClient    = _tableManager.CreateCloudTableClient(parameters.StorageConnectionString);
                List <CloudTable> cloudTableList = await _tableManager.GetTableListAsync(tableClient);

                var tableDeleteTasks = new List <Task>();
                if (cloudTableList != null)
                {
                    foreach (CloudTable table in cloudTableList)
                    {
                        tableDeleteTasks.Add(_tableManager.DeleteIfExists(table));
                    }
                    await Task.WhenAll(tableDeleteTasks);
                }

                _log.LogDebug($"Table deletion completed! Removed {tableDeleteTasks.Count} tables.");
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Exception encountered in DeleteTables method.");
                throw;
            }

            return;
        }
예제 #6
0
        public async Task DataCleanup_DeleteQueues_ShouldWork()
        {
            // Setup
            QueueClient queueClient = await SetupTestQueue();

            var queueHelper = new QueueHelper(StorageConnectionString);

            (await queueHelper.GetQueues()).Should().HaveCountGreaterOrEqualTo(1);

            var parameters = new DataCleanupParameters()
            {
                StorageConnectionString = StorageConnectionString
            };
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");

            // Act
            HttpResponseMessage response = await _clientFixture.GetClient().PostAsync(_UrlDeleteQueues, content);

            // Verify
            response.StatusCode.Should().Be(HttpStatusCode.OK);
            bool queueExists = await queueClient.ExistsAsync();

            queueExists.Should().BeFalse();
            (await queueHelper.GetQueues()).Should().HaveCount(0);
        }
예제 #7
0
        public async Task <IActionResult> InitializeCleanup(
            [HttpTrigger(AuthorizationLevel.Function, "post")] DataCleanupParameters parameters)
        {
            Task queueDeleteTask      = _serviceLayer.DeleteQueues(parameters);
            Task tableDeleteTask      = DeleteTables(parameters);
            Task domainTopicQueryTask = _serviceLayer.PopulateDomainTopicQueue(parameters);

            await Task.WhenAll(queueDeleteTask, tableDeleteTask, domainTopicQueryTask);

            return(new OkResult());
        }
예제 #8
0
        public async Task DomainTopicCleanup([QueueTrigger(QueueNameToDelete)] string queueMessage)
        {
            DataCleanupParameters parameters = JsonSerializer.Deserialize <DataCleanupParameters>(queueMessage);

            if (string.IsNullOrWhiteSpace(parameters.DomainTopicName))
            {
                throw new ArgumentException($"{nameof(parameters.DomainTopicName)} value is invalid.");
            }

            await _serviceLayer.DeleteDomainTopic(parameters);
        }
예제 #9
0
        public async Task <IActionResult> DeleteTables(
            [HttpTrigger(AuthorizationLevel.Function, "post")] DataCleanupParameters parameters)
        {
            if (string.IsNullOrWhiteSpace(parameters.StorageConnectionString))
            {
                return(new BadRequestResult());
            }

            await _serviceLayer.DeleteTables(parameters);

            return(new OkResult());
        }
예제 #10
0
        public async Task DataCleanup_DeleteTables_NullOrWhitespaceStorageConnectionString_ThrowsException(string storageConnectionString)
        {
            // Setup
            var dataCleanup = new Functions(_ServiceLayer);
            DataCleanupParameters parameters = new DataCleanupParameters()
            {
                StorageConnectionString = storageConnectionString
            };

            // Act
            var result = await dataCleanup.DeleteTables(parameters);

            // Verify
            result.Should().BeOfType <BadRequestResult>();
            _ServiceLayer.DeleteTablesCalled.Should().BeFalse();
        }
예제 #11
0
        public void ServiceLayer_DeleteTables_DataLayerException_ShouldRethrow(Mock <ITableManager> tableManagerMock, string exceptionMessage)
        {
            // Setup
            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                _queueManager,
                tableManagerMock.Object,
                _log);

            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();
            Func <Task>           act        = async() => await serviceLayer.DeleteTables(parameters);

            // Act
            act.Should().Throw <RequestFailedException>().WithMessage(exceptionMessage);

            // Verify
            _log.Messages.Should().HaveCount(2);
            _log.Messages[0].Message.Should().Be("Table deletion starting.");
            _log.Messages[1].Message.Should().Be("Exception encountered in DeleteTables method.");
        }
예제 #12
0
        public void ServiceLayer_PopulateDomainTopicQueue_QueueException_ShouldRethrow(Mock <IQueueManager> queueManagerMock, string exceptionMessage)
        {
            // Setup
            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                queueManagerMock.Object,
                _tableManager,
                _log);

            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();
            Func <Task>           act        = async() => await serviceLayer.PopulateDomainTopicQueue(parameters);

            // Act
            act.Should().Throw <RequestFailedException>().WithMessage(exceptionMessage);

            // Verify
            _log.Messages.Should().HaveCountGreaterOrEqualTo(2);
            _log.Messages[0].Message.Should().Be("Queuing domain topics for deletion.");
            _log.Messages[_log.Messages.Count - 1].Message.Should().Be("Exception encountered in PopulateDomainTopicQueue method.");
        }
예제 #13
0
        public async Task DataCleanup_DeleteQueues_BadStorageConnectionString_ReturnsBadRequest(string connectionString)
        {
            // Setup
            QueueClient queueClient = await SetupTestQueue();

            var parameters = new DataCleanupParameters()
            {
                StorageConnectionString = connectionString
            };
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");

            // Act
            HttpResponseMessage response = await _clientFixture.GetClient().PostAsync(_UrlDeleteQueues, content);

            // Verify
            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
            bool queueExists = await queueClient.ExistsAsync();

            queueExists.Should().BeTrue();
        }
예제 #14
0
        public async Task DataCleanup_DeleteTables_ShouldWork()
        {
            // Setup
            CloudTableClient tableClient = await SetupTestTable();

            var parameters = new DataCleanupParameters()
            {
                StorageConnectionString = StorageConnectionString
            };
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");

            // Act
            HttpResponseMessage response = await _clientFixture.GetClient().PostAsync(_UrlDeleteTables, content);

            // Verify
            response.StatusCode.Should().Be(HttpStatusCode.OK);
            IEnumerable <CloudTable> tableResult = tableClient.ListTables();

            tableResult.Should().HaveCount(0);
        }
예제 #15
0
        public async Task ServiceLayer_DeleteDomainTopic_ShouldWork()
        {
            // Setup
            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();

            parameters.DomainTopicName = "domaintopicname";

            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                _queueManager,
                _tableManager,
                _log);

            // Act
            await serviceLayer.DeleteDomainTopic(parameters);

            // Verify
            _log.Messages.Should().HaveCount(2);
            _log.Messages[0].Message.Should().Be($"Deleting domain topic {parameters.DomainTopicName}");
            _log.Messages[1].Message.Should().Be($"Domain topic deletion completed! {parameters.DomainTopicName}");
        }
예제 #16
0
        public async Task ServiceLayer_PopulateDomainTopicQueue_ShouldWork()
        {
            // Setup
            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();

            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                _queueManager,
                _tableManager,
                _log);

            // Act
            await serviceLayer.PopulateDomainTopicQueue(parameters);

            // Verify
            _log.Messages.Should().HaveCount(6);
            _log.Messages[0].Message.Should().Be("Queuing domain topics for deletion.");
            _log.Messages[1].Message.Should().Be($"Found {_eventGridManager.ReturnDomainTopicNames[0][0]}");
            _log.Messages[2].Message.Should().Be($"Found {_eventGridManager.ReturnDomainTopicNames[0][1]}");
            _log.Messages[3].Message.Should().Be($"Found {_eventGridManager.ReturnDomainTopicNames[0][2]}");
            _log.Messages[4].Message.Should().Be($"{_eventGridManager.ReturnDomainTopicNames[0].Count} domain topics being added to queue for deletion.");
            _log.Messages[5].Message.Should().Be("Finished processing page of domain topics.");
        }
예제 #17
0
        public void ServiceLayer_PopulateDomainTopicQueue_EventGridException_ShouldRethrow(bool getClientThrowsException, bool getTopicsThrowsException)
        {
            // Setup
            _eventGridManager.ThrowExceptionOnGetClient = getClientThrowsException;
            _eventGridManager.ThrowExceptionOnGetTopics = getTopicsThrowsException;

            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                _queueManager,
                _tableManager,
                _log);

            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();
            Func <Task>           act        = async() => await serviceLayer.PopulateDomainTopicQueue(parameters);

            // Act
            act.Should().Throw <RequestFailedException>().WithMessage(_eventGridManager.ExceptionMessage);

            // Verify
            _log.Messages.Should().HaveCount(2);
            _log.Messages[0].Message.Should().Be("Queuing domain topics for deletion.");
            _log.Messages[1].Message.Should().Be("Exception encountered in PopulateDomainTopicQueue method.");
        }
예제 #18
0
        public async Task ServiceLayer_PopulateDomainTopicQueue_MultiplePages_ShouldWork()
        {
            // Setup
            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();

            _eventGridManager.ReturnDomainTopicNames = new List <List <string> >()
            {
                new List <string>()
                {
                    "domaintopic1"
                },
                new List <string>()
                {
                    "domaintopic2"
                },
            };

            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                _queueManager,
                _tableManager,
                _log);

            // Act
            await serviceLayer.PopulateDomainTopicQueue(parameters);

            // Verify
            _log.Messages.Should().HaveCount(5);
            _log.Messages[0].Message.Should().Be("Queuing domain topics for deletion.");
            _log.Messages[1].Message.Should().Be($"Found {_eventGridManager.ReturnDomainTopicNames[0][0]}");
            _log.Messages[2].Message.Should().Be($"{_eventGridManager.ReturnDomainTopicNames[0].Count} domain topics being added to queue for deletion.");
            _log.Messages[3].Message.Should().Be("Added next domain topic page to queue.");
            _log.Messages[4].Message.Should().Be("Finished processing page of domain topics.");

            _queueManager.SentMessages.Should().HaveCount(2);
            _queueManager.SentMessages[1].Should().Contain("\"domainTopicNextPage\":\"Nextpage\"");
        }
예제 #19
0
        public async Task DataCleanup_DeleteQueues_NoQueuesExist_ShouldWork()
        {
            // Setup
            var parameters = new DataCleanupParameters()
            {
                StorageConnectionString = StorageConnectionString
            };
            var content = new StringContent(JsonSerializer.Serialize(parameters), Encoding.UTF8, "application/json");

            // - Removes pre-existing tables
            HttpResponseMessage response = await _clientFixture.GetClient().PostAsync(_UrlDeleteQueues, content);

            response.StatusCode.Should().Be(HttpStatusCode.OK);
            var queueHelper = new QueueHelper(StorageConnectionString);

            (await queueHelper.GetQueues()).Should().HaveCount(0, "Did not expect queues to exist after first delete");

            // Act
            HttpResponseMessage response2 = await _clientFixture.GetClient().PostAsync(_UrlDeleteQueues, content);

            // Verify
            response2.StatusCode.Should().Be(HttpStatusCode.OK);
            (await queueHelper.GetQueues()).Should().HaveCount(0, "Did not expect queues to exist after second delete");
        }
예제 #20
0
        public async Task ServiceLayer_DeleteQueues_UsingMoq_ShouldWork(List <string> queueList)
        {
            // Setup
            (Mock <IQueueManager> queueManagerMock, List <string> deletedQueueList) = GetMockQueueManager(queueList);

            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                queueManagerMock.Object,
                _tableManager,
                _log);

            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();

            // Act
            await serviceLayer.DeleteQueues(parameters);

            // Verify
            queueManagerMock.Verify();
            deletedQueueList.Count.Should().Be(queueList?.Count ?? 0);
            deletedQueueList.Should().BeEquivalentTo(queueList ?? new List <string>());
            _log.Messages.Should().HaveCount(2);
            _log.Messages[0].Message.Should().Be("Queue deletion starting.");
            _log.Messages[1].Message.Should().Be($"Queue deletion completed! Removed {queueList?.Count ?? 0} queues.");
        }
예제 #21
0
        public async Task ServiceLayer_DeleteTables_ShouldWork(List <CloudTable> tableList)
        {
            // Setup
            (Mock <ITableManager> tableManagerMock, List <CloudTable> deletedTableList) = GetMockTableManager(tableList);

            ServiceLayer serviceLayer = new ServiceLayer(
                _eventGridManager,
                _queueManager,
                tableManagerMock.Object,
                _log);

            DataCleanupParameters parameters = TestValues.GetDataCleanupParameters();

            // Act
            await serviceLayer.DeleteTables(parameters);

            // Verify
            tableManagerMock.Verify();
            deletedTableList.Should().HaveCount(tableList?.Count ?? 0);
            deletedTableList.Should().BeEquivalentTo(tableList ?? new List <CloudTable>());
            _log.Messages.Should().HaveCount(2);
            _log.Messages[0].Message.Should().Be("Table deletion starting.");
            _log.Messages[1].Message.Should().Be($"Table deletion completed! Removed {tableList?.Count ?? 0} tables.");
        }
예제 #22
0
        public async Task DomainTopicList([QueueTrigger(QueueNameToList)] string queueMessage)
        {
            DataCleanupParameters parameters = JsonSerializer.Deserialize <DataCleanupParameters>(queueMessage);

            await _serviceLayer.PopulateDomainTopicQueue(parameters);
        }
예제 #23
0
 public Task PopulateDomainTopicQueue(DataCleanupParameters parameters)
 {
     PopulateDomainTopicQueueCalled = true;
     LastParametersPassed           = parameters;
     return(Task.CompletedTask);
 }
예제 #24
0
 public Task DeleteTables(DataCleanupParameters parameters)
 {
     DeleteTablesCalled = true;
     return(Task.CompletedTask);
 }
예제 #25
0
 public Task DeleteDomainTopic(DataCleanupParameters parameters)
 {
     DeleteDomainTopicCalled = true;
     LastParametersPassed    = parameters;
     return(Task.CompletedTask);
 }
예제 #26
0
        public async Task PopulateDomainTopicQueue(DataCleanupParameters parameters)
        {
            _log.LogDebug("Queuing domain topics for deletion.");

            try
            {
                string      storageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage") ?? "UseDevelopmentStorage=true";
                QueueClient queuePendingDelete      = _queueManager.CreateQueueClient(storageConnectionString, QueueNameHoldingDomainTopicsToDelete);
                await _queueManager.CreateIfNotExistsAsync(queuePendingDelete);

                EventGridManagementClient eventGridManagementClient = await _eventGridManager.GetEventGridManagementClient(
                    parameters.SubscriptionId,
                    parameters.ServicePrincipalClientId,
                    parameters.ServicePrincipalClientKey,
                    string.Concat(@"https://login.windows.net/", parameters.ServicePrincipalTenantId),
                    @"https://management.azure.com/");

                (List <string> domainTopicNames, string nextPageLink) = await _eventGridManager.GetDomainTopics(
                    eventGridManagementClient,
                    parameters.ResourceGroupName,
                    parameters.EventGridName,
                    parameters.DomainTopicNextpage);

                var queueAddTasks = new List <Task>();
                foreach (var domainTopic in domainTopicNames)
                {
                    parameters.DomainTopicName = domainTopic;
                    string deleteMessage = JsonSerializer.Serialize(parameters);
                    queueAddTasks.Add(_queueManager.SendMessageAsync(queuePendingDelete, deleteMessage));

                    _log.LogDebug($"Found {domainTopic}");
                }

                _log.LogDebug($"{queueAddTasks.Count} domain topics being added to queue for deletion.");

                // If there is another page of domain topics, then add a task to the queue to parse it.
                if (!string.IsNullOrWhiteSpace(nextPageLink))
                {
                    QueueClient queuePendingList = _queueManager.CreateQueueClient(storageConnectionString, QueueNameHoldingDomainTopicsToList);
                    await _queueManager.CreateIfNotExistsAsync(queuePendingList);

                    parameters.DomainTopicNextpage = nextPageLink;
                    parameters.DomainTopicName     = null;
                    string nextPageParameters = JsonSerializer.Serialize(parameters);
                    queueAddTasks.Add(_queueManager.SendMessageAsync(queuePendingList, nextPageParameters));

                    _log.LogDebug("Added next domain topic page to queue.");
                }

                await Task.WhenAll(queueAddTasks);
            }
            catch (Exception ex)
            {
                _log.LogError(ex, "Exception encountered in PopulateDomainTopicQueue method.");
                throw;
            }

            _log.LogDebug("Finished processing page of domain topics.");

            return;
        }