Exemple #1
0
 protected Region GetRegionFromString(string regionName)
 {
     return(RegionStringConverter.Convert(regionName));
 }
Exemple #2
0
        public AutoMappingConfigs()
        {
            //STUDY
            CreateMap <Study, StudyDto>()
            .ForMember(dest => dest.Participants, source => source.MapFrom(x => x.StudyParticipants));

            CreateMap <Study, StudyDetailsDto>()
            .ForMember(dest => dest.Datasets, source => source.MapFrom(x => x.StudyDatasets.Select(y => y.Dataset).Where(d => !d.Deleted).ToList()))
            .ForMember(dest => dest.Sandboxes, source => source.MapFrom(x => x.Sandboxes.Where(sb => !sb.Deleted).ToList()))
            .ForMember(dest => dest.Participants, source => source.MapFrom(x => x.StudyParticipants));

            CreateMap <StudyDetailsDapper, StudyDetailsDto>()
            .ForMember(dest => dest.Id, source => source.MapFrom(x => x.StudyId));
            CreateMap <Study, StudyListItemDto>();
            CreateMap <StudyDto, Study>();
            CreateMap <StudyCreateDto, Study>();

            CreateMap <Study, IHasStudyPermissionDetails>()
            .ForMember(dest => dest.StudyId, source => source.MapFrom(x => x.Id))
            .ForMember(dest => dest.Restricted, source => source.MapFrom(x => x.Restricted))
            .ForMember(dest => dest.UsersAndRoles, source => source.MapFrom <StudyToStudyPermissionDetailsResolver>());

            CreateMap <StudyDetailsDto, IHasStudyPermissionDetails>()
            .ForMember(dest => dest.StudyId, source => source.MapFrom(x => x.Id))
            .ForMember(dest => dest.Restricted, source => source.MapFrom(x => x.Restricted))
            .ForMember(dest => dest.UsersAndRoles, source => source.MapFrom <StudyDetailsToStudyPermissionDetailsResolver>());

            //DATASET

            CreateMap <DatasetCreateUpdateInputBaseDto, Dataset>();

            CreateMap <Dataset, DatasetDto>()
            .ForMember(dest => dest.Studies, source => source.MapFrom(x => x.StudyDatasets.Select(y => y.Study).ToList()))
            .ForMember(dest => dest.StorageAccountName, source => source.MapFrom <DatasetStorageAccountNameResolver>())
            .ForMember(dest => dest.StorageAccountLink, source => source.MapFrom <StorageAccountExternalLinkResolver>());

            CreateMap <Dataset, DatasetLookupItemDto>();

            CreateMap <DatasetForStudyDetailsDapper, DatasetListItemDto>()
            .ForMember(dest => dest.Id, source => source.MapFrom(x => x.DatasetId))
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.DatasetName))
            .ForMember(dest => dest.Sandboxes, source => source.MapFrom(x => x.Sandboxes));

            CreateMap <Dataset, DatasetListItemDto>()
            .ForMember(dest => dest.StudyId, source => source.MapFrom(ds => ds.StudySpecific ? ds.StudyDatasets.SingleOrDefault().StudyId : default(int?)))
            .ForMember(dest => dest.Sandboxes, source => source.MapFrom(x => x.SandboxDatasets.Where(sd => !sd.Sandbox.Deleted).Select(sd => sd.Sandbox).ToList()));

            CreateMap <StudyDataset, StudyDatasetDto>()
            .ForMember(dest => dest.Id, source => source.MapFrom(x => x.Dataset.Id))
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.Dataset.Name))
            .ForMember(dest => dest.DataId, source => source.MapFrom(x => x.Dataset.DataId))
            .ForMember(dest => dest.Classification, source => source.MapFrom(x => x.Dataset.Classification));

            CreateMap <SandboxDataset, SandboxDatasetDto>()
            .ForMember(dest => dest.DatasetId, source => source.MapFrom(x => x.Dataset.Id))
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.Dataset.Name))
            .ForMember(dest => dest.Classification, source => source.MapFrom(x => x.Dataset.Classification))
            .ForMember(dest => dest.SandboxName, opt =>
            {
                opt.PreCondition(src => (!src.Sandbox.Deleted));
                opt.MapFrom(src =>

                            src.Sandbox.Name
                            );
            })
            .ForMember(dest => dest.SandboxId, source => source.MapFrom(x => x.Sandbox.Id))
            .ForMember(dest => dest.StudyId, source => source.MapFrom(x => x.Sandbox.StudyId));

            CreateMap <CloudResource, DatasetResourceLightDto>()
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.ResourceName))
            .ForMember(dest => dest.Type, source => source.MapFrom(x => AzureResourceTypeUtil.GetUserFriendlyName(x)))
            .ForMember(dest => dest.Status, source => source.MapFrom(x => ResourceStatusUtil.ResourceStatus(x)))
            .ForMember(dest => dest.LinkToExternalSystem, source => source.MapFrom <StorageAccountResourceExternalLinkResolver>())
            .ForMember(dest => dest.RetryLink, source => source.MapFrom <DatasetResourceRetryLinkResolver>());


            //SANDBOX
            CreateMap <Sandbox, SandboxDto>()
            .ForMember(dest => dest.StudyName, source => source.MapFrom(x => x.Study.Name))
            .ForMember(dest => dest.CurrentPhase, source => source.MapFrom <SandboxPhaseNameResolver>());


            CreateMap <Sandbox, SandboxListItem>();

            CreateMap <SandboxForStudyDetailsDapper, SandboxListItem>()
            .ForMember(dest => dest.Id, source => source.MapFrom(x => x.SandboxId))
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.SandboxName))
            .ForMember(dest => dest.StudyId, source => source.MapFrom(x => x.StudyId));

            CreateMap <Sandbox, SandboxDetails>()
            .ForMember(dest => dest.StudyName, source => source.MapFrom(x => x.Study.Name))
            .ForMember(dest => dest.Datasets, source => source.MapFrom(x => x.SandboxDatasets))
            .ForMember(dest => dest.LinkToCostAnalysis, source => source.MapFrom <SandboxResourceExternalCostAnalysis>())
            .ForMember(dest => dest.CurrentPhase, source => source.MapFrom <SandboxPhaseNameResolver>());

            CreateMap <SandboxCreateDto, Sandbox>();

            CreateMap <CloudResource, SandboxResourceLight>()
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.ResourceName))
            .ForMember(dest => dest.Type, source => source.MapFrom(x => AzureResourceTypeUtil.GetUserFriendlyName(x)))
            .ForMember(dest => dest.Status, source => source.MapFrom(x => ResourceStatusUtil.ResourceStatus(x)))
            .ForMember(dest => dest.LinkToExternalSystem, source => source.MapFrom <SandboxResourceExternalLinkResolver>())
            .ForMember(dest => dest.RetryLink, source => source.MapFrom <SandboxResourceRetryLinkResolver>())
            .ForMember(dest => dest.AdditionalProperties, source => source.MapFrom <SandboxResourceAdditionalPropertiesResolver>())
            ;


            //CLOUD RESOURCE

            CreateMap <CloudResource, CloudResourceDto>()
            .ForMember(dest => dest.Tags, source => source.MapFrom(x => TagUtils.TagStringToDictionary(x.Tags)))
            .ForMember(dest => dest.SandboxName, source => source.MapFrom(s => s.Sandbox.Name))
            .ForMember(dest => dest.StudyName, source => source.MapFrom(s => s.Sandbox.Study.Name));


            CreateMap <CloudResourceDto, CloudResource>()
            .ForMember(dest => dest.Tags, source => source.MapFrom(x => TagUtils.TagDictionaryToString(x.Tags)));

            CreateMap <CloudResourceOperation, CloudResourceOperationDto>();
            CreateMap <CloudResourceOperationDto, CloudResourceOperation>();

            //USERS/PARTICIPANTS

            CreateMap <User, UserDto>().ReverseMap();

            CreateMap <User, ParticipantLookupDto>()
            .ForMember(dest => dest.Source, source => source.MapFrom(s => ParticipantSource.Db))
            .ForMember(dest => dest.DatabaseId, source => source.MapFrom(s => s.Id));

            CreateMap <Microsoft.Graph.User, AzureUserDto>();

            CreateMap <Microsoft.Graph.User, ParticipantLookupDto>()
            .ForMember(dest => dest.FullName, source => source.MapFrom(x => x.DisplayName))
            .ForMember(dest => dest.EmailAddress, source => source.MapFrom(x => x.Mail))
            .ForMember(dest => dest.ObjectId, source => source.MapFrom(x => x.Id))
            .ForMember(dest => dest.UserName, source => source.MapFrom(x => x.UserPrincipalName))
            .ForMember(dest => dest.Source, source => source.MapFrom(s => ParticipantSource.Azure));

            CreateMap <StudyParticipant, StudyParticipantDto>()
            .ForMember(dest => dest.EmailAddress, source => source.MapFrom(x => x.User.EmailAddress))
            .ForMember(dest => dest.FullName, source => source.MapFrom(x => x.User.FullName))
            .ForMember(dest => dest.UserName, source => source.MapFrom(x => x.User.UserName))
            .ForMember(dest => dest.Role, source => source.MapFrom(x => x.RoleName));

            CreateMap <StudyParticipant, StudyParticipantListItem>()
            .ForMember(dest => dest.EmailAddress, source => source.MapFrom(x => x.User.EmailAddress))
            .ForMember(dest => dest.FullName, source => source.MapFrom(x => x.User.FullName))
            .ForMember(dest => dest.UserName, source => source.MapFrom(x => x.User.UserName))
            .ForMember(dest => dest.Role, source => source.MapFrom(x => x.RoleName));

            CreateMap <StudyParticipantForStudyDetailsDapper, StudyParticipantListItem>()
            .ForMember(dest => dest.Role, source => source.MapFrom(x => x.Role));


            //AZURE
            CreateMap <IResource, AzureResourceDto>();

            CreateMap <IResourceGroup, AzureResourceGroupDto>()
            .ForMember(dest => dest.ProvisioningState, source => source.MapFrom(x => x.ProvisioningState));

            CreateMap <IStorageAccount, AzureStorageAccountDto>()
            .ForMember(dest => dest.ProvisioningState, source => source.MapFrom(x => x.ProvisioningState));

            CreateMap <VirtualMachineCreateDto, VmSettingsDto>();

            CreateMap <CloudResourceDto, VmDto>()
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.ResourceName))
            .ForMember(dest => dest.Region, source => source.MapFrom(x => RegionStringConverter.Convert(x.Region).Name));

            CreateMap <CloudResource, VmDto>()
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.ResourceName))
            .ForMember(dest => dest.Region, source => source.MapFrom(x => RegionStringConverter.Convert(x.Region).Name))
            .ForMember(dest => dest.Status, source => source.MapFrom(x => ResourceStatusUtil.ResourceStatus(x)))
            .ForMember(dest => dest.OperatingSystem, source => source.MapFrom(x => VmOsUtil.GetOsName(x)))
            .ForMember(dest => dest.LinkToExternalSystem, source => source.MapFrom <SandboxResourceExternalLinkResolver>());


            CreateMap <VmRuleDto, NsgRuleDto>()
            .ForMember(dest => dest.Protocol, source => source.MapFrom(x => x.Protocol))
            .ForMember(dest => dest.Description, source => source.MapFrom(x => x.Description));


            CreateMap <AzureRegionDto, LookupDto>();

            CreateMap <VmSize, VmSizeDto>()
            .ForMember(dest => dest.Name, source => source.MapFrom(x => x.Key));

            CreateMap <VmSize, VmSizeLookupDto>()
            .ForMember(dest => dest.DisplayValue, source => source.MapFrom(x => x.DisplayText));
        }
        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);
            }
        }