private async Task <(Guid, int)> GetSubscriptionCapacityAsync(ProjectTypeDocument projectType, Guid subscriptionId)
        {
            var subscription = await azureResourceService
                               .GetSubscriptionAsync(subscriptionId)
                               .ConfigureAwait(false);

            if (subscription is null)
            {
                return(subscriptionId, 0);
            }

            var identity = await azureResourceService.AzureSessionService
                           .GetIdentityAsync()
                           .ConfigureAwait(false);

            var hasOwnership = await subscription
                               .HasRoleAssignmentAsync(identity.ObjectId.ToString(), AzureRoleDefinition.Owner)
                               .ConfigureAwait(false);

            if (hasOwnership)
            {
                var instanceCount = await projectTypeRepository
                                    .GetInstanceCountAsync(projectType.Id, subscriptionId)
                                    .ConfigureAwait(false);

                return(subscriptionId, Math.Max(projectType.SubscriptionCapacity - instanceCount, 0));
            }

            return(subscriptionId, 0);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] ProjectType projectType)
        {
            if (projectType is null)
            {
                return(ErrorResult
                       .BadRequest("Request body must not be empty.", ResultErrorCode.ValidationError)
                       .ToActionResult());
            }

            if (!projectType.TryValidate(out var validationResult, serviceProvider: HttpContext.RequestServices))
            {
                return(ErrorResult
                       .BadRequest(validationResult)
                       .ToActionResult());
            }

            var projectTypeDocument = await projectTypeRepository
                                      .GetAsync(projectType.Id)
                                      .ConfigureAwait(false);

            if (projectTypeDocument != null)
            {
                return(ErrorResult
                       .Conflict($"A ProjectType with id '{projectType.Id}' already exists.  Please try your request again with a unique id or call PUT to update the existing ProjectType.")
                       .ToActionResult());
            }

            var providers = await ProviderRepository
                            .ListAsync(includeServiceProviders : false)
                            .ToListAsync()
                            .ConfigureAwait(false);

            var validProviders = projectType.Providers
                                 .All(p => providers.Any(provider => provider.Id == p.Id));

            if (!validProviders)
            {
                var validProviderIds = string.Join(", ", providers.Select(p => p.Id));

                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance and cannot be a Service Provider. Valid provider ids are: {validProviderIds}"
                })
                       .ToActionResult());
            }

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

            projectTypeDocument = new ProjectTypeDocument()
                                  .PopulateFromExternalModel(projectType);

            return(await Orchestrator
                   .InvokeAndReturnActionResultAsync <ProjectTypeDocument, ProjectType>(new OrchestratorProjectTypeCreateCommand(currentUser, projectTypeDocument), Request)
                   .ConfigureAwait(false));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Put(
            [HttpTrigger(AuthorizationLevel.Function, "put", Route = "data/projectTypes")] ProjectTypeDocument projectType)
        {
            if (projectType is null)
            {
                throw new ArgumentNullException(nameof(projectType));
            }

            var newProjectType = await projectTypesRepository
                                 .SetAsync(projectType)
                                 .ConfigureAwait(false);

            return(new OkObjectResult(newProjectType));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Post([FromBody] ProjectType projectType)
        {
            if (projectType is null)
            {
                throw new ArgumentNullException(nameof(projectType));
            }

            var validation = new ProjectTypeValidator().Validate(projectType);

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

            var existingProjectType = await projectTypesRepository
                                      .GetAsync(projectType.Id)
                                      .ConfigureAwait(false);

            if (existingProjectType != null)
            {
                return(ErrorResult
                       .Conflict($"A ProjectType with id '{projectType.Id}' already exists.  Please try your request again with a unique id or call PUT to update the existing ProjectType.")
                       .ActionResult());
            }

            var providers = await providersRepository
                            .ListAsync()
                            .ToListAsync()
                            .ConfigureAwait(false);

            var validProviders = projectType.Providers
                                 .All(p => providers.Any(provider => provider.Id == p.Id));

            if (!validProviders)
            {
                var validProviderIds = string.Join(", ", providers.Select(p => p.Id));
                return(ErrorResult
                       .BadRequest(new ValidationError {
                    Field = "projectType", Message = $"All provider ids on a ProjectType must match the id of a registered Provider on the TeamCloud instance. Valid provider ids are: {validProviderIds}"
                })
                       .ActionResult());
            }

            var newProjectType = new ProjectTypeDocument()
                                 .PopulateFromExternalModel(projectType);

            var addResult = await orchestrator
                            .AddAsync(newProjectType)
                            .ConfigureAwait(false);

            var baseUrl  = HttpContext.GetApplicationBaseUrl();
            var location = new Uri(baseUrl, $"api/projectTypes/{addResult.Id}").ToString();

            var returnAddResult = addResult.PopulateExternalModel();

            return(DataResult <ProjectType>
                   .Created(returnAddResult, location)
                   .ActionResult());
        }