public async Task DeleteAllStudyRelatedResourcesAsync(Study study, CancellationToken cancellationToken = default) { _logger.LogInformation($"DeleteAllStudyRelatedResourcesAsync - Study Id: {study.Id}"); try { var resourceGroupEntry = await GetResourceGroupEntryForStudySpecificDatasetDeletionAsync(study); if (resourceGroupEntry != null) { var currentUser = await _userService.GetCurrentUserAsync(); SoftDeleteUtil.MarkAsDeleted(resourceGroupEntry, currentUser); foreach (var curResource in resourceGroupEntry.ChildResources) { SoftDeleteUtil.MarkAsDeleted(curResource, currentUser); } var deleteOperation = await _cloudResourceOperationCreateService.CreateDeleteOperationAsync(resourceGroupEntry.Id, $"Delete study related resurces for Study {study.Id}"); await _provisioningQueueService.CreateItemAndEnqueue(deleteOperation); } } catch (Exception ex) { throw new Exception($"Failed to delete resources for study specific dataset", ex); } }
async Task <CloudResourceOperation> EnsureExistsDeleteOperationInternalAsync(string deleteDescription, CloudResource resource) { _logger.LogInformation($"{deleteDescription}: Ensuring delete operation exist"); var deleteOperation = await _cloudResourceOperationReadService.GetUnfinishedDeleteOperation(resource.Id); if (deleteOperation == null) { _logger.LogInformation($"{deleteDescription}: Creating delete operation"); deleteOperation = await _cloudResourceOperationCreateService.CreateDeleteOperationAsync(resource.Id, ResourceOperationDescriptionUtils.CreateDescriptionForResourceOperation( resource.ResourceType, CloudResourceOperationType.DELETE, resource.Id, sandboxId: resource.SandboxId.Value)); } else { _logger.LogInformation($"{deleteDescription}: Existing delete operation found, re-queueing that"); await _cloudResourceOperationUpdateService.ReInitiateAsync(resource.Id); } return(deleteOperation); }
public async Task HandleSandboxDeleteAsync(int sandboxId, EventId eventId) { var sandboxFromDb = await _sandboxModelService.GetWithResourcesNoPermissionCheckAsync(sandboxId); CloudResource sandboxResourceGroup = null; if (sandboxFromDb.Resources.Count > 0) { //Mark all resources as deleted foreach (var curResource in sandboxFromDb.Resources) { if (curResource.ResourceType == AzureResourceType.ResourceGroup) { sandboxResourceGroup = curResource; } _logger.LogInformation(eventId, "Study {0}, Sandbox {1}: Marking resource {2} for deletion", sandboxFromDb.StudyId, sandboxId, curResource.Id); await _cloudResourceDeleteService.MarkAsDeletedAsync(curResource.Id); } if (sandboxResourceGroup == null) { throw new Exception($"Unable to find ResourceGroup record in DB for Sandbox {sandboxId}, StudyId: {sandboxFromDb.StudyId}."); } _logger.LogInformation(eventId, $"Creating delete operation for resource group {sandboxResourceGroup.ResourceGroupName}"); var deleteOperation = await _cloudResourceOperationCreateService.CreateDeleteOperationAsync(sandboxResourceGroup.Id, ResourceOperationDescriptionUtils.CreateDescriptionForResourceOperation(sandboxResourceGroup.ResourceType, CloudResourceOperationType.DELETE, sandboxId: sandboxResourceGroup.SandboxId.Value) + ". (Delete of Sandbox resource group and all resources within)"); _logger.LogInformation(eventId, "Study {0}, Sandbox {1}: Queuing operation", sandboxFromDb.StudyId, sandboxId); var queueParentItem = QueueItemFactory.CreateParent(deleteOperation); await _provisioningQueueService.SendMessageAsync(queueParentItem, visibilityTimeout : TimeSpan.FromSeconds(10)); } else { _logger.LogCritical(eventId, "Study {0}, Sandbox {1}: Unable to find any resources for Sandbox", sandboxFromDb.StudyId, sandboxId); } }