Ejemplo n.º 1
0
        public async Task ActivateProjectServiceTemplate(Guid organizationId, Guid projectServiceTemplateId)
        {
            var template = await _projectServiceTemplateRepository.GetPendingProjectServiceTemplateById(projectServiceTemplateId);

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

                return;
            }

            if (template.Status != EntityStatus.Preparing)
            {
                await _domainManagerService.AddConflict($"The template with id {projectServiceTemplateId} must be in status NEW to be activated.");

                return;
            }

            template.Activate();

            _projectServiceTemplateRepository.Update(template);

            await _projectServiceTemplateRepository.SaveChanges();
        }
Ejemplo n.º 2
0
        public async Task CreateOrganizationProjectServiceTemplate(Guid organizationId, OrganizationProjectServiceTemplatePostRp 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;
            }

            PipelineRole role = user.GetRoleInOrganization(organizationId);

            if (role != PipelineRole.OrganizationAdmin)
            {
                await _domainManagerService.AddForbidden($"You are not authorized to create project service templates in this organization.");

                return;
            }

            OrganizationProjectServiceTemplate existingTemplate = organization.GetProjectServiceTemplateByName(resource.Name);

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

                return;
            }

            ProjectServiceTemplate sourceProjectServiceTemplate = await _projectServiceTemplateRepository.GetProjectServiceTemplateInternalById(resource.SourceProjectServiceTemplateId);

            if (existingTemplate != null)
            {
                await _domainManagerService.AddConflict($"The source project service templates {resource.Name} does not exists.");

                return;
            }

            ProgrammingLanguage programmingLanguage = await _programmingLanguageRepository.GetProgrammingLanguageById(resource.ProgrammingLanguageId);

            if (programmingLanguage == null)
            {
                await _domainManagerService.AddNotFound($"The programming language with id {resource.ProgrammingLanguageId} does not exists.");

                return;
            }

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

            if (organizationCMS == null)
            {
                await _domainManagerService.AddNotFound($"The connection with id {resource.ConnectionId} does not exists.");

                return;
            }

            if (organizationCMS.ConnectionType != Domain.Models.Enums.CMSConnectionType.TemplateLevel)
            {
                await _domainManagerService.AddConflict($"The connection with id {resource.ConnectionId} is not for templates.");

                return;
            }

            /*Create repository : BEGIN*/

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

            var teamId    = string.Empty;
            var projectId = string.Empty;

            if (resource.RepositoryCMSType == ConfigurationManagementService.Bitbucket)
            {
                teamId    = resource.TeamId;
                projectId = resource.ProjectExternalId;
            }
            else
            {
                teamId    = resource.ProjectExternalId;
                projectId = resource.ProjectExternalId;
            }

            CMSServiceAvailabilityResultModel cmsServiceAvailability =
                await _cmsService(organizationCMS.Type).ValidateServiceAvailability(cmsAuthCredential, teamId, projectId, resource.ProjectExternalName, resource.Name);

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

                return;
            }

            //SaveChanges in CMS
            CMSServiceCreateModel       serviceCreateModel = CMSServiceCreateModel.Factory.Create(teamId, projectId, resource.ProjectExternalName, resource.Name, true);
            CMSServiceCreateResultModel cmsServiceCreate   = await _cmsService(organizationCMS.Type).CreateService(cmsAuthCredential, serviceCreateModel);

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

                return;
            }

            /*Create repository : END*/
            var template = user.AddProjectTemplateService(organizationId, resource.Name,
                                                          sourceProjectServiceTemplate.ServiceCMSType,
                                                          sourceProjectServiceTemplate.ServiceCPSType,
                                                          resource.Description,
                                                          cmsServiceCreate.ServiceExternalUrl,
                                                          resource.Logo,
                                                          resource.PipeType,
                                                          Domain.Models.Enums.TemplateType.Standard,
                                                          Domain.Models.Enums.TemplateAccess.Organization,
                                                          true, resource.ProgrammingLanguageId, resource.Framework, resource.RepositoryCMSType,
                                                          organizationCMS.AccessId,
                                                          organizationCMS.AccessSecret,
                                                          organizationCMS.AccessToken,
                                                          sourceProjectServiceTemplate.Parameters);

            _projectServiceTemplateRepository.Add(template);
            await _projectServiceTemplateRepository.SaveChanges();

            _userRepository.Update(user);
            await _userRepository.SaveChanges();

            var @event = new ProjectServiceTemplateCreatedEvent(_correlationId)
            {
                OrganizationId           = organization.OrganizationId,
                ProjectServiceTemplateId = template.ProjectServiceTemplateId,
                SourceTemplateUrl        = sourceProjectServiceTemplate.Url,
                TemplateAccess           = template.TemplateAccess,
                NeedCredentials          = template.NeedCredentials,
                RepositoryCMSType        = template.Credential.CMSType,
                RepositoryAccessId       = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessId) : string.Empty,
                RepositoryAccessSecret   = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessSecret) : string.Empty,
                RepositoryAccessToken    = template.NeedCredentials ? _dataProtectorService.Unprotect(template.Credential.AccessToken) : string.Empty,
                RepositoryUrl            = template.Url
            };

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