public async Task <CloudResourceOperationDto> CreateUpdateOperationAsync(int sandboxResourceId, string operationType = CloudResourceOperationType.UPDATE, int dependsOn = 0, string batchId = null, string desiredState = null)
        {
            var sandboxResourceFromDb = await GetResourceOrThrowAsync(sandboxResourceId);

            var currentUser = await _userService.GetCurrentUserAsync();

            if (dependsOn == 0)
            {
                var mustWaitFor = await CheckAnyIfOperationsToWaitFor(sandboxResourceFromDb, currentUser);

                if (mustWaitFor != null)
                {
                    dependsOn = mustWaitFor.Id;
                }
            }

            var newOperation = await CreateUpdateOperationAsync(
                ResourceOperationDescriptionUtils.CreateDescriptionForResourceOperation(sandboxResourceFromDb.ResourceType, operationType, sandboxResourceId),
                operationType,
                dependsOn,
                batchId,
                desiredState);

            sandboxResourceFromDb.Operations.Add(newOperation);
            await _db.SaveChangesAsync();

            return(await GetOperationDtoInternal(newOperation.Id));
        }
        public static CloudResource CreateStudyResourceGroupEntry(UserDto currentUser, string sessionId,
                                                                  int studyId, string region, string resourceGroupName, Dictionary <string, string> tags
                                                                  )
        {
            var resourceType = AzureResourceType.ResourceGroup;
            var newResource  = CreateBasicResource(currentUser, sessionId, region, resourceType, resourceGroupName, tags, resourceGroupName);

            newResource.StudyId = studyId;
            newResource.Purpose = CloudResourcePurpose.StudySpecificDatasetContainer;

            var createOperationDescription = ResourceOperationDescriptionUtils.CreateDescriptionForResourceOperation(resourceType, CloudResourceOperationType.CREATE, studyId: studyId);

            SetOperationProperties(newResource, createOperationDescription);

            return(newResource);
        }
        public static CloudResource CreateSandboxResourceGroupEntry(UserDto currentUser, string sessionId,
                                                                    int sandboxId, string region, string resourceGroupName, Dictionary <string, string> tags,
                                                                    string batchId
                                                                    )
        {
            var resourceType = AzureResourceType.ResourceGroup;
            var newResource  = CreateBasicResource(currentUser, sessionId, region, resourceType, resourceGroupName, tags, resourceGroupName);

            newResource.SandboxId         = sandboxId;
            newResource.SandboxControlled = true;
            newResource.Purpose           = CloudResourcePurpose.SandboxResourceGroup;

            var createOperationDescription = ResourceOperationDescriptionUtils.CreateDescriptionForResourceOperation(resourceType, CloudResourceOperationType.CREATE, sandboxId: sandboxId);

            SetOperationProperties(newResource, createOperationDescription, batchId);

            return(newResource);
        }
        public static CloudResource CreateStudySpecificDatasetStorageAccountEntry(UserDto currentUser, string sessionId, int datasetId,
                                                                                  string region, int resourceGroupId, string resourceGroupName, string resourceName, Dictionary <string, string> tags, int dependsOnId
                                                                                  )
        {
            var resourceType = AzureResourceType.StorageAccount;
            var newResource  = CreateBasicResource(currentUser, sessionId, region, resourceType, resourceName, tags, resourceGroupName);

            newResource.DatasetId        = datasetId;
            newResource.ParentResourceId = resourceGroupId;

            newResource.Purpose = CloudResourcePurpose.StudySpecificDatasetStorageAccount;

            var createOperationDescription = ResourceOperationDescriptionUtils.CreateDescriptionForResourceOperation(resourceType, CloudResourceOperationType.CREATE);

            SetOperationProperties(newResource, createOperationDescription, operationDependsOn: dependsOnId);

            return(newResource);
        }
        public static CloudResource CreateSandboxResourceEntry(UserDto currentUser, string sessionId, int sandboxId, string region, string resourceType, int resourceGroupId, string resourceName, Dictionary <string, string> tags,
                                                               string configString      = null,
                                                               string batchId           = null,
                                                               int dependsOn            = 0,
                                                               string resourceGroupName = null, bool sandboxControlled = true
                                                               )
        {
            var newResource = CreateBasicResource(currentUser, sessionId, region, resourceType, resourceName, tags, resourceGroupName);

            newResource.SandboxId         = sandboxId;
            newResource.SandboxControlled = sandboxControlled;
            newResource.ParentResourceId  = resourceGroupId;
            newResource.Purpose           = CloudResourcePurpose.SandboxResource;
            newResource.ConfigString      = configString;

            var createOperationDescription = ResourceOperationDescriptionUtils.CreateDescriptionForResourceOperation(resourceType, CloudResourceOperationType.CREATE, sandboxId: sandboxId);

            SetOperationProperties(newResource, createOperationDescription, batchId, dependsOn);

            return(newResource);
        }
Beispiel #6
0
        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);
            }
        }
Beispiel #7
0
        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);
        }