Пример #1
0
        public async Task <IActionResult> CreateProject(Guid organizationId, [FromBody] ProjectPostRp projectRp)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(ModelState));
            }

            await _projectService.CreateProject(organizationId, projectRp);

            if (_domainManagerService.HasNotFounds())
            {
                return(this.NotFound(_domainManagerService.GetNotFounds()));
            }

            if (_domainManagerService.HasForbidden())
            {
                return(this.Forbidden(_domainManagerService.GetForbidden()));
            }

            if (_domainManagerService.HasConflicts())
            {
                return(this.Conflict(_domainManagerService.GetConflicts()));
            }

            return(this.Ok(new { ProjectId = await _domainManagerService.GetResult <Guid>("ProjectId") }));
        }
        public async Task CreateProject(Guid organizationId, ProjectPostRp resource)
        {
            string loggedUserId = _identityService.GetUserId();

            User user = await _userRepository.GetUser(loggedUserId);

            Organization organization = user.FindOrganizationById(organizationId);

            if (organization == null)
            {
                await _domainManagerService.AddNotFound($"The organzation with id {organizationId} does not exists.");

                return;
            }

            OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceById(resource.OrganizationCMSId);

            if (organizationCMS == null)
            {
                await _domainManagerService.AddNotFound($"The configuration management service with id {resource.OrganizationCMSId} does not exists.");

                return;
            }

            if (organizationCMS.Type == ConfigurationManagementService.VSTS && resource.projectVisibility == ProjectVisibility.None)
            {
                await _domainManagerService.AddConflict($"The project visibility should be Private or Public.");

                return;
            }

            OrganizationCPS organizationCPS = null;

            if (resource.OrganizationCPSId.HasValue)
            {
                organizationCPS = organization.GetCloudProviderServiceById(resource.OrganizationCPSId.Value);

                if (organizationCPS == null)
                {
                    await _domainManagerService.AddNotFound($"The cloud provider service with id {resource.OrganizationCPSId} does not exists.");

                    return;
                }
            }
            else
            {
                organizationCPS = new OrganizationCPS {
                    Type = CloudProviderService.None
                };
            }

            ProjectTemplate projectTemplate = null;

            if (resource.ProjectTemplateId.HasValue)
            {
                projectTemplate = await _projectTemplateRepository.GetProjectTemplateById(resource.ProjectTemplateId.Value);

                if (projectTemplate == null)
                {
                    await _domainManagerService.AddNotFound($"The project template with id {resource.ProjectTemplateId.Value} does not exists.");

                    return;
                }
            }

            Project existingProject = organization.GetProjectByName(resource.Name);

            if (existingProject != null)
            {
                await _domainManagerService.AddConflict($"The project name {resource.Name} has already been taken.");

                return;
            }

            //Auth
            CMSAuthCredentialModel cmsAuthCredential = this._cmsCredentialService(organizationCMS.Type).GetToken(
                _dataProtectorService.Unprotect(organizationCMS.AccountId),
                _dataProtectorService.Unprotect(organizationCMS.AccountName),
                _dataProtectorService.Unprotect(organizationCMS.AccessSecret),
                _dataProtectorService.Unprotect(organizationCMS.AccessToken));

            CMSProjectAvailabilityResultModel cmsProjectAvailability = await _cmsService(organizationCMS.Type).ValidateProjectAvailability(cmsAuthCredential, resource.TeamId, resource.Name);

            if (!cmsProjectAvailability.Success)
            {
                await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectAvailability.GetReasonForNoSuccess()}");

                return;
            }

            Project newProject = user.CreateProject(organizationId, resource.TeamId, resource.Name, resource.Description, resource.ProjectType, resource.OrganizationCMSId, resource.OrganizationCPSId, resource.ProjectTemplateId, resource.AgentPoolId, resource.projectVisibility, organizationCPS.Type, organizationCMS.Type);

            //SaveChanges in CSM
            CMSProjectCreateModel projectCreateModel = CMSProjectCreateModel.Factory.Create(organization.Name, resource.Name, resource.Description, resource.projectVisibility);

            projectCreateModel.TeamId = resource.TeamId;

            CMSProjectCreateResultModel cmsProjectCreate = await _cmsService(organizationCMS.Type).CreateProject(cmsAuthCredential, projectCreateModel);

            if (!cmsProjectCreate.Success)
            {
                await _domainManagerService.AddConflict($"The CMS data is not valid. {cmsProjectCreate.GetReasonForNoSuccess()}");

                return;
            }

            newProject.UpdateExternalInformation(cmsProjectCreate.ProjectExternalId, resource.Name);

            _userRepository.Update(user);

            await _userRepository.SaveChanges();

            await _domainManagerService.AddResult("ProjectId", newProject.ProjectId);

            //send event
            var @event = new ProjectCreatedEvent(_correlationId)
            {
                OrganizationId      = organization.OrganizationId,
                ProjectId           = newProject.ProjectId,
                ProjectName         = newProject.Name,
                InternalProjectName = newProject.InternalName,
                ProjectVSTSFake     = this._slugService.GetSlug($"{organization.Owner.Email} {organization.Name} {newProject.Name}"),
                AgentPoolId         = newProject.AgentPoolId,

                CMSType         = organizationCMS.Type,
                CMSAccountId    = _dataProtectorService.Unprotect(organizationCMS.AccountId),
                CMSAccountName  = _dataProtectorService.Unprotect(organizationCMS.AccountName),
                CMSAccessId     = _dataProtectorService.Unprotect(organizationCMS.AccessId),
                CMSAccessSecret = _dataProtectorService.Unprotect(organizationCMS.AccessSecret),
                CMSAccessToken  = _dataProtectorService.Unprotect(organizationCMS.AccessToken),

                CPSType            = organizationCPS.Type,
                CPSAccessId        = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessId) : string.Empty,
                CPSAccessName      = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessName) : string.Empty,
                CPSAccessSecret    = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessSecret) : string.Empty,
                CPSAccessAppId     = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessAppId) : string.Empty,
                CPSAccessAppSecret = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessAppSecret) : string.Empty,
                CPSAccessDirectory = organizationCPS.Type != CloudProviderService.None ? _dataProtectorService.Unprotect(organizationCPS.AccessDirectory) : string.Empty,
                UserId             = loggedUserId
            };

            if (resource.ProjectTemplateId.HasValue)
            {
                @event.ProjectTemplate          = new ProjectTemplateCreatedEvent();
                @event.ProjectTemplate.Services = projectTemplate.Services.Select(x => new ProjectTemplateServiceCreatedEvent()
                {
                    Name = x.Name,
                    ProjectServiceTemplateId = x.ProjectServiceTemplateId
                }).ToList();
            }

            await _eventBusService.Publish(queueName : "ProjectCreatedEvent", @event : @event);
        }