Files
Moonlight/Moonlight/Features/Servers/UI/Views/Admin/Nodes/Index.razor
2024-04-23 14:03:35 +02:00

109 lines
3.3 KiB
Plaintext

@page "/admin/servers/nodes"
@using Moonlight.Features.Servers.Models.Forms.Admin.Nodes
@using Moonlight.Features.Servers.UI.Components
@using Moonlight.Features.Servers.Entities
@using MoonCore.Abstractions
@using BlazorTable
@using Microsoft.EntityFrameworkCore
@using MoonCore.Exceptions
@using MoonCore.Helpers
@using System.Text.RegularExpressions;
@inject Repository<Server> ServerRepository
@inject Repository<ServerNode> NodeRepository
@attribute [RequirePermission(5001)]
<AdminServersNavigation Index="1"/>
<AutoCrud TItem="ServerNode"
TCreateForm="CreateNodeForm"
TUpdateForm="UpdateNodeForm"
Title=""
Load="Load"
ValidateAdd="ValidateAdd"
ValidateUpdate="ValidateUpdate"
ValidateDelete="ValidateDelete">
<Actions>
<a href="/admin/servers/nodes/view/@(context.Id)" class="btn btn-icon btn-info">
<i class="bx bx-sm bx-wrench"></i>
</a>
</Actions>
<View>
<CrudColumn TItem="ServerNode" Field="@(x => x.Id)" Title="Id"/>
<CrudColumn TItem="ServerNode" Field="@(x => x.Name)" Title="Name"/>
<CrudColumn TItem="ServerNode" Field="@(x => x.Fqdn)" Title="Fqdn"/>
<CrudColumn TItem="ServerNode" Field="@(x => x.Id)" Title="Status">
<Template>
<span class="text-muted">N/A</span>
</Template>
</CrudColumn>
</View>
<NoItemsView>
<IconAlert Title="No nodes found" Color="primary" Icon="bx-search-alt">
Add a new node in order to get started. Need help? Check out our <a href="https://docs.moonlightpanel.xyz">documentation</a>
</IconAlert>
</NoItemsView>
</AutoCrud>
@code
{
private ServerNode[] Load(Repository<ServerNode> repository)
{
return repository
.Get()
.ToArray();
}
private Task ValidateDelete(ServerNode node)
{
if (ServerRepository
.Get()
.Any(x => x.Node.Id == node.Id))
{
throw new DisplayException("There are still servers on this node. Delete the servers in order to delete the node");
}
if (NodeRepository
.Get()
.Include(x => x.Allocations)
.First(x => x.Id == node.Id)
.Allocations
.Any())
{
throw new DisplayException("There are still allocations on this node. Delete the allocations in order to delete the node");
}
return Task.CompletedTask;
}
private Task ValidateAdd(ServerNode node)
{
if (!IsDomainOrIp(node.Fqdn))
throw new DisplayException("The fqdn needs to be a valid domain or an ip address");
node.Token = Formatter.GenerateString(32);
return Task.CompletedTask;
}
private Task ValidateUpdate(ServerNode node)
{
if (!IsDomainOrIp(node.Fqdn))
throw new DisplayException("The fqdn needs to be a valid domain or an ip address");
return Task.CompletedTask;
}
private bool IsDomainOrIp(string input)
{
if (Regex.IsMatch(input, "^(?!-)(?:[a-zA-Z\\d-]{0,62}[a-zA-Z\\d]\\.)+(?:[a-zA-Z]{2,})$"))
return true;
if (Regex.IsMatch(input, "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"))
return true;
return false;
}
}