Пример #1
0
        public void StudySpecificDatasourceStorageAccountTags_ShouldReturnCorrectValues()
        {
            var config = AzureResourceTagsFactory_Factory.GetConfiguration(_serviceProvider);
            List <StudyParticipant> participants = new List <StudyParticipant>();
            var user = new User()
            {
                Id = 1, FullName = "John Doe", EmailAddress = "*****@*****.**"
            };

            participants.Add(new StudyParticipant()
            {
                RoleName = "Study Owner", User = user
            });
            var study = new Study()
            {
                Name = "Study1", WbsCode = "123", StudyParticipants = participants
            };

            var res = ResourceTagFactory.StudySpecificDatasourceStorageAccountTags(config, study, "dataset1");

            var expectedResultStudy       = "Study1";
            var expectedResultOwnerName   = "John Doe";
            var expectedResultOwnerEmail  = "*****@*****.**";
            var expectedResultDatasetName = "dataset1";

            Assert.Equal(expectedResultStudy, res["StudyName"]);
            Assert.Equal(expectedResultOwnerName, res["StudyOwnerName"]);
            Assert.Equal(expectedResultOwnerEmail, res["StudyOwnerEmail"]);
            Assert.Equal(expectedResultDatasetName, res["DatasetName"]);
        }
        public async Task CreateResourceGroupForStudySpecificDatasetsAsync(Study study, CancellationToken cancellationToken = default)
        {
            var studyForCreation = await _studyModelService.GetForDatasetCreationNoAccessCheckAsync(study.Id);

            var resourceGroupForDatasets = GetResourceGroupForStudySpecificDataset(studyForCreation);

            var parentQueueItem = QueueItemFactory.CreateParent("Create resource group for Study specific datasets");

            if (resourceGroupForDatasets == null)
            {
                var resourceGroupName = AzureResourceNameUtil.StudySpecificDatasetResourceGroup(studyForCreation.Name);
                var tags = ResourceTagFactory.StudySpecificDatasourceResourceGroupTags(_config, studyForCreation);
                resourceGroupForDatasets = await _cloudResourceCreateService.CreateStudySpecificResourceGroupEntryAsync(studyForCreation.Id, resourceGroupName, "norwayeast", tags);

                ProvisioningQueueUtil.CreateChildAndAdd(parentQueueItem, resourceGroupForDatasets);
            }
            else
            {
                throw new Exception("Resource group allready exists");
            }

            await ScheduleResourceGroupRoleAssignments(studyForCreation, resourceGroupForDatasets, parentQueueItem);

            await _provisioningQueueService.SendMessageAsync(parentQueueItem, cancellationToken : cancellationToken);
        }
        async Task <CloudResource> CreateResourceGroupForStudySpecificDatasetsInternalAsync(Study study, ProvisioningQueueParentDto parentQueueItem)
        {
            var resourceGroupName = AzureResourceNameUtil.StudySpecificDatasetResourceGroup(study.Name);
            var tags = ResourceTagFactory.StudySpecificDatasourceResourceGroupTags(_config, study);
            var resourceGroupForDatasets = await _cloudResourceCreateService.CreateStudySpecificResourceGroupEntryAsync(study.Id, resourceGroupName, "norwayeast", tags);

            ProvisioningQueueUtil.CreateChildAndAdd(parentQueueItem, resourceGroupForDatasets);

            await ScheduleResourceGroupRoleAssignments(study, resourceGroupForDatasets, parentQueueItem);

            return(resourceGroupForDatasets);
        }
        async Task OrderCreationOfStudySpecificDatasetStorageAccount(Study study, Dataset dataset, CloudResource resourceGroup, string clientIp, ProvisioningQueueParentDto queueParent, CancellationToken cancellationToken)
        {
            try
            {
                if (resourceGroup == null)
                {
                    throw new ArgumentNullException("resourceGroup", "Resource group entry is null");
                }

                _logger.LogInformation($"CreateResourcesForStudySpecificDataset - Dataset Id: {dataset.Id}");

                var currentUser = await _userService.GetCurrentUserAsync();

                var tagsForStorageAccount = ResourceTagFactory.StudySpecificDatasourceStorageAccountTags(_config, study, dataset.Name);
                var storageAccountName    = AzureResourceNameUtil.StudySpecificDataSetStorageAccount(dataset.Name);

                var resourceEntry = await _cloudResourceCreateService.CreateStudySpecificDatasetEntryAsync(dataset.Id, resourceGroup.Id, resourceGroup.Region, resourceGroup.ResourceGroupName, storageAccountName, tagsForStorageAccount);

                ProvisioningQueueUtil.CreateChildAndAdd(queueParent, resourceEntry);

                var serverPublicIp = await _publicIpService.GetIp();

                DatasetFirewallUtils.EnsureDatasetHasFirewallRules(_logger, currentUser, dataset, clientIp, serverPublicIp);

                await _db.SaveChangesAsync();

                var stateForFirewallOperation = DatasetFirewallUtils.TranslateAllowedIpsToOperationDesiredState(dataset.FirewallRules.ToList());

                var createStorageAccountOperation = CloudResourceOperationUtil.GetCreateOperation(resourceEntry);
                var firewallUpdateOperation       = await _cloudResourceOperationCreateService.CreateUpdateOperationAsync(resourceEntry.Id, CloudResourceOperationType.ENSURE_FIREWALL_RULES, dependsOn : createStorageAccountOperation.Id, desiredState : stateForFirewallOperation);

                ProvisioningQueueUtil.CreateChildAndAdd(queueParent, firewallUpdateOperation);

                var stateForCorsRules   = DatasetCorsUtils.CreateDatasetCorsRules(_config);
                var corsUpdateOperation = await _cloudResourceOperationCreateService.CreateUpdateOperationAsync(resourceEntry.Id, CloudResourceOperationType.ENSURE_CORS_RULES, dependsOn : firewallUpdateOperation.Id, desiredState : stateForCorsRules);

                ProvisioningQueueUtil.CreateChildAndAdd(queueParent, corsUpdateOperation);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to schedule creation of Azure Storage Account", ex);
            }
        }
