Improved admin server crud
This commit is contained in:
@@ -4,12 +4,16 @@
|
|||||||
@using Microsoft.EntityFrameworkCore
|
@using Microsoft.EntityFrameworkCore
|
||||||
@using MoonCore.Abstractions
|
@using MoonCore.Abstractions
|
||||||
@using MoonCore.Exceptions
|
@using MoonCore.Exceptions
|
||||||
|
@using MoonCore.Helpers
|
||||||
|
@using MoonCoreUI.Services
|
||||||
@using Moonlight.Features.Servers.Entities
|
@using Moonlight.Features.Servers.Entities
|
||||||
|
@using Moonlight.Features.Servers.Models.Enums
|
||||||
@using Moonlight.Features.Servers.Models.Forms.Admin.Servers
|
@using Moonlight.Features.Servers.Models.Forms.Admin.Servers
|
||||||
@using Moonlight.Features.Servers.Services
|
@using Moonlight.Features.Servers.Services
|
||||||
|
|
||||||
@inject ServerService ServerService
|
@inject ServerService ServerService
|
||||||
@inject Repository<Server> ServerRepository
|
@inject Repository<Server> ServerRepository
|
||||||
|
@inject ToastService ToastService
|
||||||
|
|
||||||
@attribute [RequirePermission(5000)]
|
@attribute [RequirePermission(5000)]
|
||||||
|
|
||||||
@@ -20,6 +24,7 @@
|
|||||||
TUpdateForm="CreateServerForm"
|
TUpdateForm="CreateServerForm"
|
||||||
Loader="Load"
|
Loader="Load"
|
||||||
CustomAdd="CustomAdd"
|
CustomAdd="CustomAdd"
|
||||||
|
CustomUpdate="CustomUpdate"
|
||||||
ValidateUpdate="ValidateUpdate"
|
ValidateUpdate="ValidateUpdate"
|
||||||
CustomDelete="CustomDelete">
|
CustomDelete="CustomDelete">
|
||||||
<View>
|
<View>
|
||||||
@@ -42,7 +47,7 @@
|
|||||||
</CrudColumn>
|
</CrudColumn>
|
||||||
</View>
|
</View>
|
||||||
<CustomLoaders>
|
<CustomLoaders>
|
||||||
<DefineCustomLoader T="ServerAllocation" Id="FreeAllocations" Func="LoadFreeAllocations" />
|
<DefineCustomLoader TItem="Server" T="ServerAllocation" Id="FreeAllocations" Func="LoadFreeAllocations"/>
|
||||||
</CustomLoaders>
|
</CustomLoaders>
|
||||||
<NoItemsView>
|
<NoItemsView>
|
||||||
<IconAlert Title="No servers found" Color="primary" Icon="bx-search-alt">
|
<IconAlert Title="No servers found" Color="primary" Icon="bx-search-alt">
|
||||||
@@ -63,26 +68,75 @@
|
|||||||
.Include(x => x.Node);
|
.Include(x => x.Node);
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<ServerAllocation> LoadFreeAllocations(Repository<ServerAllocation> repository)
|
private IEnumerable<ServerAllocation> LoadFreeAllocations(Repository<ServerAllocation> repository, Server currentServer)
|
||||||
{
|
{
|
||||||
return repository
|
return currentServer.Allocations.Concat(
|
||||||
|
repository
|
||||||
.Get()
|
.Get()
|
||||||
.FromSqlRaw("SELECT * FROM `ServerAllocations` WHERE ServerId IS NULL");
|
.FromSqlRaw($"SELECT * FROM `ServerAllocations` WHERE ServerId IS NULL AND ServerNodeId = {currentServer.Node.Id}")
|
||||||
|
.AsEnumerable() // => executes the sql
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CustomAdd(Server form) => await ServerService.Create(form);
|
private async Task CustomAdd(Server form) => await ServerService.Create(form);
|
||||||
|
|
||||||
private async Task CustomDelete(Server s) => await ServerService.Delete(s);
|
private async Task CustomDelete(Server s) => await ServerService.Delete(s);
|
||||||
|
|
||||||
|
private async Task CustomUpdate(Server server)
|
||||||
|
{
|
||||||
|
ServerRepository.Update(server);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Let the daemon know we changed this server
|
||||||
|
await ServerService.Sync(server);
|
||||||
|
|
||||||
|
// Check if the server is running to let the user know if he needs to restart the
|
||||||
|
// server. This should prevent the confusion why a running server does not get the changes applied
|
||||||
|
// ... hopefully ;)
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (await ServerService.GetState(server) == ServerState.Offline)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await ToastService.Info("Server is currently running. It requires a restart of the server in order to apply the changes");
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// ignore, sync has already happened
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Logger.Error("Unable to sync server changes due to an error occuring");
|
||||||
|
Logger.Error(e);
|
||||||
|
|
||||||
|
await ToastService.Danger("An error occured while sending the changes to the daemon");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Task ValidateUpdate(Server server)
|
private Task ValidateUpdate(Server server)
|
||||||
{
|
{
|
||||||
var oldServer = ServerRepository
|
var oldServer = ServerRepository
|
||||||
.Get()
|
.Get()
|
||||||
|
.Include(x => x.Image)
|
||||||
.First(x => x.Id == server.Id);
|
.First(x => x.Id == server.Id);
|
||||||
|
|
||||||
|
// Virtual disk check
|
||||||
if (oldServer.UseVirtualDisk != server.UseVirtualDisk)
|
if (oldServer.UseVirtualDisk != server.UseVirtualDisk)
|
||||||
throw new DisplayException("Unable to switch from/to virtual disks. This is not supported at the moment");
|
throw new DisplayException("Unable to switch from/to virtual disks. This is not supported at the moment");
|
||||||
|
|
||||||
|
// Allocation amount check
|
||||||
|
if (server.Allocations.Count < oldServer.Image.AllocationsNeeded)
|
||||||
|
throw new DisplayException($"The server image requires at least {oldServer.Image.AllocationsNeeded} allocation(s) in order to work");
|
||||||
|
|
||||||
|
// Set the correct main allocation
|
||||||
|
server.MainAllocation = server.Allocations.First();
|
||||||
|
|
||||||
|
// Check for image changes
|
||||||
|
if (oldServer.Image.Id != server.Image.Id)
|
||||||
|
throw new DisplayException("Changing the server image has not been implemented yet");
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user