Exemple #1
0
        public async Task <IActionResult> Post([FromBody] Provider provider)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            var validation = new ProviderValidator().Validate(provider);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ActionResult());
            }

            var teamCloudInstance = await teamCloudRepository
                                    .GetAsync()
                                    .ConfigureAwait(false);

            if (teamCloudInstance is null)
            {
                return(ErrorResult
                       .NotFound($"No TeamCloud Instance was found.")
                       .ActionResult());
            }

            if (teamCloudInstance.Providers.Contains(provider))
            {
                return(ErrorResult
                       .Conflict($"A Provider with the ID '{provider.Id}' already exists on this TeamCloud Instance. Please try your request again with a unique ID or call PUT to update the existing Provider.")
                       .ActionResult());
            }

            var command = new OrchestratorProviderCreateCommand(CurrentUser, provider);

            var commandResult = await orchestrator
                                .InvokeAsync(command)
                                .ConfigureAwait(false);

            if (commandResult.Links.TryGetValue("status", out var statusUrl))
            {
                return(StatusResult
                       .Accepted(commandResult.CommandId.ToString(), statusUrl, commandResult.RuntimeStatus.ToString(), commandResult.CustomStatus)
                       .ActionResult());
            }

            throw new Exception("This shoudn't happen, but we need to decide to do when it does.");
        }
Exemple #2
0
        public async Task <IActionResult> Post([FromBody] Provider provider)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            var validation = new ProviderValidator().Validate(provider);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ActionResult());
            }

            var existingProvider = await providersRepository
                                   .GetAsync(provider.Id)
                                   .ConfigureAwait(false);

            if (existingProvider != null)
            {
                return(ErrorResult
                       .Conflict($"A Provider with the ID '{provider.Id}' already exists on this TeamCloud Instance. Please try your request again with a unique ID or call PUT to update the existing Provider.")
                       .ActionResult());
            }

            var currentUserForCommand = await userService
                                        .CurrentUserAsync()
                                        .ConfigureAwait(false);

            var commandProvider = new TeamCloud.Model.Internal.Data.Provider();

            commandProvider.PopulateFromExternalModel(provider);

            var command = new OrchestratorProviderCreateCommand(currentUserForCommand, commandProvider);

            return(await orchestrator
                   .InvokeAndReturnAccepted(command)
                   .ConfigureAwait(false));
        }
        public async Task <IActionResult> Post([FromBody] Provider provider)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            var validation = new ProviderValidator().Validate(provider);

            if (!validation.IsValid)
            {
                return(ErrorResult
                       .BadRequest(validation)
                       .ToActionResult());
            }

            var providerDocument = await ProviderRepository
                                   .GetAsync(provider.Id)
                                   .ConfigureAwait(false);

            if (providerDocument != null)
            {
                return(ErrorResult
                       .Conflict($"A Provider with the ID '{provider.Id}' already exists on this TeamCloud Instance. Please try your request again with a unique ID or call PUT to update the existing Provider.")
                       .ToActionResult());
            }

            if (provider.Type == ProviderType.Virtual)
            {
                var serviceProviders = await ProviderRepository
                                       .ListAsync(providerType : ProviderType.Service)
                                       .ToListAsync()
                                       .ConfigureAwait(false);

                var serviceProvider = serviceProviders
                                      .FirstOrDefault(p => provider.Id.StartsWith($"{p.Id}.", StringComparison.Ordinal));

                if (serviceProvider is null)
                {
                    var validServiceProviderIds = string.Join(", ", serviceProviders.Select(p => p.Id));

                    return(ErrorResult
                           .BadRequest(new ValidationError {
                        Field = "id", Message = $"No matching service provider found. Virtual provider ids must begin with the associated Service provider id followed by a period (.). Available service providers: {validServiceProviderIds}"
                    })
                           .ToActionResult());
                }

                var urlPrefix = $"{serviceProvider.Url}?";

                if (!provider.Url.StartsWith(urlPrefix, StringComparison.OrdinalIgnoreCase))
                {
                    return(ErrorResult
                           .BadRequest(new ValidationError {
                        Field = "url", Message = $"Virtual provider url must match the associated service provider url followed by a query string. The url should begin with {urlPrefix}"
                    })
                           .ToActionResult());
                }
            }

            var currentUser = await UserService
                              .CurrentUserAsync()
                              .ConfigureAwait(false);

            providerDocument = new ProviderDocument()
                               .PopulateFromExternalModel(provider);

            var command = new OrchestratorProviderCreateCommand(currentUser, providerDocument);

            return(await Orchestrator
                   .InvokeAndReturnActionResultAsync <ProviderDocument, Provider>(command, Request)
                   .ConfigureAwait(false));
        }