power actions structure
parent
b01587eef8
commit
a3926a06b7
|
@ -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
|
||||
|
||||
MachineShutdown result;
|
||||
|
||||
switch (powerConfiguration.Performer.PerformerCode)
|
||||
{
|
||||
case PowerActionPerformers.NETWORK_RESURRECTOR_SERVER:
|
||||
var pingResult = await _resurrectorService.Shutdown(machine.IPv4Address ?? machine.MachineName);
|
||||
var result = new MachineShutdown(pingResult.Success, pingResult.Status);
|
||||
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;
|
||||
|
|
|
@ -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<Machine> Machines { get; set; }
|
||||
public DbSet<Entities.Power.PowerActionConfiguration> PowerActionConfigurations { get; set; }
|
||||
|
||||
public NetworkDbContext(DbContextOptions<NetworkDbContext> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<MachineAgent>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<MachineAgent> builder)
|
||||
{
|
||||
builder.ToTable("MachineAgent").HasKey(key => key.MachineId);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<PowerAction>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PowerAction> builder)
|
||||
{
|
||||
builder.ToTable("PowerAction").HasKey(key => key.PowerActionId);
|
||||
builder.Property(z => z.PowerActionId).ValueGeneratedOnAdd();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace NetworkResurrector.Api.Domain.Data.EntityTypeConfiguration.Power
|
||||
{
|
||||
class PowerActionConfigurationEf : IEntityTypeConfiguration<Entities.Power.PowerActionConfiguration>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Entities.Power.PowerActionConfiguration> 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<PowerActionPerformer>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<PowerActionPerformer> builder)
|
||||
{
|
||||
builder.ToTable("PowerActionPerformer").HasKey(key => key.PerformerId);
|
||||
builder.Property(z => z.PerformerId).ValueGeneratedOnAdd();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<PowerActionConfiguration> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
}
|
|
@ -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";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace NetworkResurrector.Api.Domain.Entities
|
||||
{
|
||||
public class MachineAgent
|
||||
{
|
||||
public int MachineId { get; set; }
|
||||
public int AgentPort { get; set; }
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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<Machine[]> GetMachines();
|
||||
Task<Machine> GetMachine(int machineId);
|
||||
Task<PowerActionConfiguration> GetPowerActionConfiguration(int machineId, string actionCode);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue