public static ProjectServiceTemplate Create(string name, ConfigurationManagementService serviceCMSType, CloudProviderService serviceCPSType, string description, string url, string logo, PipeType pipeType, TemplateType templateType, TemplateAccess templateAccess, bool needCredentials, Guid programmingLanguageId, string framework, string createdBy) { var entity = new ProjectServiceTemplate() { Name = name, ServiceCMSType = serviceCMSType, ServiceCPSType = serviceCPSType, Description = description, Url = url, Logo = logo, PipeType = pipeType, TemplateType = templateType, TemplateAccess = templateAccess, NeedCredentials = needCredentials, ProgrammingLanguageId = programmingLanguageId, Framework = framework, CreatedBy = createdBy }; var validationResult = new DataValidatorManager <ProjectServiceTemplate>().Build().Validate(entity); if (!validationResult.IsValid) { throw new ApplicationException(validationResult.Errors); } return(entity); }
public async Task CreateProjectService(Guid organizationId, Guid projectId, ProjectServicePostRp resource, string userId = null) { string loggedUserId = userId ?? _identityService.GetUserId(); User user = await _userRepository.GetUser(loggedUserId); DomainModels.ProjectServiceTemplate projectServiceTemplate = await _projectServiceTemplateRepository.GetProjectServiceTemplateById(resource.ProjectServiceTemplateId); if (projectServiceTemplate == null) { await _domainManagerService.AddConflict($"The pipe template with id {resource.ProjectServiceTemplateId} does not exists."); return; } DomainModels.Organization organization = user.FindOrganizationById(organizationId); if (organization == null) { await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists."); return; } DomainModels.Project project = user.FindProjectById(projectId, false); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return; } if (project.Status != EntityStatus.Active) { await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to add a new service."); return; } DomainModels.ProjectService existingService = project.GetServiceByName(resource.Name); if (existingService != null) { await _domainManagerService.AddConflict($"The pipe name {resource.Name} has already been taken."); return; } CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(project.OrganizationCMS.Type).GetToken( _dataProtectorService.Unprotect(project.OrganizationCMS.AccountId), _dataProtectorService.Unprotect(project.OrganizationCMS.AccountName), _dataProtectorService.Unprotect(project.OrganizationCMS.AccessSecret), _dataProtectorService.Unprotect(project.OrganizationCMS.AccessToken)); CMSServiceAvailabilityResultModel cmsServiceAvailability = await _cmsService(project.OrganizationCMS.Type).ValidateServiceAvailability(cmsAuthCredential, project.OrganizationExternalId, project.ProjectExternalId, project.Name, resource.RepositoryName); if (!cmsServiceAvailability.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsServiceAvailability.GetReasonForNoSuccess()}"); return; } DomainModels.ProjectService newService = user.CreateProjectService(organizationId, projectId, project.OrganizationCMSId, resource.AgentPoolId, resource.Name, resource.RepositoryName, resource.Description, resource.ProjectServiceTemplateId, projectServiceTemplate.PipeType); //SaveChanges in CMS CMSServiceCreateModel serviceCreateModel = CMSServiceCreateModel.Factory.Create(project.OrganizationExternalId, project.ProjectExternalId, project.Name, resource.RepositoryName, project.ProjectVisibility == ProjectVisibility.Public ? true : false); CMSServiceCreateResultModel cmsServiceCreate = await _cmsService(project.OrganizationCMS.Type).CreateService(cmsAuthCredential, serviceCreateModel); if (!cmsServiceCreate.Success) { await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsServiceCreate.GetReasonForNoSuccess()}"); return; } newService.UpdateExternalInformation(cmsServiceCreate.ServiceExternalId, cmsServiceCreate.ServiceExternalUrl, resource.Name); newService.AddEnvironmentsAndVariables(projectServiceTemplate.Parameters); _userRepository.Update(user); await _userRepository.SaveChanges(); await _domainManagerService.AddResult("ServiceId", newService.ProjectServiceId); if (project.OrganizationCPS == null) { project.OrganizationCPS = new OrganizationCPS { Type = CloudProviderService.None } } ; var @event = new ProjectServiceCreatedEvent(_correlationId) { OrganizationId = organization.OrganizationId, OrganizationName = organization.Name, ProjectId = project.ProjectId, ServiceId = newService.ProjectServiceId, ProjectExternalId = project.ProjectExternalId, ProjectExternalEndpointId = project.ProjectExternalEndpointId, ProjectExternalGitEndpoint = project.ProjectExternalGitEndpoint, ProjectVSTSFakeId = project.ProjectVSTSFakeId, ProjectVSTSFakeName = project.ProjectVSTSFakeName, ProjectName = project.Name, InternalProjectName = project.InternalName, AgentPoolId = newService.AgentPoolId, ServiceExternalId = newService.ProjectServiceExternalId, ServiceExternalUrl = newService.ProjectServiceExternalUrl, ServiceName = resource.Name, InternalServiceName = newService.InternalName, ServiceTemplateUrl = projectServiceTemplate.Url, CMSType = project.OrganizationCMS.Type, CMSAccountId = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountId), CMSAccountName = _dataProtectorService.Unprotect(project.OrganizationCMS.AccountName), CMSAccessId = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessId), CMSAccessSecret = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessSecret), CMSAccessToken = _dataProtectorService.Unprotect(project.OrganizationCMS.AccessToken), UserId = loggedUserId, TemplateParameters = projectServiceTemplate.Parameters.Select(x => new ProjectServiceTemplateParameterCreatedEvent() { VariableName = x.VariableName, Value = x.Value, Scope = x.Scope }).ToList(), CPSType = project.OrganizationCPS.Type, CPSAccessId = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId), CPSAccessName = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName), CPSAccessSecret = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret), CPSAccessRegion = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion), TemplateAccess = projectServiceTemplate.TemplateAccess, NeedCredentials = projectServiceTemplate.NeedCredentials, RepositoryCMSType = projectServiceTemplate.TemplateAccess == DomainModels.Enums.TemplateAccess.Organization ? projectServiceTemplate.Credential.CMSType : ConfigurationManagementService.VSTS, RepositoryAccessId = projectServiceTemplate.TemplateAccess == DomainModels.Enums.TemplateAccess.Organization ? projectServiceTemplate.NeedCredentials ? _dataProtectorService.Unprotect(projectServiceTemplate.Credential.AccessId) : string.Empty : string.Empty, RepositoryAccessSecret = projectServiceTemplate.TemplateAccess == DomainModels.Enums.TemplateAccess.Organization ? projectServiceTemplate.NeedCredentials ? _dataProtectorService.Unprotect(projectServiceTemplate.Credential.AccessSecret) : string.Empty : string.Empty, RepositoryAccessToken = projectServiceTemplate.TemplateAccess == DomainModels.Enums.TemplateAccess.Organization ? projectServiceTemplate.NeedCredentials ? _dataProtectorService.Unprotect(projectServiceTemplate.Credential.AccessToken) : string.Empty : string.Empty }; //Cloud Provider Data await _eventBusService.Publish(queueName : "ProjectServiceCreatedEvent", @event : @event); }