public async Task <CPSCloudResourceSummaryModel> GetProjectServiceCloudSummary(Guid organizationId, Guid projectId, Guid serviceId) { string loggedUserId = _identityService.GetUserId(); DomainModels.User user = await _userRepository.GetUser(loggedUserId); DomainModels.Organization organization = user.FindOrganizationById(organizationId); if (organization == null) { await _domainManagerService.AddNotFound($"The organzation with id ${organizationId} does not exists."); return(null); } DomainModels.Project project = user.FindProjectById(projectId); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return(null); } DomainModels.ProjectService service = project.GetServiceById(serviceId); if (service == null) { await _domainManagerService.AddNotFound($"The project pipe with id {serviceId} does not exists."); return(null); } var environments = project.GetEnvironments().Select(x => x.Name).ToList(); CPSAuthCredentialModel authCredentials = new CPSAuthCredentialModel(); authCredentials.AccessId = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId); authCredentials.AccessName = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName); authCredentials.AccessSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret); authCredentials.AccessRegion = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion); authCredentials.AccessAppId = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId); authCredentials.AccessAppSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret); authCredentials.AccessDirectory = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory); var summary = await _cpsQueryService(project.OrganizationCPS.Type).GetSummary(organization.Name, project.Name, service.Name, environments, "Root", authCredentials); return(summary); }
public async Task SortProjectEnvironments(Guid organizationId, Guid projectId, ProjectEnvironmentSortPostRp resource) { string loggedUserId = _identityService.GetUserId(); DomainModels.User user = await _userRepository.GetUser(loggedUserId); 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); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return; } DomainModels.PipelineRole role = user.GetRoleInProject(projectId); if (role != DomainModels.PipelineRole.ProjectAdmin) { await _domainManagerService.AddForbidden($"You are not authorized to sort environments in this project."); return; } if (project.Status != DomainModels.EntityStatus.Active) { await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to sort de environments."); return; } var developmentEnvironment = project.GetDevelopmentEnvironment(); var productionEnvironment = project.GetProductionEnvironment(); foreach (var item in resource.Items) { var environment = project.GetEnvironmentById(item.EnvironmentId); if (environment == null) { await _domainManagerService.AddNotFound($"The environment with id {item.EnvironmentId}) does not exists."); return; } if (environment.Type == EnvironmentType.Root || environment.Type == EnvironmentType.Fact) { await _domainManagerService.AddConflict($"The environment {environment.Name} ({environment.ProjectEnvironmentId}) is not sortable."); return; } if (!(developmentEnvironment.Rank < item.Rank && item.Rank < productionEnvironment.Rank)) { await _domainManagerService.AddConflict($"The rank of the environment {environment.Name} ({environment.ProjectEnvironmentId}) must be between {developmentEnvironment.Rank + 1} and {productionEnvironment.Rank - 1}."); return; } environment.Rank = item.Rank; } var groupped = project.Environments.GroupBy(x => x.Rank); if (groupped.Count() != project.Environments.Count) { await _domainManagerService.AddConflict($"The rank of the environments must be sorted sequentially between {developmentEnvironment.Rank + 1} and {productionEnvironment.Rank - 1}."); return; } _userRepository.Update(user); await _userRepository.SaveChanges(); var projectServices = project.GetServicesWithReleaseStages(); //send events foreach (var projectService in projectServices) { var @event = new ProjectEnvironmentCreatedEvent(_correlationId) { OrganizationId = organization.OrganizationId, OrganizationName = organization.Name, ProjectId = project.ProjectId, ProjectExternalId = project.ProjectExternalId, ProjectExternalEndpointId = project.ProjectExternalEndpointId, ProjectVSTSFakeName = project.ProjectVSTSFakeName, ProjectName = project.Name, 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), ReleseStageId = projectService.ReleaseStageId.Value, ServiceName = projectService.Name, ServiceLastBuildSuccessVersionId = projectService.LastBuildSuccessVersionId, ServiceLastBuildSuccessVersionName = projectService.LastBuildSuccessVersionName }; @event.Environments = new List <ProjectEnvironmentItemCreatedEvent>(); foreach (var item in projectService.Environments) { var parentEnvironment = project.GetEnvironments().First(x => x.ProjectEnvironmentId == item.ProjectEnvironmentId); var serviceEnvironment = new ProjectEnvironmentItemCreatedEvent(); serviceEnvironment.Id = item.ProjectEnvironmentId; serviceEnvironment.Name = parentEnvironment.Name; serviceEnvironment.RequiredApproval = parentEnvironment.RequiresApproval; serviceEnvironment.Variables = new List <ProjectEnvironmentItemVariableCreatedEvent>(); serviceEnvironment.Rank = parentEnvironment.Rank; serviceEnvironment.LastSuccessVersionId = item.LastSuccessVersionId; serviceEnvironment.LastSuccessVersionName = item.LastSuccessVersionName; if (parentEnvironment.Variables != null) { foreach (var variable in parentEnvironment.Variables) { serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent() { Name = variable.Name, Value = variable.Value }); } } if (item.Variables != null) { foreach (var variable in item.Variables) { serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent() { Name = variable.Name, Value = variable.Value }); } } @event.Environments.Add(serviceEnvironment); } //Cloud Provider Data @event.CPSType = project.OrganizationCPS.Type; @event.CPSAccessId = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId); @event.CPSAccessName = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName); @event.CPSAccessSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret); @event.CPSAccessRegion = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion); await _eventBusService.Publish(queueName : "ProjectEnvironmentCreatedEvent", @event : @event); } }
public async Task CreateProjectEnvironmentVariables(Guid organizationId, Guid projectId, Guid environmentId, ProjectEnvironmentVariablePostRp resource) { string loggedUserId = _identityService.GetUserId(); DomainModels.User user = await _userRepository.GetUser(loggedUserId); 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); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return; } DomainModels.PipelineRole role = user.GetRoleInProject(projectId); if (role != DomainModels.PipelineRole.ProjectAdmin) { await _domainManagerService.AddForbidden($"You are not authorized to create environments variables in this project."); return; } if (project.Status != DomainModels.EntityStatus.Active) { await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to add a new feature."); return; } DomainModels.ProjectEnvironment environment = project.GetEnvironmentById(environmentId); if (environment == null) { await _domainManagerService.AddNotFound($"The environment with id {environmentId} does not exists."); return; } bool autoProvision = false; if (environment.Type == DomainModels.EnvironmentType.Root) { foreach (var resourceVariable in resource.Items) { if (string.IsNullOrEmpty(resourceVariable.Name) || string.IsNullOrEmpty(resourceVariable.Value)) { await _domainManagerService.AddConflict($"The environment variable name/value is mandatory."); return; } var variable = environment.GetVariableByName(resourceVariable.Name); if (variable != null) { environment.SetVariable(resourceVariable.Name, resourceVariable.Value); } else { environment.AddVariable(resourceVariable.Name, resourceVariable.Value); } } } else { DomainModels.ProjectEnvironment rootEnvironment = project.GetRootEnvironment(); foreach (var variable in rootEnvironment.Variables) { var resourceVariable = resource.Items.FirstOrDefault(x => x.Name.Equals(variable.Name, StringComparison.InvariantCultureIgnoreCase)); if (resourceVariable == null) { await _domainManagerService.AddConflict($"The environment variable {variable.Name} is mandatory."); return; } if (string.IsNullOrEmpty(resourceVariable.Value)) { await _domainManagerService.AddConflict($"The environment variable value {variable.Name} is mandatory."); return; } var existingVariable = environment.GetVariableByName(resourceVariable.Name); if (existingVariable != null) { environment.SetVariable(resourceVariable.Name, resourceVariable.Value); } else { environment.AddVariable(resourceVariable.Name, resourceVariable.Value); } } if (environment.Status == DomainModels.EntityStatus.Preparing) { autoProvision = environment.AutoProvision; } environment.Activate(); } var projectServices = project.GetServicesWithReleaseStages(); //replicate service environments foreach (var projectService in projectServices) { var rootVariables = projectService.GetRootEnvironmentVariables(); projectService.AddEnvironment(environment.ProjectEnvironmentId, rootVariables); } _userRepository.Update(user); await _userRepository.SaveChanges(); //send events foreach (var projectService in projectServices) { var @event = new ProjectEnvironmentCreatedEvent(_correlationId) { OrganizationId = organization.OrganizationId, OrganizationName = organization.Name, ProjectId = project.ProjectId, ProjectExternalId = project.ProjectExternalId, ProjectExternalEndpointId = project.ProjectExternalEndpointId, ProjectVSTSFakeName = project.ProjectVSTSFakeName, ProjectName = project.Name, 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), EnvironmentId = environment.ProjectEnvironmentId, EnvironmentName = environment.Name, EnvironmentRank = environment.Rank, EnvironmentAutoProvision = autoProvision, ReleseStageId = projectService.ReleaseStageId.Value, ServiceName = projectService.Name, ServiceLastBuildSuccessVersionId = projectService.LastBuildSuccessVersionId, ServiceLastBuildSuccessVersionName = projectService.LastBuildSuccessVersionName }; @event.Environments = new List <ProjectEnvironmentItemCreatedEvent>(); foreach (var item in projectService.Environments) { var parentEnvironment = project.GetEnvironments().First(x => x.ProjectEnvironmentId == item.ProjectEnvironmentId); var serviceEnvironment = new ProjectEnvironmentItemCreatedEvent(); serviceEnvironment.Id = item.ProjectEnvironmentId; serviceEnvironment.Name = parentEnvironment.Name; serviceEnvironment.RequiredApproval = parentEnvironment.RequiresApproval; serviceEnvironment.Variables = new List <ProjectEnvironmentItemVariableCreatedEvent>(); serviceEnvironment.Rank = parentEnvironment.Rank; serviceEnvironment.LastSuccessVersionId = item.LastSuccessVersionId; serviceEnvironment.LastSuccessVersionName = item.LastSuccessVersionName; if (parentEnvironment.Variables != null) { foreach (var variable in parentEnvironment.Variables) { serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent() { Name = variable.Name, Value = variable.Value }); } } if (item.Variables != null) { foreach (var variable in item.Variables) { serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent() { Name = variable.Name, Value = variable.Value }); } } @event.Environments.Add(serviceEnvironment); } //Cloud Provider Data @event.CPSType = project.OrganizationCPS.Type; @event.CPSAccessId = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId); @event.CPSAccessName = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName); @event.CPSAccessSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret); @event.CPSAccessRegion = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion); await _eventBusService.Publish(queueName : "ProjectEnvironmentCreatedEvent", @event : @event); } }
public async Task DeleteProjectService(Guid organizationId, Guid projectId, Guid serviceId) { string loggedUserId = _identityService.GetUserId(); User user = await _userRepository.GetUser(loggedUserId); DomainModels.Organization organization = user.FindOrganizationById(organizationId); if (organization == null) { await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists."); return; } DomainModels.Project project = organization.GetProjectById(projectId); 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 modify a service."); return; } DomainModels.ProjectService service = project.GetServiceById(serviceId); if (service == null) { await _domainManagerService.AddNotFound($"The project pipe with id {serviceId} does not exists."); return; } if (service.Status != EntityStatus.Active) { await _domainManagerService.AddConflict($"The pipe with id {serviceId} must be in status Active to be modified/deleted."); return; } /*Check If any feature is associated with the service*/ var features = project.GetFeatures(); foreach (var feature in features) { var featureService = feature.GetFeatureServiceById(serviceId); if (featureService != null) { await _domainManagerService.AddConflict($"The are active features ({feature.Name}) using the service, you cannot delete the service."); return; } } user.DeleteProjectService(organizationId, projectId, serviceId); _userRepository.Update(user); await _userRepository.SaveChanges(); if (project.OrganizationCPS == null) { project.OrganizationCPS = new OrganizationCPS { Type = CloudProviderService.None } } ; //send event var projectServiceDeletedEvent = new ProjectServiceDeletedEvent(_correlationId) { OrganizationExternalId = project.OrganizationExternalId, OrganizationName = organization.Name, ProjectName = project.Name, ServiceName = service.Name, ProjectVSTSFakeName = project.ProjectVSTSFakeName, ProjectExternalId = project.ProjectExternalId, ProjectServiceExternalId = service.ProjectServiceExternalId, CommitStageId = service.CommitStageId, ReleaseStageId = service.ReleaseStageId, CommitServiceHookId = service.CommitServiceHookId, CodeServiceHookId = service.CodeServiceHookId, ReleaseStartedServiceHookId = service.ReleaseStartedServiceHookId, ReleasePendingApprovalServiceHookId = service.ReleasePendingApprovalServiceHookId, ReleaseCompletedApprovalServiceHookId = service.ReleaseCompletedApprovalServiceHookId, ReleaseServiceHookId = service.ReleaseServiceHookId, Environments = project.GetEnvironments().Select(x => x.Name).ToList(), 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), 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), CPSAccessAppId = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppId), CPSAccessAppSecret = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessAppSecret), CPSAccessDirectory = project.OrganizationCPS.Type == CloudProviderService.None ? string.Empty : _dataProtectorService.Unprotect(project.OrganizationCPS.AccessDirectory), SourceEvent = DomainModels.Enums.SourceEvent.Service }; await _eventBusService.Publish(queueName : "ProjectServiceDeletedEvent", @event : projectServiceDeletedEvent); }
public async Task CreateProjectServiceEnvironmentVariables(Guid organizationId, Guid projectId, Guid serviceId, Guid environmentId, ProjectServiceEnvironmentVariablePostRp resource) { string loggedUserId = _identityService.GetUserId(); DomainModels.User user = await _userRepository.GetUser(loggedUserId); 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); if (project == null) { await _domainManagerService.AddNotFound($"The project with id {projectId} does not exists."); return; } DomainModels.PipelineRole role = user.GetRoleInProject(projectId); if (role != DomainModels.PipelineRole.ProjectAdmin) { await _domainManagerService.AddForbidden($"You are not authorized to create environments variables in this project."); return; } if (project.Status != DomainModels.EntityStatus.Active) { await _domainManagerService.AddConflict($"The project with id {projectId} must be in status Active to add a new feature."); return; } DomainModels.ProjectService projectService = project.GetServiceById(serviceId); if (projectService == null) { await _domainManagerService.AddNotFound($"The project service with id {serviceId} does not exists."); return; } if (projectService.Status != DomainModels.EntityStatus.Active) { await _domainManagerService.AddConflict($"The pipe with id {serviceId} must be in status Active to add/modify variables."); return; } DomainModels.ProjectServiceEnvironment environment = projectService.GetServiceEnvironment(environmentId); if (environment == null) { await _domainManagerService.AddNotFound($"The environment with id {environmentId} does not exists."); return; } foreach (var resourceVariable in resource.Items) { if (string.IsNullOrEmpty(resourceVariable.Name) || string.IsNullOrEmpty(resourceVariable.Value)) { await _domainManagerService.AddConflict($"The environment variable name/value is mandatory."); return; } var variable = environment.GetVariableByName(resourceVariable.Name); if (variable != null) { environment.SetVariable(resourceVariable.Name, resourceVariable.Value); } else { environment.AddVariable(resourceVariable.Name, resourceVariable.Value); } } _userRepository.Update(user); await _userRepository.SaveChanges(); var @event = new ProjectEnvironmentCreatedEvent(_correlationId) { OrganizationId = organization.OrganizationId, OrganizationName = organization.Name, ProjectId = project.ProjectId, ProjectExternalId = project.ProjectExternalId, ProjectExternalEndpointId = project.ProjectExternalEndpointId, ProjectVSTSFakeName = project.ProjectVSTSFakeName, ProjectName = project.Name, 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), ReleseStageId = projectService.ReleaseStageId.Value }; @event.Environments = new List <ProjectEnvironmentItemCreatedEvent>(); foreach (var item in projectService.Environments) { var parentEnvironment = project.GetEnvironments().First(x => x.ProjectEnvironmentId == item.ProjectEnvironmentId); var serviceEnvironment = new ProjectEnvironmentItemCreatedEvent(); serviceEnvironment.Name = parentEnvironment.Name; serviceEnvironment.RequiredApproval = parentEnvironment.RequiresApproval; serviceEnvironment.Rank = parentEnvironment.Rank; serviceEnvironment.Variables = new List <ProjectEnvironmentItemVariableCreatedEvent>(); foreach (var variable in parentEnvironment.Variables) { serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent() { Name = variable.Name, Value = variable.Value }); } foreach (var variable in item.Variables) { serviceEnvironment.Variables.Add(new ProjectEnvironmentItemVariableCreatedEvent() { Name = variable.Name, Value = variable.Value }); } @event.Environments.Add(serviceEnvironment); } //Cloud Provider Data @event.CPSType = project.OrganizationCPS.Type; @event.CPSAccessId = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessId); @event.CPSAccessName = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessName); @event.CPSAccessSecret = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessSecret); @event.CPSAccessRegion = _dataProtectorService.Unprotect(project.OrganizationCPS.AccessRegion); await _eventBusService.Publish(queueName : "ProjectEnvironmentCreatedEvent", @event : @event); }