public async Task <IActionResult> CreateCloudProviderService(Guid organizationId, [FromBody] OrganizationCPSPostRp 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.CreateCloudProviderService(organizationId, organizationCPSRp);

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

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

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

            return(this.Ok());
        }
Exemplo n.º 2
0
        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();
        }