From 806b94c3c436f44822b2c343987446a1edda542f Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Tue, 21 Dec 2021 12:43:28 +0200 Subject: [PATCH] ValidationService --- NetworkResurrector.sln | 7 ++++++ .../CommandHandlers/ShutdownHandler.cs | 10 +++++--- .../DependencyInjectionExtensions.cs | 3 ++- ...etworkResurrector.Agent.Application.csproj | 1 + .../Services/Abstractions/IParamProvider.cs | 9 ++++++++ .../Abstractions/IValidationService.cs | 9 ++++++++ .../Services/ParamProvider.cs | 18 +++++++++++++++ .../Services/ValidationService.cs | 23 +++++++++++++++++++ .../Constants/Errors.cs | 15 ++++++++++++ .../Models/ConfigurationRecords.cs | 7 ++++++ .../NetworkResurrector.Agent.Domain.csproj | 7 ++++++ 11 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IParamProvider.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IValidationService.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/Services/ParamProvider.cs create mode 100644 src/agent/NetworkResurrector.Agent.Application/Services/ValidationService.cs create mode 100644 src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs create mode 100644 src/agent/NetworkResurrector.Agent.Domain/Models/ConfigurationRecords.cs create mode 100644 src/agent/NetworkResurrector.Agent.Domain/NetworkResurrector.Agent.Domain.csproj diff --git a/NetworkResurrector.sln b/NetworkResurrector.sln index be95ef9..a68b6aa 100644 --- a/NetworkResurrector.sln +++ b/NetworkResurrector.sln @@ -60,6 +60,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Wr EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetworkResurrector.Agent.PublishedLanguage", "src\agent\NetworkResurrector.Agent.PublishedLanguage\NetworkResurrector.Agent.PublishedLanguage.csproj", "{2B084ADC-829E-4B72-8FF3-69780E06083D}" 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 GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -146,6 +152,7 @@ Global {3795AB02-7F2A-424B-BFD2-1B915E155829} = {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} + {750531C0-CB4D-44AE-ADDE-CC815F2539C5} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7} diff --git a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs index a24d408..c481efc 100644 --- a/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs +++ b/src/agent/NetworkResurrector.Agent.Application/CommandHandlers/ShutdownHandler.cs @@ -13,11 +13,13 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers { private readonly ILogger _logger; private readonly IShutdownService _shutdownService; + private readonly IValidationService _validationService; - public ShutdownHandler(ILogger logger, IShutdownService shutdownService) + public ShutdownHandler(ILogger logger, IShutdownService shutdownService, IValidationService validationService) { - _logger = logger; - _shutdownService = shutdownService; + _logger=logger; + _shutdownService=shutdownService; + _validationService=validationService; } public async Task Handle(Shutdown command, CancellationToken cancellationToken) @@ -27,6 +29,8 @@ namespace NetworkResurrector.Agent.Application.CommandHandlers private ShutdownResult Handle(Shutdown command) { + _validationService.ValidateRestrictions(command.Owner); + _logger.LogDebug($"Start shutting down the system."); var stopWatch = new Stopwatch(); diff --git a/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs b/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs index 75c055c..0a73d65 100644 --- a/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs +++ b/src/agent/NetworkResurrector.Agent.Application/DependencyInjectionExtensions.cs @@ -8,7 +8,8 @@ namespace NetworkResurrector.Agent.Application { public static void AddApplicationServices(this IServiceCollection services) { - // services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); } } diff --git a/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj b/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj index 3ad4980..7e2cce8 100644 --- a/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj +++ b/src/agent/NetworkResurrector.Agent.Application/NetworkResurrector.Agent.Application.csproj @@ -14,6 +14,7 @@ + diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IParamProvider.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IParamProvider.cs new file mode 100644 index 0000000..e969acf --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IParamProvider.cs @@ -0,0 +1,9 @@ +using NetworkResurrector.Agent.Domain.Models; + +namespace NetworkResurrector.Agent.Application.Services.Abstractions +{ + public interface IParamProvider + { + ConfigurationRecords.Restrictions Restrictions { get; } + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IValidationService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IValidationService.cs new file mode 100644 index 0000000..417f796 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/IValidationService.cs @@ -0,0 +1,9 @@ +using NetworkResurrector.Agent.PublishedLanguage.Dto; + +namespace NetworkResurrector.Agent.Application.Services.Abstractions +{ + public interface IValidationService + { + void ValidateRestrictions(ActionOwner actionOwner); + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/ParamProvider.cs b/src/agent/NetworkResurrector.Agent.Application/Services/ParamProvider.cs new file mode 100644 index 0000000..40934e3 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Services/ParamProvider.cs @@ -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(); + } +} diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/ValidationService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/ValidationService.cs new file mode 100644 index 0000000..819d2f5 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Application/Services/ValidationService.cs @@ -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); + } + } +} diff --git a/src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs b/src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs new file mode 100644 index 0000000..b7e6149 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Domain/Constants/Errors.cs @@ -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."); + } +} diff --git a/src/agent/NetworkResurrector.Agent.Domain/Models/ConfigurationRecords.cs b/src/agent/NetworkResurrector.Agent.Domain/Models/ConfigurationRecords.cs new file mode 100644 index 0000000..5d21d8a --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Domain/Models/ConfigurationRecords.cs @@ -0,0 +1,7 @@ +namespace NetworkResurrector.Agent.Domain.Models +{ + public class ConfigurationRecords + { + public record Restrictions(bool EnforceActionOwner); + } +} diff --git a/src/agent/NetworkResurrector.Agent.Domain/NetworkResurrector.Agent.Domain.csproj b/src/agent/NetworkResurrector.Agent.Domain/NetworkResurrector.Agent.Domain.csproj new file mode 100644 index 0000000..f208d30 --- /dev/null +++ b/src/agent/NetworkResurrector.Agent.Domain/NetworkResurrector.Agent.Domain.csproj @@ -0,0 +1,7 @@ + + + + net5.0 + + +