public async Task CreateConfigurationManagementService(Guid organizationId, OrganizationCMSPostRp 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 settings in this organization."); return; } //OrganizationCMS organizationCMS = organization.GetConfigurationManagementServiceByType(resource.Type); //if (organizationCMS != null && organizationCMS.ConnectionType == resource.ConnectionType) //{ // await _domainManagerService.AddConflict($"The configuration management service with type {resource.Type} already exists."); // return; //} OrganizationCMS existingCMP = organization.GetConfigurationManagementServiceByName(resource.Name); if (existingCMP != null) { await _domainManagerService.AddConflict($"The configuration management service {resource.Name} has already been taken."); return; } //existing same connection in other account OrganizationCMS existingInOtherOrganization = await _organizationCMSRepository.FindOrganizationCMSByTypeAndAccountName(resource.Type, resource.AccountName); if (existingInOtherOrganization != null) { await _domainManagerService.AddConflict($"The configuration management service {resource.Type}/{resource.AccountName} has already been taken in other organization."); return; } user.AddConfigurationManagementService(organizationId, resource.Name, resource.Type, resource.ConnectionType, _dataProtectorService.Protect(resource.AccountId), _dataProtectorService.Protect(resource.AccountName), _dataProtectorService.Protect(resource.AccessId), _dataProtectorService.Protect(resource.AccessSecret), _dataProtectorService.Protect(resource.AccessToken)); _userRepository.Update(user); await _userRepository.SaveChanges(); }
public async Task CreateCloudProviderService(Guid organizationId, OrganizationCPSPostRp 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 settings in this organization."); return; } //OrganizationCPS organizationCPS = organization.GetCloudProviderServiceByType(resource.Type); //if (organizationCPS != null) //{ // await _domainManagerService.AddConflict($"The cloud provider service with type {resource.Type} already exists."); // return; //} OrganizationCPS existingCSP = organization.GetCloudProviderServiceByName(resource.Name); if (existingCSP != null) { await _domainManagerService.AddConflict($"The cloud provider service {resource.Name} has already been taken."); return; } bool validCredentials = await _cpsCredentialService(resource.Type).ValidateCredentials(resource.AccessId, resource.AccessName, resource.AccessSecret, resource.AccessAppId, resource.AccessAppSecret, resource.AccessDirectory, resource.AccessRegion); if (!validCredentials) { if (resource.Type == CloudProviderService.AWS) { await _domainManagerService.AddConflict($"The credentials are not valid"); } else { await _domainManagerService.AddConflict($"The credentials are not valid or the client does not have enough privileges"); } return; } user.AddCloudProviderService(organizationId, resource.Name, resource.Type, _dataProtectorService.Protect(resource.AccessId), _dataProtectorService.Protect(resource.AccessName), _dataProtectorService.Protect(resource.AccessSecret), _dataProtectorService.Protect(resource.AccessAppId), _dataProtectorService.Protect(resource.AccessAppSecret), _dataProtectorService.Protect(resource.AccessDirectory), _dataProtectorService.Protect(resource.AccessRegion)); _userRepository.Update(user); await _userRepository.SaveChanges(); }