ValidationService

master
Tudor Stanciu 2021-12-21 12:43:28 +02:00
parent c0b07ed59e
commit 806b94c3c4
11 changed files with 105 additions and 4 deletions

View File

@ -60,6 +60,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Wr
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent.PublishedLanguage", "src\agent\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj", "{2B084ADC-829E-4B72-8FF3-69780E06083D}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent.PublishedLanguage", "src\agent\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj", "{2B084ADC-829E-4B72-8FF3-69780E06083D}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent.Domain", "src\agent\NetworkResurrector.Agent.Domain\NetworkResurrector.Agent.Domain.csproj", "{750531C0-CB4D-44AE-ADDE-CC815F2539C5}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -122,6 +124,10 @@ Global
{2B084ADC-829E-4B72-8FF3-69780E06083D}.Debug|Any CPU.Build.0 = Debug|Any CPU {2B084ADC-829E-4B72-8FF3-69780E06083D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B084ADC-829E-4B72-8FF3-69780E06083D}.Release|Any CPU.ActiveCfg = Release|Any CPU {2B084ADC-829E-4B72-8FF3-69780E06083D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B084ADC-829E-4B72-8FF3-69780E06083D}.Release|Any CPU.Build.0 = Release|Any CPU {2B084ADC-829E-4B72-8FF3-69780E06083D}.Release|Any CPU.Build.0 = Release|Any CPU
{750531C0-CB4D-44AE-ADDE-CC815F2539C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{750531C0-CB4D-44AE-ADDE-CC815F2539C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{750531C0-CB4D-44AE-ADDE-CC815F2539C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{750531C0-CB4D-44AE-ADDE-CC815F2539C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -146,6 +152,7 @@ Global
{3795AB02-7F2A-424B-BFD2-1B915E155829} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} {3795AB02-7F2A-424B-BFD2-1B915E155829} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}
{BA96E3C3-3E18-4882-8BDB-ED7B40836892} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} {BA96E3C3-3E18-4882-8BDB-ED7B40836892} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}
{2B084ADC-829E-4B72-8FF3-69780E06083D} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} {2B084ADC-829E-4B72-8FF3-69780E06083D} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}
{750531C0-CB4D-44AE-ADDE-CC815F2539C5} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7} SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7}

View File

@ -13,11 +13,13 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers
{ {
private readonly ILogger<ShutdownHandler> _logger; private readonly ILogger<ShutdownHandler> _logger;
private readonly IShutdownService _shutdownService; private readonly IShutdownService _shutdownService;
private readonly IValidationService _validationService;
public ShutdownHandler(ILogger<ShutdownHandler> logger, IShutdownService shutdownService) public ShutdownHandler(ILogger<ShutdownHandler> logger, IShutdownService shutdownService, IValidationService validationService)
{ {
_logger=logger; _logger=logger;
_shutdownService=shutdownService; _shutdownService=shutdownService;
_validationService=validationService;
} }
public async Task<ShutdownResult> Handle(Shutdown command, CancellationToken cancellationToken) public async Task<ShutdownResult> Handle(Shutdown command, CancellationToken cancellationToken)
@ -27,6 +29,8 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers
private ShutdownResult Handle(Shutdown command) private ShutdownResult Handle(Shutdown command)
{ {
_validationService.ValidateRestrictions(command.Owner);
_logger.LogDebug($"Start shutting down the system."); _logger.LogDebug($"Start shutting down the system.");
var stopWatch = new Stopwatch(); var stopWatch = new Stopwatch();

View File

@ -8,7 +8,8 @@ namespace NetworkResurrector.Agent.Application
{ {
public static void AddApplicationServices(this IServiceCollection services) public static void AddApplicationServices(this IServiceCollection services)
{ {
// services.AddSingleton<IParamProvider, ParamProvider>(); services.AddSingleton<IParamProvider, ParamProvider>();
services.AddSingleton<IValidationService, ValidationService>();
services.AddSingleton<IShutdownService, ShutdownService>(); services.AddSingleton<IShutdownService, ShutdownService>();
} }
} }

View File

@ -14,6 +14,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\NetworkResurrector.Agent.Domain\NetworkResurrector.Agent.Domain.csproj" />
<ProjectReference Include="..\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj" /> <ProjectReference Include="..\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,9 @@
using NetworkResurrector.Agent.Domain.Models;
namespace NetworkResurrector.Agent.Application.Services.Abstractions
{
public interface IParamProvider
{
ConfigurationRecords.Restrictions Restrictions { get; }
}
}

View File

@ -0,0 +1,9 @@
using NetworkResurrector.Agent.PublishedLanguage.Dto;
namespace NetworkResurrector.Agent.Application.Services.Abstractions
{
public interface IValidationService
{
void ValidateRestrictions(ActionOwner actionOwner);
}
}

View File

@ -0,0 +1,18 @@
using Microsoft.Extensions.Configuration;
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.Domain.Models;
namespace NetworkResurrector.Agent.Application.Services
{
internal class ParamProvider : IParamProvider
{
private readonly IConfiguration _configuration;
public ParamProvider(IConfiguration configuration)
{
_configuration = configuration;
}
public ConfigurationRecords.Restrictions Restrictions => _configuration.GetSection("Restrictions").Get<ConfigurationRecords.Restrictions>();
}
}

View File

@ -0,0 +1,23 @@
using NetworkResurrector.Agent.Application.Services.Abstractions;
using NetworkResurrector.Agent.Domain.Errors;
using NetworkResurrector.Agent.PublishedLanguage.Dto;
using System;
namespace NetworkResurrector.Agent.Application.Services
{
internal class ValidationService : IValidationService
{
private readonly IParamProvider _paramProvider;
public ValidationService(IParamProvider paramProvider)
{
_paramProvider=paramProvider;
}
public void ValidateRestrictions(ActionOwner actionOwner)
{
if (_paramProvider.Restrictions.EnforceActionOwner && (actionOwner == null || string.IsNullOrWhiteSpace(actionOwner.OwnerCode)))
throw new Exception(Errors.ACTION_OWNER_NOT_SPECIFIED.Code);
}
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NetworkResurrector.Agent.Domain.Errors
{
public struct Errors
{
public record Error(string Code, string Message);
public static Error ACTION_OWNER_NOT_SPECIFIED = new("ACTION_OWNER_NOT_SPECIFIED", "Action owner was not specified.");
}
}

View File

@ -0,0 +1,7 @@
namespace NetworkResurrector.Agent.Domain.Models
{
public class ConfigurationRecords
{
public record Restrictions(bool EnforceActionOwner);
}
}

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>