From a97c8d9971e178962bee85f0538556e3c83c2117 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 17 Jan 2022 09:52:54 +0200 Subject: [PATCH] LockMachineHandler --- .../CommandHandlers/LockMachineHandler.cs | 63 +++++++++++++++++++ .../Commands/LockMachine.cs | 10 +++ .../Events/MachineLocked.cs | 4 ++ .../Controllers/ResurrectorController.cs | 7 +++ 4 files changed, 84 insertions(+) create mode 100644 src/api/NetworkResurrector.Api.Application/CommandHandlers/LockMachineHandler.cs create mode 100644 src/api/NetworkResurrector.Api.PublishedLanguage/Commands/LockMachine.cs create mode 100644 src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineLocked.cs diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/LockMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/LockMachineHandler.cs new file mode 100644 index 0000000..9add6c9 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/LockMachineHandler.cs @@ -0,0 +1,63 @@ +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; +using NetworkResurrector.Api.PublishedLanguage.Events; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Api.Application.CommandHandlers +{ + public class LockMachineHandler : IRequestHandler + { + private readonly ILogger _logger; + private readonly INetworkRepository _repository; + private readonly IResurrectorAgentService _resurrectorAgentService; + private readonly IConfiguration _configuration; + + public LockMachineHandler(ILogger logger, INetworkRepository repository, IResurrectorAgentService resurrectorAgentService, IConfiguration configuration) + { + _logger=logger; + _repository=repository; + _resurrectorAgentService=resurrectorAgentService; + _configuration=configuration; + } + + public async Task Handle(LockMachine command, CancellationToken cancellationToken) + { + _logger.LogDebug($"Start locking machine {command.MachineId}"); + var machine = await _repository.GetMachine(command.MachineId); + var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.LOCK); + + //log activity + + var ipAddressOrMachineName = machine.IPv4Address ?? machine.MachineName; + MachineLocked result; + + switch (powerConfiguration.Performer.PerformerCode) + { + case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT: + if (machine.Agent == null) + throw new Exception($"Cannot use network resurrector agent as lock performer for machine '{ipAddressOrMachineName}' because it is not configured."); + + var owner = new ActionOwner(_configuration.GetValue("Service:Code")); + var lockResultByAgent = await _resurrectorAgentService.Lock(ipAddressOrMachineName, machine.Agent.AgentPort, owner); + var status = lockResultByAgent.Success ? $"Machine '{ipAddressOrMachineName}' successfully locked." : $"Machine '{ipAddressOrMachineName}' could not be locked."; + result = new MachineLocked(lockResultByAgent.Success, status); + break; + + default: + throw new Exception($"Power action performer {powerConfiguration.Performer.PerformerCode} is not implemented."); + } + + _logger.LogDebug($"Machine {command.MachineId} locking finished. Success: {result.Success}; Status: {result.Status}"); + + return result; + } + } +} diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/LockMachine.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/LockMachine.cs new file mode 100644 index 0000000..8e3a865 --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Commands/LockMachine.cs @@ -0,0 +1,10 @@ +using NDB.Application.DataContracts; +using NetworkResurrector.Api.PublishedLanguage.Events; + +namespace NetworkResurrector.Api.PublishedLanguage.Commands +{ + public class LockMachine : Command + { + public int MachineId { get; set; } + } +} diff --git a/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineLocked.cs b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineLocked.cs new file mode 100644 index 0000000..a9d829a --- /dev/null +++ b/src/api/NetworkResurrector.Api.PublishedLanguage/Events/MachineLocked.cs @@ -0,0 +1,4 @@ +namespace NetworkResurrector.Api.PublishedLanguage.Events +{ + public record MachineLocked(bool Success, string Status); +} diff --git a/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs b/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs index 9e3ae32..a33bd33 100644 --- a/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs +++ b/src/api/NetworkResurrector.Api/Controllers/ResurrectorController.cs @@ -52,5 +52,12 @@ namespace NetworkResurrector.Api.Controllers var result = await _mediator.Send(sleepMachine); return Ok(result); } + + [HttpPost("lock")] + public async Task LockMachine([FromBody] LockMachine lockMachine) + { + var result = await _mediator.Send(lockMachine); + return Ok(result); + } } }