From a3926a06b7533a38b9e615325da45ec43cf7eb19 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Fri, 14 Jan 2022 01:22:39 +0200 Subject: [PATCH] power actions structure --- .../CommandHandlers/ShutdownMachineHandler.cs | 24 +++++++++++++++++-- .../DbContexts/NetworkDbContext.cs | 5 ++++ .../MachineAgentConfiguration.cs | 14 +++++++++++ .../Power/PowerActionConfiguration.cs | 15 ++++++++++++ .../Power/PowerActionConfigurationEf.cs | 16 +++++++++++++ .../PowerActionPerformerConfiguration.cs | 15 ++++++++++++ .../Repositories/NetworkRepository.cs | 14 +++++++++++ .../Constants/PowerActionPerformers.cs | 9 +++++++ .../Constants/PowerActions.cs | 14 +++++++++++ .../Entities/MachineAgent.cs | 8 +++++++ .../Entities/Power/PowerAction.cs | 9 +++++++ .../Power/PowerActionConfiguration.cs | 12 ++++++++++ .../Entities/Power/PowerActionPerformer.cs | 9 +++++++ .../Repositories/INetworkRepository.cs | 2 ++ 14 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineAgentConfiguration.cs create mode 100644 src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfiguration.cs create mode 100644 src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfigurationEf.cs create mode 100644 src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionPerformerConfiguration.cs create mode 100644 src/api/NetworkResurrector.Api.Domain/Constants/PowerActionPerformers.cs create mode 100644 src/api/NetworkResurrector.Api.Domain/Constants/PowerActions.cs create mode 100644 src/api/NetworkResurrector.Api.Domain/Entities/MachineAgent.cs create mode 100644 src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerAction.cs create mode 100644 src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionConfiguration.cs create mode 100644 src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionPerformer.cs diff --git a/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs b/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs index f7c0c80..98386a1 100644 --- a/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs +++ b/src/api/NetworkResurrector.Api.Application/CommandHandlers/ShutdownMachineHandler.cs @@ -1,9 +1,11 @@ using MediatR; using Microsoft.Extensions.Logging; +using NetworkResurrector.Api.Domain.Constants; using NetworkResurrector.Api.Domain.Repositories; using NetworkResurrector.Api.PublishedLanguage.Commands; using NetworkResurrector.Api.PublishedLanguage.Events; using NetworkResurrector.Server.Wrapper.Services; +using System; using System.Threading; using System.Threading.Tasks; @@ -26,10 +28,28 @@ namespace NetworkResurrector.Api.Application.CommandHandlers { _logger.LogDebug($"Start shutdown machine {command.MachineId}"); var machine = await _repository.GetMachine(command.MachineId); + var powerConfiguration = await _repository.GetPowerActionConfiguration(command.MachineId, PowerActions.SHUTDOWN); //log activity - var pingResult = await _resurrectorService.Shutdown(machine.IPv4Address ?? machine.MachineName); - var result = new MachineShutdown(pingResult.Success, pingResult.Status); + + 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); + break; + + case PowerActionPerformers.NETWORK_RESURRECTOR_AGENT: + + result = new MachineShutdown(true, ""); + break; + + default: + throw new Exception($"Power action performer {powerConfiguration.Performer.PerformerCode} is not implemented."); + } + _logger.LogDebug($"Machine {command.MachineId} shutdown finished. Success: {result.Success}; Status: {result.Status}"); return result; diff --git a/src/api/NetworkResurrector.Api.Domain.Data/DbContexts/NetworkDbContext.cs b/src/api/NetworkResurrector.Api.Domain.Data/DbContexts/NetworkDbContext.cs index daa9e7e..3a422cf 100644 --- a/src/api/NetworkResurrector.Api.Domain.Data/DbContexts/NetworkDbContext.cs +++ b/src/api/NetworkResurrector.Api.Domain.Data/DbContexts/NetworkDbContext.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration; +using NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration.Power; using NetworkResurrector.Api.Domain.Entities; namespace NetworkResurrector.Api.Domain.Data.DbContexts @@ -7,6 +8,7 @@ namespace NetworkResurrector.Api.Domain.Data.DbContexts public class NetworkDbContext : DbContext { public DbSet Machines { get; set; } + public DbSet PowerActionConfigurations { get; set; } public NetworkDbContext(DbContextOptions options) : base(options) @@ -20,6 +22,9 @@ namespace NetworkResurrector.Api.Domain.Data.DbContexts base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfiguration(new MachineConfiguration()); + modelBuilder.ApplyConfiguration(new PowerActionConfiguration()); + modelBuilder.ApplyConfiguration(new PowerActionPerformerConfiguration()); + modelBuilder.ApplyConfiguration(new MachineAgentConfiguration()); } } } diff --git a/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineAgentConfiguration.cs b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineAgentConfiguration.cs new file mode 100644 index 0000000..6d405e9 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/MachineAgentConfiguration.cs @@ -0,0 +1,14 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using NetworkResurrector.Api.Domain.Entities; + +namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration +{ + class MachineAgentConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("MachineAgent").HasKey(key => key.MachineId); + } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfiguration.cs b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfiguration.cs new file mode 100644 index 0000000..7222453 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfiguration.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using NetworkResurrector.Api.Domain.Entities.Power; + +namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration.Power +{ + class PowerActionConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("PowerAction").HasKey(key => key.PowerActionId); + builder.Property(z => z.PowerActionId).ValueGeneratedOnAdd(); + } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfigurationEf.cs b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfigurationEf.cs new file mode 100644 index 0000000..4c32c0f --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionConfigurationEf.cs @@ -0,0 +1,16 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration.Power +{ + class PowerActionConfigurationEf : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("PowerActionConfiguration").HasKey(key => key.ConfigurationId); + builder.Property(z => z.ConfigurationId).ValueGeneratedOnAdd(); + builder.HasOne(z => z.PowerAction).WithMany().HasForeignKey(z => z.PowerActionId); + builder.HasOne(z => z.Performer).WithMany().HasForeignKey(z => z.PerformerId); + } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionPerformerConfiguration.cs b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionPerformerConfiguration.cs new file mode 100644 index 0000000..7e89849 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain.Data/EntityTypeConfiguration/Power/PowerActionPerformerConfiguration.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using NetworkResurrector.Api.Domain.Entities.Power; + +namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration.Power +{ + class PowerActionPerformerConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("PowerActionPerformer").HasKey(key => key.PerformerId); + builder.Property(z => z.PerformerId).ValueGeneratedOnAdd(); + } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs b/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs index 1280b9a..ee77343 100644 --- a/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs +++ b/src/api/NetworkResurrector.Api.Domain.Data/Repositories/NetworkRepository.cs @@ -1,6 +1,7 @@ using Microsoft.EntityFrameworkCore; using NetworkResurrector.Api.Domain.Data.DbContexts; using NetworkResurrector.Api.Domain.Entities; +using NetworkResurrector.Api.Domain.Entities.Power; using NetworkResurrector.Api.Domain.Repositories; using System; using System.Threading.Tasks; @@ -29,5 +30,18 @@ namespace NetworkResurrector.Api.Domain.Data.Repositories return machine; } + + public async Task GetPowerActionConfiguration(int machineId, string actionCode) + { + var config = await _dbContext.PowerActionConfigurations + .Include(z => z.PowerAction) + .Include(z => z.Performer) + .FirstOrDefaultAsync(z => z.MachineId == machineId && z.PowerAction.PowerActionCode == actionCode); + + if (config == null) + throw new Exception($"Action '{actionCode}' is not configured for machine '{machineId}'."); + + return config; + } } } diff --git a/src/api/NetworkResurrector.Api.Domain/Constants/PowerActionPerformers.cs b/src/api/NetworkResurrector.Api.Domain/Constants/PowerActionPerformers.cs new file mode 100644 index 0000000..44a89b1 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain/Constants/PowerActionPerformers.cs @@ -0,0 +1,9 @@ +namespace NetworkResurrector.Api.Domain.Constants +{ + public struct PowerActionPerformers + { + public const string + NETWORK_RESURRECTOR_SERVER = "NETWORK_RESURRECTOR_SERVER", + NETWORK_RESURRECTOR_AGENT = "NETWORK_RESURRECTOR_AGENT"; + } +} diff --git a/src/api/NetworkResurrector.Api.Domain/Constants/PowerActions.cs b/src/api/NetworkResurrector.Api.Domain/Constants/PowerActions.cs new file mode 100644 index 0000000..85259e4 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain/Constants/PowerActions.cs @@ -0,0 +1,14 @@ +namespace NetworkResurrector.Api.Domain.Constants +{ + public struct PowerActions + { + public const string + WAKE = "WAKE", + PING = "PING", + SHUTDOWN = "SHUTDOWN", + RESTART = "RESTART", + SLEEP = "SLEEP", + LOGOUT = "LOGOUT", + LOCK = "LOCK"; + } +} diff --git a/src/api/NetworkResurrector.Api.Domain/Entities/MachineAgent.cs b/src/api/NetworkResurrector.Api.Domain/Entities/MachineAgent.cs new file mode 100644 index 0000000..fa9f505 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain/Entities/MachineAgent.cs @@ -0,0 +1,8 @@ +namespace NetworkResurrector.Api.Domain.Entities +{ + public class MachineAgent + { + public int MachineId { get; set; } + public int AgentPort { get; set; } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerAction.cs b/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerAction.cs new file mode 100644 index 0000000..1f9c553 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerAction.cs @@ -0,0 +1,9 @@ +namespace NetworkResurrector.Api.Domain.Entities.Power +{ + public class PowerAction + { + public int PowerActionId { get; set; } + public string PowerActionCode { get; set; } + public string PowerActionName { get; set; } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionConfiguration.cs b/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionConfiguration.cs new file mode 100644 index 0000000..1ba2aa1 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionConfiguration.cs @@ -0,0 +1,12 @@ +namespace NetworkResurrector.Api.Domain.Entities.Power +{ + public class PowerActionConfiguration + { + public int ConfigurationId { get; set; } + public int MachineId { get; set; } + public int PowerActionId { get; set; } + public int PerformerId { get; set; } + public PowerAction PowerAction { get; set; } + public PowerActionPerformer Performer { get; set; } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionPerformer.cs b/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionPerformer.cs new file mode 100644 index 0000000..bab3c77 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Domain/Entities/Power/PowerActionPerformer.cs @@ -0,0 +1,9 @@ +namespace NetworkResurrector.Api.Domain.Entities.Power +{ + public class PowerActionPerformer + { + public int PerformerId { get; set; } + public string PerformerCode { get; set; } + public string PerformerName { get; set; } + } +} diff --git a/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs b/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs index 29ea5db..9d1c272 100644 --- a/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs +++ b/src/api/NetworkResurrector.Api.Domain/Repositories/INetworkRepository.cs @@ -1,4 +1,5 @@ using NetworkResurrector.Api.Domain.Entities; +using NetworkResurrector.Api.Domain.Entities.Power; using System.Threading.Tasks; namespace NetworkResurrector.Api.Domain.Repositories @@ -7,5 +8,6 @@ namespace NetworkResurrector.Api.Domain.Repositories { Task GetMachines(); Task GetMachine(int machineId); + Task GetPowerActionConfiguration(int machineId, string actionCode); } }