Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed max terraform paralellism to a setting #50

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Caster.Api/Domain/Services/TerraformService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ public TerraformResult Plan(Workspace workspace, bool destroy, string[] targets,
{
List<string> args = new List<string> { "plan", "-input=false", "-out=plan" };

if (workspace.Parallelism.HasValue)
{
args.Add($"-parallelism={workspace.Parallelism}");
}

if (destroy)
{
args.Add("-destroy");
Expand Down
6 changes: 4 additions & 2 deletions src/Caster.Api/Features/Directories/Requests/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using Caster.Api.Infrastructure.Extensions;
using Caster.Api.Features.Shared.Services;
using Caster.Api.Features.Shared.Validators;
using Microsoft.Extensions.Options;
using Caster.Api.Infrastructure.Options;

namespace Caster.Api.Features.Directories
{
Expand Down Expand Up @@ -80,11 +82,11 @@ public class Command : IRequest<Directory>, IDirectoryUpdateRequest

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator(IValidationService validationService)
public CommandValidator(IValidationService validationService, TerraformOptions options)
{
RuleFor(x => x.ProjectId).ProjectExists(validationService);
RuleFor(x => x.Parallelism.Value)
.ParalellismValidation()
.ParalellismValidation(options)
.When(x => x.Parallelism.HasValue);
RuleFor(x => x.AzureDestroyFailureThreshold.Value)
.AzureThresholdValidation()
Expand Down
5 changes: 3 additions & 2 deletions src/Caster.Api/Features/Directories/Requests/Edit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Caster.Api.Infrastructure.Extensions;
using Caster.Api.Features.Shared.Services;
using Caster.Api.Features.Shared.Validators;
using Caster.Api.Infrastructure.Options;

namespace Caster.Api.Features.Directories
{
Expand Down Expand Up @@ -74,11 +75,11 @@ public class Command : IRequest<Directory>, IDirectoryUpdateRequest

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator(IValidationService validationService)
public CommandValidator(IValidationService validationService, TerraformOptions options)
{
RuleFor(x => x.ParentId.Value).DirectoryExists(validationService).When(x => x.ParentId.HasValue);
RuleFor(x => x.Parallelism.Value)
.ParalellismValidation()
.ParalellismValidation(options)
.When(x => x.Parallelism.HasValue);
RuleFor(x => x.AzureDestroyFailureThreshold.Value)
.AzureThresholdValidation()
Expand Down
5 changes: 3 additions & 2 deletions src/Caster.Api/Features/Directories/Requests/PartialEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Caster.Api.Infrastructure.Extensions;
using System.Text.Json.Serialization;
using Caster.Api.Features.Shared.Validators;
using Caster.Api.Infrastructure.Options;

namespace Caster.Api.Features.Directories
{
Expand Down Expand Up @@ -76,11 +77,11 @@ public class Command : IRequest<Directory>, IDirectoryUpdateRequest

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator(IValidationService validationService)
public CommandValidator(IValidationService validationService, TerraformOptions options)
{
RuleFor(x => x.ParentId.Value.Value).DirectoryExists(validationService).When(x => x.ParentId.HasValue && x.ParentId.Value.HasValue);
RuleFor(x => x.Parallelism.Value.Value)
.ParalellismValidation()
.ParalellismValidation(options)
.When(x => x.Parallelism.HasValue && x.Parallelism.Value.HasValue);
RuleFor(x => x.AzureDestroyFailureThreshold.Value.Value)
.AzureThresholdValidation()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Copyright 2021 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using Caster.Api.Infrastructure.Options;
using FluentValidation;

namespace Caster.Api.Features.Shared.Validators;

public static class TerraformValidationRules
{
public static IRuleBuilderOptions<T, int> ParalellismValidation<T>(this IRuleBuilder<T, int> rule)
public static IRuleBuilderOptions<T, int> ParalellismValidation<T>(this IRuleBuilder<T, int> rule, TerraformOptions options)
{
return rule
.GreaterThan(0)
.LessThan(25);
.LessThan(options.MaxParallelism);
}

public static IRuleBuilderOptions<T, int> AzureThresholdValidation<T>(this IRuleBuilder<T, int> rule)
Expand Down
56 changes: 56 additions & 0 deletions src/Caster.Api/Features/Terraform/Requests/GetMaxParallelism.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2021 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using System.Threading;
using System.Threading.Tasks;
using MediatR;
using System.Runtime.Serialization;
using Caster.Api.Infrastructure.Exceptions;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Caster.Api.Infrastructure.Authorization;
using Caster.Api.Infrastructure.Identity;
using Caster.Api.Domain.Services;
using System;
using Caster.Api.Data;
using AutoMapper;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using Caster.Api.Infrastructure.Options;
using System.Linq;

namespace Caster.Api.Features.Terraform
{
public class GetMaxParallelism
{
[DataContract(Name = "GetTerraformMaxParallelism")]
public class Query : IRequest<int>
{
}

public class Handler : IRequestHandler<Query, int>
{
private readonly IAuthorizationService _authorizationService;
private readonly ClaimsPrincipal _user;
private readonly TerraformOptions _terraformOptions;

public Handler(
IAuthorizationService authorizationService,
IIdentityResolver identityResolver,
TerraformOptions terraformOptions)
{
_authorizationService = authorizationService;
_user = identityResolver.GetClaimsPrincipal();
_terraformOptions = terraformOptions;
}

public async Task<int> Handle(Query request, CancellationToken cancellationToken)
{
if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
throw new ForbiddenException();

return _terraformOptions.MaxParallelism;
}
}
}
}
12 changes: 12 additions & 0 deletions src/Caster.Api/Features/Terraform/TerraformController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,17 @@ public async Task<IActionResult> GetVersions()
var result = await this._mediator.Send(new GetVersions.Query());
return Ok(result);
}

/// <summary>
/// Get the maximum allowed parallelism setting value
/// </summary>
[HttpGet("terraform/max-parallelism")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
[SwaggerOperation(OperationId = "GetTerraformMaxParallelism")]
public async Task<IActionResult> GetMaxParallelism()
{
var result = await this._mediator.Send(new GetMaxParallelism.Query());
return Ok(result);
}
}
}
4 changes: 2 additions & 2 deletions src/Caster.Api/Features/Workspaces/Requests/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ public class Command : IRequest<Workspace>, IWorkspaceUpdateRequest

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator(IValidationService validationService)
public CommandValidator(IValidationService validationService, TerraformOptions options)
{
RuleFor(x => x.DirectoryId).DirectoryExists(validationService);
RuleFor(x => x.Parallelism.Value)
.ParalellismValidation()
.ParalellismValidation(options)
.When(x => x.Parallelism.HasValue);
RuleFor(x => x.AzureDestroyFailureThreshold.Value)
.AzureThresholdValidation()
Expand Down
5 changes: 3 additions & 2 deletions src/Caster.Api/Features/Workspaces/Requests/Edit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Caster.Api.Infrastructure.Extensions;
using Caster.Api.Features.Shared.Services;
using Caster.Api.Features.Shared.Validators;
using Caster.Api.Infrastructure.Options;

namespace Caster.Api.Features.Workspaces
{
Expand Down Expand Up @@ -72,11 +73,11 @@ public class Command : IRequest<Workspace>, IWorkspaceUpdateRequest

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator(IValidationService validationService)
public CommandValidator(IValidationService validationService, TerraformOptions options)
{
RuleFor(x => x.DirectoryId).DirectoryExists(validationService);
RuleFor(x => x.Parallelism.Value)
.ParalellismValidation()
.ParalellismValidation(options)
.When(x => x.Parallelism.HasValue);
RuleFor(x => x.AzureDestroyFailureThreshold.Value)
.AzureThresholdValidation()
Expand Down
5 changes: 3 additions & 2 deletions src/Caster.Api/Features/Workspaces/Requests/PartialEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Caster.Api.Infrastructure.Extensions;
using System.Text.Json.Serialization;
using Caster.Api.Features.Shared.Validators;
using Caster.Api.Infrastructure.Options;

namespace Caster.Api.Features.Workspaces
{
Expand Down Expand Up @@ -74,11 +75,11 @@ public class Command : IRequest<Workspace>, IWorkspaceUpdateRequest

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator(IValidationService validationService)
public CommandValidator(IValidationService validationService, TerraformOptions options)
{
RuleFor(x => x.DirectoryId.Value).DirectoryExists(validationService).When(x => x.DirectoryId.HasValue);
RuleFor(x => x.Parallelism.Value.Value)
.ParalellismValidation()
.ParalellismValidation(options)
.When(x => x.Parallelism.HasValue && x.Parallelism.Value.HasValue);
RuleFor(x => x.AzureDestroyFailureThreshold.Value.Value)
.AzureThresholdValidation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ public class TerraformOptions
public int StateRetryCount { get; set; }
public int StateRetryIntervalSeconds { get; set; }
public int? AzureDestroyFailureThreshhold { get; set; }
public int MaxParallelism { get; set; }
}
}
3 changes: 2 additions & 1 deletion src/Caster.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"GitlabGroupId": null,
"StateRetryCount": 12,
"StateRetryIntervalSeconds": 5,
"AzureDestroyFailureThreshhold": 2
"AzureDestroyFailureThreshhold": 2,
"MaxParallelism": 25
},
"FileVersions": {
"DaysToSaveAllUntaggedVersions": 7,
Expand Down
Loading