public async Task <IActionResult> UpdateCloudProviderService(Guid organizationId, Guid organizationCPSId, [FromBody] OrganizationCPSPutRp organizationCPSRp) { if (!ModelState.IsValid) { return(this.BadRequest(ModelState)); } if (organizationCPSRp.Type == Domain.Models.CloudProviderService.AWS && string.IsNullOrEmpty(organizationCPSRp.AccessSecret)) { ModelState.AddModelError("", "Access Secret is required"); return(this.BadRequest(ModelState)); } if (organizationCPSRp.Type == Domain.Models.CloudProviderService.Azure) { if (string.IsNullOrEmpty(organizationCPSRp.AccessName)) { ModelState.AddModelError("", "Subscription Name is required"); } if (string.IsNullOrEmpty(organizationCPSRp.AccessAppId)) { ModelState.AddModelError("", "Application Id is required"); } if (string.IsNullOrEmpty(organizationCPSRp.AccessAppSecret)) { ModelState.AddModelError("", "Application Secret is required"); } if (string.IsNullOrEmpty(organizationCPSRp.AccessDirectory)) { ModelState.AddModelError("", "Directory is required"); } if (ModelState.ErrorCount > 0) { return(this.BadRequest(ModelState)); } } await _organizationCPSService.UpdateCloudProviderService(organizationId, organizationCPSId, organizationCPSRp); 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()); }
public async Task UpdateCloudProviderService(Guid organizationId, Guid organizationCPSId, OrganizationCPSPutRp 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 update settings in this organization."); return; } OrganizationCPS organizationCPS = organization.GetCloudProviderServiceById(organizationCPSId); if (organizationCPS == null) { await _domainManagerService.AddConflict($"The cloud provider service with id {organizationCPSId} does not exists."); return; } bool validCredentials = await _cpsCredentialService(resource.Type).ValidateCredentials(resource.AccessId, resource.AccessName, resource.AccessSecret, resource.AccessAppId, resource.AccessAppSecret, resource.AccessDirectory, resource.AccessRegion); if (!validCredentials) { await _domainManagerService.AddConflict($"The credentials are not valid or there are some permissions problems"); return; } user.UpdateCloudProviderService(organizationId, organizationCPSId, _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(); }