Пример #1
0
        public async Task <ProjectEnvironmentListRp> GetProjectEnvironments(Guid organizationId, Guid projectId)
        {
            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);
            }

            ProjectEnvironmentListRp list = new ProjectEnvironmentListRp();

            if (project.Environments != null)
            {
                list.Items = project.Environments.Select(x => new ProjectEnvironmentListItemRp()
                {
                    ProjectEnvironmentId = x.ProjectEnvironmentId,
                    Name        = x.Name,
                    Description = x.Description,
                    Type        = x.Type,
                    Status      = x.Status,
                    Rank        = x.Rank
                }).OrderBy(x => x.Rank).ToList();


                var services = project.Services.Select(x => new
                {
                    ProjectServiceId = x.ProjectServiceId,
                    Name             = 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);

                if (services != null && services.Any())
                {
                    foreach (var env in list.Items)
                    {
                        var projectServiceEnvironments    = new ProjectServiceEnvironmentListRp();
                        var projectServiceEnvironmentList = new ConcurrentBag <ProjectServiceEnvironmentListItemRp>();

                        await services.ForEachAsync(5, async service =>
                        {
                            var projectServiceEnvironmentListItem     = new ProjectServiceEnvironmentListItemRp();
                            projectServiceEnvironmentListItem.Name    = service.Name;
                            projectServiceEnvironmentListItem.Summary = await _cpsQueryService(project.OrganizationCPS.Type).GetEnvironmentSummary(organization.Name, project.Name, service.Name, env.Name, "Root", authCredentials);
                            projectServiceEnvironmentList.Add(projectServiceEnvironmentListItem);
                        });

                        projectServiceEnvironments.Items = projectServiceEnvironmentList.ToList();

                        env.Services = projectServiceEnvironments;
                    }
                }
            }

            return(list);
        }
        public async Task <ProjectServiceEnvironmentListRp> GetProjectServiceEnvironments(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 projectService = project.GetServiceById(serviceId);
            if (projectService == null)
            {
                await _domainManagerService.AddNotFound($"The project service with id {serviceId} does not exists.");

                return(null);
            }

            ProjectServiceEnvironmentListRp list = new ProjectServiceEnvironmentListRp();

            if (project.OrganizationCPS.Type == DomainModels.CloudProviderService.None)
            {
                return(list);
            }

            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);

            if (projectService.Environments != null)
            {
                List <ProjectServiceEnvironmentListItemRp> projectServiceEnvironmentList = new List <ProjectServiceEnvironmentListItemRp>();
                var environments = projectService.Environments.OrderBy(x => x.ProjectEnvironment.Rank);
                foreach (var item in environments)
                {
                    var projectServiceEnvironmentListItem = new ProjectServiceEnvironmentListItemRp();
                    projectServiceEnvironmentListItem.ProjectServiceEnvironmentId = item.ProjectServiceEnvironmentId;
                    projectServiceEnvironmentListItem.Name    = item.ProjectEnvironment.Name;
                    projectServiceEnvironmentListItem.Status  = item.Status;
                    projectServiceEnvironmentListItem.Summary = await _cpsQueryService(project.OrganizationCPS.Type).GetEnvironmentSummary(organization.Name, project.Name, projectService.Name, item.ProjectEnvironment.Name, "Root", authCredentials);

                    projectServiceEnvironmentListItem.Variables = item.Variables.Select(p => new ProjectServiceEnvironmentVariableListItemRp()
                    {
                        Name  = p.Name,
                        Value = p.Value
                    }).ToList();
                    projectServiceEnvironmentList.Add(projectServiceEnvironmentListItem);
                }
                list.Items = projectServiceEnvironmentList;
            }

            return(list);
        }