Example #1
0
        public async Task <ProjectEnvironmentVariableListRp> GetProjectEnvironmentVariables(Guid organizationId, Guid projectId, Guid environmentId)
        {
            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.ProjectEnvironment environment = project.GetEnvironmentById(environmentId);
            if (environment == null)
            {
                return(null);
            }

            DomainModels.ProjectEnvironment  defaultEnvironment = project.GetRootEnvironment();
            ProjectEnvironmentVariableListRp list = new ProjectEnvironmentVariableListRp();

            if (environment.Variables != null)
            {
                var variables = from dv in defaultEnvironment.Variables
                                join ev in environment.Variables on dv.Name equals ev.Name into ev
                                from p in ev.DefaultIfEmpty()
                                select new ProjectEnvironmentVariableListItemRp
                {
                    Name  = dv.Name,
                    Value = p == null ? "" : p.Value
                };

                list.Items = variables.ToList();
            }

            return(list);
        }
Example #2
0
        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);
            }
        }