Пример #5
0
        public async Task CreateBasicSandboxResourcesAsync(Sandbox sandbox)
        {
            _logger.LogInformation($"Creating basic sandbox resources for sandbox: {sandbox.Name}. First creating Resource Group, other resources are created by worker");

            try
            {
                var tags = ResourceTagFactory.SandboxResourceTags(_configuration, sandbox.Study, sandbox);

                var creationAndSchedulingDto =
                    new SandboxResourceCreationAndSchedulingDto()
                {
                    StudyId     = sandbox.Study.Id,
                    SandboxId   = sandbox.Id,
                    StudyName   = sandbox.Study.Name,
                    SandboxName = sandbox.Name,
                    Region      = sandbox.Region,
                    Tags        = tags,
                    BatchId     = Guid.NewGuid().ToString()
                };

                var queueParentItem = new ProvisioningQueueParentDto
                {
                    Description = $"Create basic resources for Sandbox: {creationAndSchedulingDto.SandboxId}"
                };

                await ScheduleCreationOfSandboxResourceGroup(creationAndSchedulingDto, queueParentItem);
                await ScheduleCreationOfSandboxResourceGroupRoleAssignments(creationAndSchedulingDto, queueParentItem);
                await ScheduleCreationOfDiagStorageAccount(creationAndSchedulingDto, queueParentItem);
                await ScheduleCreationOfNetworkSecurityGroup(creationAndSchedulingDto, queueParentItem);
                await ScheduleCreationOfVirtualNetwork(creationAndSchedulingDto, queueParentItem);
                await ScheduleCreationOfBastion(creationAndSchedulingDto, queueParentItem);

                await _provisioningQueueService.SendMessageAsync(queueParentItem);

                _logger.LogInformation($"Done ordering creation of basic resources for sandbox: {creationAndSchedulingDto.SandboxName}");
            }
            catch (Exception ex)
            {
                throw new Exception($"Unable to create basic sandbox resources.", ex);
            }
        }
        public async Task <VmDto> CreateAsync(int sandboxId, VirtualMachineCreateDto userInput)
        {
            CloudResource vmResourceEntry = null;

            try
            {
                ValidateVmPasswordOrThrow(userInput.Password);

                GenericNameValidation.ValidateName(userInput.Name);

                _logger.LogInformation($"Creating Virtual Machine for sandbox: {sandboxId}");

                var sandbox = await _sandboxModelService.GetByIdForResourceCreationAsync(sandboxId, UserOperation.Study_Crud_Sandbox);

                var virtualMachineName = AzureResourceNameUtil.VirtualMachine(sandbox.Study.Name, sandbox.Name, userInput.Name);

                await _cloudResourceCreateService.ValidateThatNameDoesNotExistThrowIfInvalid(virtualMachineName);

                var tags = ResourceTagFactory.SandboxResourceTags(_config, sandbox.Study, sandbox);

                var region = RegionStringConverter.Convert(sandbox.Region);

                userInput.DataDisks = await TranslateDiskSizes(sandbox.Region, userInput.DataDisks);

                var resourceGroup = await CloudResourceQueries.GetResourceGroupEntry(_db, sandboxId);

                //Make this dependent on bastion create operation to be completed, since bastion finishes last
                var dependsOn = await CloudResourceQueries.GetCreateOperationIdForBastion(_db, sandboxId);

                vmResourceEntry = await _cloudResourceCreateService.CreateVmEntryAsync(sandboxId, resourceGroup, region.Name, tags, virtualMachineName, dependsOn, null);

                //Create vm settings and immeately attach to resource entry
                var vmSettingsString = await CreateVmSettingsString(sandbox.Region, vmResourceEntry.Id, sandbox.Study.Id, sandboxId, userInput);

                vmResourceEntry.ConfigString = vmSettingsString;
                await _cloudResourceUpdateService.Update(vmResourceEntry.Id, vmResourceEntry);

                var queueParentItem = new ProvisioningQueueParentDto
                {
                    Description = $"Create VM for Sandbox: {sandboxId}"
                };

                queueParentItem.Children.Add(new ProvisioningQueueChildDto()
                {
                    ResourceOperationId = vmResourceEntry.Operations.FirstOrDefault().Id
                });

                await _provisioningQueueService.SendMessageAsync(queueParentItem);

                var dtoMappedFromResource = _mapper.Map <VmDto>(vmResourceEntry);

                return(dtoMappedFromResource);
            }
            catch (Exception ex)
            {
                try
                {
                    //Delete resource if created
                    if (vmResourceEntry != null)
                    {
                        await _cloudResourceDeleteService.HardDeleteAsync(vmResourceEntry.Id);
                    }
                }
                catch (Exception rollbackEx)
                {
                    _logger.LogError(rollbackEx, $"Failed to roll back VM creation for sandbox {sandboxId}");
                }

                throw new Exception($"Failed to create VM: {ex.Message}", ex);
            }
        }