Cleaned up interfaces. Extracted server state machine trigger handler to seperated classes. Removed legacy code

This commit is contained in:
2025-09-06 15:34:35 +02:00
parent 7587a7e8e3
commit 348e9560ab
97 changed files with 1256 additions and 4670 deletions

View File

@@ -1,9 +1,9 @@
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using MoonCore.Extended.Models;
using MoonCore.Models;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Mappers;
@@ -28,56 +28,57 @@ public class NodeAllocationsController : Controller
AllocationRepository = allocationRepository;
}
[HttpGet("")]
[HttpGet]
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
public async Task<IPagedData<NodeAllocationResponse>> Get(
public async Task<ActionResult<IPagedData<NodeAllocationResponse>>> Get(
[FromRoute] int nodeId,
[FromQuery] [Range(0, int.MaxValue)] int page,
[FromQuery] [Range(1, 100)] int pageSize
[FromQuery] PagedOptions options
)
{
var count = await AllocationRepository.Get().CountAsync(x => x.Node.Id == nodeId);
var count = await AllocationRepository
.Get()
.CountAsync(x => x.Node.Id == nodeId);
var allocations = await AllocationRepository
.Get()
.OrderBy(x => x.Id)
.Skip(page * pageSize)
.Take(pageSize)
.Skip(options.Page * options.PageSize)
.Take(options.PageSize)
.Where(x => x.Node.Id == nodeId)
.AsNoTracking()
.ProjectToAdminResponse()
.ToArrayAsync();
var mappedAllocations = allocations
.Select(AllocationMapper.ToNodeAllocation)
.ToArray();
return new PagedData<NodeAllocationResponse>()
{
Items = mappedAllocations,
CurrentPage = page,
PageSize = pageSize,
Items = allocations,
CurrentPage = options.Page,
PageSize = options.PageSize,
TotalItems = count,
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
};
}
[HttpGet("{id:int}")]
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
public async Task<NodeAllocationResponse> GetSingle([FromRoute] int nodeId, [FromRoute] int id)
public async Task<ActionResult<NodeAllocationResponse>> GetSingle([FromRoute] int nodeId, [FromRoute] int id)
{
var allocation = await AllocationRepository
.Get()
.Where(x => x.Node.Id == nodeId)
.AsNoTracking()
.ProjectToAdminResponse()
.FirstOrDefaultAsync(x => x.Id == id);
if (allocation == null)
throw new HttpApiException("No allocation with that id found", 404);
return Problem("No allocation with that id found", statusCode: 400);
return AllocationMapper.ToNodeAllocation(allocation);
return allocation;
}
[HttpPost("")]
[HttpPost]
[Authorize(Policy = "permissions:admin.servers.nodes.create")]
public async Task<NodeAllocationResponse> Create(
public async Task<ActionResult<NodeAllocationResponse>> Create(
[FromRoute] int nodeId,
[FromBody] CreateNodeAllocationRequest request
)
@@ -87,7 +88,7 @@ public class NodeAllocationsController : Controller
.FirstOrDefaultAsync(x => x.Id == nodeId);
if (node == null)
throw new HttpApiException("No node with that id found", 404);
return Problem("No node with that id found", statusCode: 404);
var allocation = AllocationMapper.ToAllocation(request);
@@ -97,7 +98,7 @@ public class NodeAllocationsController : Controller
}
[HttpPatch("{id:int}")]
public async Task<NodeAllocationResponse> Update(
public async Task<ActionResult<NodeAllocationResponse>> Update(
[FromRoute] int nodeId,
[FromRoute] int id,
[FromBody] UpdateNodeAllocationRequest request
@@ -109,7 +110,7 @@ public class NodeAllocationsController : Controller
.FirstOrDefaultAsync(x => x.Id == id);
if (allocation == null)
throw new HttpApiException("No allocation with that id found", 404);
return Problem("No allocation with that id found", statusCode: 404);
AllocationMapper.Merge(request, allocation);
await AllocationRepository.Update(allocation);
@@ -118,7 +119,7 @@ public class NodeAllocationsController : Controller
}
[HttpDelete("{id:int}")]
public async Task Delete([FromRoute] int nodeId, [FromRoute] int id)
public async Task<ActionResult> Delete([FromRoute] int nodeId, [FromRoute] int id)
{
var allocation = await AllocationRepository
.Get()
@@ -126,32 +127,44 @@ public class NodeAllocationsController : Controller
.FirstOrDefaultAsync(x => x.Id == id);
if (allocation == null)
throw new HttpApiException("No allocation with that id found", 404);
return Problem("No allocation with that id found", statusCode: 404);
await AllocationRepository.Remove(allocation);
return NoContent();
}
[HttpPost("range")]
public async Task CreateRange([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRangeRequest rangeRequest)
public async Task<ActionResult> CreateRange(
[FromRoute] int nodeId,
[FromBody] CreateNodeAllocationRangeRequest request
)
{
if (request.Start > request.End)
return Problem("Invalid start and end specified", statusCode: 400);
if (request.End - request.Start == 0)
return Problem("Empty range specified", statusCode: 400);
var node = await NodeRepository
.Get()
.FirstOrDefaultAsync(x => x.Id == nodeId);
if (node == null)
throw new HttpApiException("No node with that id found", 404);
return Problem("No node with that id found", statusCode: 404);
var existingAllocations = AllocationRepository
var existingAllocations = await AllocationRepository
.Get()
.Where(x => x.Node.Id == nodeId)
.ToArray();
.Where(x => x.Port >= request.Start && x.Port <= request.End &&
x.IpAddress == request.IpAddress)
.AsNoTracking()
.ToArrayAsync();
var ports = new List<int>();
for (var i = rangeRequest.Start; i < rangeRequest.End; i++)
for (var i = request.Start; i < request.End; i++)
{
// Skip existing allocations
if (existingAllocations.Any(x => x.Port == i && x.IpAddress == rangeRequest.IpAddress))
if (existingAllocations.Any(x => x.Port == i))
continue;
ports.Add(i);
@@ -160,17 +173,18 @@ public class NodeAllocationsController : Controller
var allocations = ports
.Select(port => new Allocation()
{
IpAddress = rangeRequest.IpAddress,
IpAddress = request.IpAddress,
Port = port,
Node = node
})
.ToArray();
await AllocationRepository.RunTransaction(async set => { await set.AddRangeAsync(allocations); });
return NoContent();
}
[HttpDelete("all")]
public async Task DeleteAll([FromRoute] int nodeId)
public async Task<ActionResult> DeleteAll([FromRoute] int nodeId)
{
var allocations = AllocationRepository
.Get()
@@ -178,14 +192,14 @@ public class NodeAllocationsController : Controller
.ToArray();
await AllocationRepository.RunTransaction(set => { set.RemoveRange(allocations); });
return NoContent();
}
[HttpGet("free")]
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
public async Task<IPagedData<NodeAllocationResponse>> GetFree(
[FromRoute] int nodeId,
[FromQuery] int page,
[FromQuery] [Range(1, 100)] int pageSize,
[FromQuery] PagedOptions options,
[FromQuery] int serverId = -1
)
{
@@ -202,19 +216,21 @@ public class NodeAllocationsController : Controller
.Where(x => x.Server == null || x.Server.Id == serverId);
var count = await freeAllocationsQuery.CountAsync();
var allocations = await freeAllocationsQuery.ToArrayAsync();
var mappedAllocations = allocations
.Select(AllocationMapper.ToNodeAllocation)
.ToArray();
var allocations = await freeAllocationsQuery
.Skip(options.Page * options.PageSize)
.Take(options.PageSize)
.AsNoTracking()
.ProjectToAdminResponse()
.ToArrayAsync();
return new PagedData<NodeAllocationResponse>()
{
Items = mappedAllocations,
CurrentPage = page,
PageSize = pageSize,
Items = allocations,
CurrentPage = options.Page,
PageSize = options.PageSize,
TotalItems = count,
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
};
}
}

View File

@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonCore.Extended.Abstractions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using MoonlightServers.ApiServer.Database.Entities;
using MoonlightServers.ApiServer.Services;
using MoonlightServers.Shared.Http.Responses.Admin.Nodes.Sys;
@@ -24,10 +25,13 @@ public class NodeStatusController : Controller
[HttpGet("{nodeId:int}/system/status")]
[Authorize(Policy = "permissions:admin.servers.nodes.status")]
public async Task<NodeSystemStatusResponse> GetStatus([FromRoute] int nodeId)
public async Task<ActionResult<NodeSystemStatusResponse>> GetStatus([FromRoute] int nodeId)
{
var node = GetNode(nodeId);
var node = await GetNode(nodeId);
if (node.Value == null)
return node.Result ?? Problem("Unable to retrieve node");
NodeSystemStatusResponse response;
var sw = new Stopwatch();
@@ -35,7 +39,7 @@ public class NodeStatusController : Controller
try
{
var statusResponse = await NodeService.GetSystemStatus(node);
var statusResponse = await NodeService.GetSystemStatus(node.Value);
sw.Stop();
@@ -65,14 +69,14 @@ public class NodeStatusController : Controller
return response;
}
private Node GetNode(int nodeId)
private async Task<ActionResult<Node>> GetNode(int nodeId)
{
var result = NodeRepository
var result = await NodeRepository
.Get()
.FirstOrDefault(x => x.Id == nodeId);
.FirstOrDefaultAsync(x => x.Id == nodeId);
if (result == null)
throw new HttpApiException("A node with this id could not be found", 404);
return Problem("A node with this id could not be found", statusCode: 404);
return result;
}

View File

@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using MoonCore.Extended.Abstractions;
using Microsoft.AspNetCore.Authorization;
using MoonCore.Exceptions;
using MoonCore.Extended.Models;
using MoonCore.Helpers;
using MoonCore.Models;
using MoonlightServers.ApiServer.Database.Entities;
@@ -19,62 +20,55 @@ public class NodesController : Controller
{
private readonly DatabaseRepository<Node> NodeRepository;
public NodesController(
DatabaseRepository<Node> nodeRepository
)
public NodesController(DatabaseRepository<Node> nodeRepository)
{
NodeRepository = nodeRepository;
}
[HttpGet]
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
public async Task<IPagedData<NodeResponse>> Get(
[FromQuery] [Range(0, int.MaxValue)] int page,
[FromQuery] [Range(1, 100)] int pageSize
)
public async Task<IPagedData<NodeResponse>> Get([FromQuery] PagedOptions options)
{
var query = NodeRepository
.Get();
var count = await NodeRepository.Get().CountAsync();
var count = await query.CountAsync();
var items = await query
var items = await NodeRepository
.Get()
.OrderBy(x => x.Id)
.Skip(page * pageSize)
.Take(pageSize)
.Skip(options.Page * options.PageSize)
.Take(options.PageSize)
.AsNoTracking()
.ProjectToAdminResponse()
.ToArrayAsync();
var mappedItems = items
.Select(NodeMapper.ToAdminNodeResponse)
.ToArray();
return new PagedData<NodeResponse>()
{
Items = mappedItems,
CurrentPage = page,
PageSize = pageSize,
Items = items,
CurrentPage = options.Page,
PageSize = options.PageSize,
TotalItems = count,
TotalPages = count == 0 ? 0 : count / pageSize
TotalPages = (int)Math.Ceiling(Math.Max(0, count) / (double)options.PageSize)
};
}
[HttpGet("{id:int}")]
[Authorize(Policy = "permissions:admin.servers.nodes.get")]
public async Task<NodeResponse> GetSingle([FromRoute] int id)
public async Task<ActionResult<NodeResponse>> GetSingle([FromRoute] int id)
{
var node = await NodeRepository
.Get()
.AsNoTracking()
.ProjectToAdminResponse()
.FirstOrDefaultAsync(x => x.Id == id);
if (node == null)
throw new HttpApiException("No node with this id found", 404);
return NodeMapper.ToAdminNodeResponse(node);
return Problem("No node with this id found", statusCode: 404);
return node;
}
[HttpPost]
[Authorize(Policy = "permissions:admin.servers.nodes.create")]
public async Task<NodeResponse> Create([FromBody] CreateNodeRequest request)
public async Task<ActionResult<NodeResponse>> Create([FromBody] CreateNodeRequest request)
{
var node = NodeMapper.ToNode(request);
@@ -88,14 +82,14 @@ public class NodesController : Controller
[HttpPatch("{id:int}")]
[Authorize(Policy = "permissions:admin.servers.nodes.update")]
public async Task<NodeResponse> Update([FromRoute] int id, [FromBody] UpdateNodeRequest request)
public async Task<ActionResult<NodeResponse>> Update([FromRoute] int id, [FromBody] UpdateNodeRequest request)
{
var node = await NodeRepository
.Get()
.FirstOrDefaultAsync(x => x.Id == id);
if (node == null)
throw new HttpApiException("No node with this id found", 404);
return Problem("No node with this id found", statusCode: 404);
NodeMapper.Merge(request, node);
await NodeRepository.Update(node);
@@ -105,15 +99,16 @@ public class NodesController : Controller
[HttpDelete("{id:int}")]
[Authorize(Policy = "permissions:admin.servers.nodes.delete")]
public async Task Delete([FromRoute] int id)
public async Task<ActionResult> Delete([FromRoute] int id)
{
var node = await NodeRepository
.Get()
.FirstOrDefaultAsync(x => x.Id == id);
if (node == null)
throw new HttpApiException("No node with this id found", 404);
return Problem("No node with this id found", statusCode: 404);
await NodeRepository.Remove(node);
return Ok();
}
}

View File

@@ -27,12 +27,16 @@ public class StatisticsController : Controller
[HttpGet]
[SuppressMessage("ReSharper.DPA", "DPA0011: High execution time of MVC action", MessageId = "time: 1142ms",
Justification = "The daemon has an artificial delay of one second to calculate accurate cpu usage values")]
public async Task<StatisticsResponse> Get([FromRoute] int nodeId)
public async Task<ActionResult<StatisticsResponse>> Get([FromRoute] int nodeId)
{
var node = await GetNode(nodeId);
var statistics = await NodeService.GetStatistics(node);
return new()
if (node.Value == null)
return node.Result ?? Problem("Unable to retrieve node");
var statistics = await NodeService.GetStatistics(node.Value);
return new StatisticsResponse()
{
Cpu = new()
{
@@ -62,12 +66,16 @@ public class StatisticsController : Controller
}
[HttpGet("docker")]
public async Task<DockerStatisticsResponse> GetDocker([FromRoute] int nodeId)
public async Task<ActionResult<DockerStatisticsResponse>> GetDocker([FromRoute] int nodeId)
{
var node = await GetNode(nodeId);
var statistics = await NodeService.GetDockerStatistics(node);
return new()
if (node.Value == null)
return node.Result ?? Problem("Unable to retrieve node");
var statistics = await NodeService.GetDockerStatistics(node.Value);
return new DockerStatisticsResponse()
{
BuildCacheReclaimable = statistics.BuildCacheReclaimable,
BuildCacheUsed = statistics.BuildCacheUsed,
@@ -79,14 +87,14 @@ public class StatisticsController : Controller
};
}
private async Task<Node> GetNode(int nodeId)
private async Task<ActionResult<Node>> GetNode(int nodeId)
{
var result = await NodeRepository
.Get()
.FirstOrDefaultAsync(x => x.Id == nodeId);
if (result == null)
throw new HttpApiException("A node with this id could not be found", 404);
return Problem("A node with this id could not be found", statusCode: 404);
return result;
}