diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs index 98386a1..e160075 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs @@ -1,5 +1,8 @@ using MediatR; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; +using NetworkResurrector.Agent.PublishedLanguage.Dto; +using NetworkResurrector.Agent.Wrapper.Services; using NetworkResurrector.Api.Domain.Constants; using NetworkResurrector.Api.Domain.Repositories; using NetworkResurrector.Api.PublishedLanguage.Commands; @@ -16,12 +19,16 @@ namespace NetworkResurrector.Api.Application.CommandHandlers private readonly ILogger _logger; private readonly IResurrectorService _resurrectorService; private readonly INetworkRepository _repository; + private readonly IResurrectorAgentService _resurrectorAgentService; + private readonly IConfiguration _configuration; - public ShutdownMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository) + public ShutdownMachineHandler(ILogger logger, IResurrectorService resurrectorService, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration) { _logger=logger; _resurrectorService=resurrectorService; _repository=repository; + _resurrectorAgentService=resurrectorAgentService; + _configuration=configuration; } public async Task Handle(ShutdownMachine command, CancellationToken cancellationToken) @@ -32,18 +39,28 @@ namespace NetworkResurrector.Api.Application.CommandHandlers //log activity + var ipAddressOrMachineName = machine.IPv4Address ?? machine.MachineName; MachineShutdown result; switch (powerConfiguration.Performer.PerformerCode) { case PowerActionPerformers.NETWORK_RESURRECTOR_SERVER: - var pingResult = await _resurrectorService.Shutdown(machine.IPv4Address ?? machine.MachineName); - result = new MachineShutdown(pingResult.Success, pingResult.Status); + if (command.Delay.HasValue || command.Force) + _logger.LogWarning($"Shutdown options are not implemented for '{PowerActionPerformers.NETWORK_RESURRECTOR_SERVER}' performer."); + + var shutdownResult = await _resurrectorService.Shutdown(ipAddressOrMachineName); + result = new MachineShutdown(shutdownResult.Success, shutdownResult.Status); break; case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT: + if (machine.Agent == null) + throw new Exception($"Cannot use network resurrector agent as shutdown performer for machine '{ipAddressOrMachineName}' because it is not configured."); - result = new MachineShutdown(true, ""); + var owner = new ActionOwner(_configuration.GetValue("Service:Code")); + var options = new ActionOptions(command.Delay ?? 0, command.Force); + var shutdownResultByAgent = await _resurrectorAgentService.Shutdown(ipAddressOrMachineName, machine.Agent.AgentPort, owner, options); + var status = shutdownResultByAgent.Success ? $"Machine '{ipAddressOrMachineName}' successfully shutdown." : $"Machine '{ipAddressOrMachineName}' could not be shutdown."; + result = new MachineShutdown(shutdownResultByAgent.Success, status); break; default: diff --git a/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj b/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj index cf4214d..7c8dcc4 100644 --- a/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj +++ b/src/api/NetworkResurrector.Api.Application/NetworkResurrector.Api.Application.csproj @@ -11,6 +11,7 @@ + diff --git a/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs index 3c5b9e1..734090d 100644 --- a/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs +++ b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs @@ -10,6 +10,7 @@ namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration { builder.ToTable("Machine").HasKey(key => key.MachineId); builder.Property(z => z.MachineId).ValueGeneratedOnAdd(); + builder.HasOne(z => z.Agent).WithOne(); } } } diff --git a/src/api/NetworkResurrector.Api.Domain/Entities/Machine.cs b/src/api/NetworkResurrector.Api.Domain/Entities/Machine.cs index 29e4fa7..541f9e5 100644 --- a/src/api/NetworkResurrector.Api.Domain/Entities/Machine.cs +++ b/src/api/NetworkResurrector.Api.Domain/Entities/Machine.cs @@ -8,5 +8,7 @@ public string MACAddress { get; set; } public string IPv4Address { get; set; } public string Description { get; set; } + + public MachineAgent Agent { get; set; } } } diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs index dede1fd..3d7f3b0 100644 --- a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/ShutdownMachine.cs @@ -6,5 +6,7 @@ namespace NetworkResurrector.Api.PublishedLanguage.Commands public class ShutdownMachine : Command { public int MachineId { get; set; } + public int? Delay { get; set; } + public bool Force { get; set; } } } diff --git a/src/api/NetworkResurrector.Api/Startup.cs b/src/api/NetworkResurrector.Api/Startup.cs index b6e062b..73e991b 100644 --- a/src/api/NetworkResurrector.Api/Startup.cs +++ b/src/api/NetworkResurrector.Api/Startup.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Hosting; using NDB.Extensions.Swagger; using NDB.Extensions.Swagger.Constants; using NDB.Security.Authentication.Identity; +using NetworkResurrector.Agent.Wrapper; using NetworkResurrector.Api.Application; using NetworkResurrector.Api.Domain.Data; using NetworkResurrector.Server.Wrapper; @@ -49,6 +50,9 @@ namespace NetworkResurrector.Api // Add network resurrector server services services.UseResurrectorServices(_configuration.GetSection("NetworkResurrectorServer")["BaseAddress"]); + // Add network resurrector agent services + services.UseResurrectorAgentServices(); + // Data access services.AddDataAccess(); diff --git a/src/api/NetworkResurrector.Api/appsettings.json b/src/api/NetworkResurrector.Api/appsettings.json index b600f54..5c30696 100644 --- a/src/api/NetworkResurrector.Api/appsettings.json +++ b/src/api/NetworkResurrector.Api/appsettings.json @@ -12,6 +12,9 @@ } }, "AllowedHosts": "*", + "Service": { + "Code": "NETWORK_RESURRECTOR_API" + }, "IdentityServer": { //"BaseAddress": "http://localhost:5063/" "BaseAddress": "https://toodle.ddns.net/identity-server-api/"