Added multi allocation actions in node update page

This commit is contained in:
2024-12-14 18:38:12 +01:00
parent 680827e0ea
commit 747712c5c4
4 changed files with 167 additions and 10 deletions

View File

@@ -90,4 +90,57 @@ public class NodeAllocationsController : Controller
await CrudHelper.Delete(id); await CrudHelper.Delete(id);
} }
[HttpPost("{nodeId:int}/allocations/range")]
public Task CreateRange([FromRoute] int nodeId, [FromBody] CreateNodeAllocationRangeRequest rangeRequest)
{
ApplyNode(nodeId);
var existingAllocations = AllocationRepository
.Get()
.Where(x => x.Node.Id == nodeId)
.ToArray();
var ports = new List<int>();
for (var i = rangeRequest.Start; i < rangeRequest.End; i++)
{
// Skip existing allocations
if(existingAllocations.Any(x => x.Port == i && x.IpAddress == rangeRequest.IpAddress))
continue;
ports.Add(i);
}
var allocations = ports
.Select(port => new Allocation()
{
IpAddress = rangeRequest.IpAddress,
Port = port,
Node = Node
})
.ToArray();
// TODO: Add AddRange in database repository
foreach (var allocation in allocations)
AllocationRepository.Add(allocation);
return Task.CompletedTask;
}
[HttpDelete("{nodeId:int}/allocations/all")]
public Task DeleteAll([FromRoute] int nodeId)
{
ApplyNode(nodeId);
var allocations = AllocationRepository
.Get()
.Where(x => x.Node.Id == nodeId)
.ToArray();
foreach (var allocation in allocations)
AllocationRepository.Delete(allocation);
return Task.CompletedTask;
}
} }

View File

@@ -0,0 +1,55 @@
@using MoonCore.Blazor.Tailwind.Modals.Components
@using MoonCore.Blazor.Tailwind.Components
@using MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations
@inherits BaseModal
<h1 class="mb-5 font-semibold text-xl">Add a range of new allocations</h1>
<HandleForm @ref="HandleForm" Model="Form" OnValidSubmit="OnValidSubmit">
<div class="grid grid-cols-1 gap-2">
<div class="col-span-1">
<label class="block text-sm font-medium leading-6 text-white">IP Address</label>
<input @bind="Form.IpAddress" type="text" class="form-input w-full"/>
</div>
<div class="col-span-1">
<label class="block text-sm font-medium leading-6 text-white">Start Port</label>
<input @bind="Form.Start" type="text" class="form-input w-full"/>
</div>
<div class="col-span-1">
<label class="block text-sm font-medium leading-6 text-white">End Port</label>
<input @bind="Form.End" type="text" class="form-input w-full"/>
</div>
</div>
</HandleForm>
<div class="mt-5 flex space-x-2">
<WButton OnClick="_ => Hide()" CssClasses="btn btn-secondary grow">Cancel</WButton>
<WButton OnClick="_ => Submit()" CssClasses="btn btn-primary grow">Create</WButton>
</div>
@code
{
[Parameter] public Func<CreateNodeAllocationRangeRequest, Task> OnSubmit { get; set; }
private CreateNodeAllocationRangeRequest Form;
private HandleForm HandleForm;
protected override void OnInitialized()
{
Form = new()
{
IpAddress = "0.0.0.0"
};
}
private async Task OnValidSubmit()
{
await OnSubmit.Invoke(Form);
await Hide();
}
private Task Submit() => HandleForm.Submit();
}

View File

@@ -17,14 +17,15 @@
<div class="grid grid-cols-1 md:grid-cols-3 md:gap-x-5"> <div class="grid grid-cols-1 md:grid-cols-3 md:gap-x-5">
<div class="col-span-1"> <div class="col-span-1">
<div class="card"> <div class="card">
<div class="card-header card-header bg-gray-700 rounded-t-lg text-gray-500 bg-opacity-50 border-0 py-2 text-base font-semibold"> <div
class="card-header card-header bg-gray-700 rounded-t-lg text-gray-500 bg-opacity-50 border-0 py-2 text-base font-semibold">
<span class="card-title">Actions</span> <span class="card-title">Actions</span>
</div> </div>
<div class="card-body"> <div class="card-body">
<div class="flex flex-col gap-y-3"> <div class="flex flex-col gap-y-3">
<button @onclick="AddAllocation" class="btn btn-primary">Create allocation</button> <button @onclick="AddAllocation" class="btn btn-primary">Create allocation</button>
<button class="btn btn-tertiary">Add multiple</button> <button @onclick="AddAllocationRange" class="btn btn-tertiary">Add multiple</button>
<button class="btn btn-danger">Delete all</button> <button @onclick="DeleteAllAllocations" class="btn btn-danger">Delete all</button>
</div> </div>
</div> </div>
</div> </div>
@@ -40,10 +41,12 @@
<DataColumn TItem="NodeAllocationDetailResponse" Title=""> <DataColumn TItem="NodeAllocationDetailResponse" Title="">
<Template> <Template>
<div class="flex justify-end items-center"> <div class="flex justify-end items-center">
<a @onclick="() => UpdateAllocation(context)" @onclick:preventDefault href="#" class="text-primary-500 mr-2 sm:mr-3"> <a @onclick="() => UpdateAllocation(context)" @onclick:preventDefault href="#"
class="text-primary-500 mr-2 sm:mr-3">
<i class="icon-pencil text-base"></i> <i class="icon-pencil text-base"></i>
</a> </a>
<a @onclick="() => DeleteAllocation(context)" @onclick:preventDefault href="#" class="text-danger-500"> <a @onclick="() => DeleteAllocation(context)" @onclick:preventDefault href="#"
class="text-danger-500">
<i class="icon-trash text-base"></i> <i class="icon-trash text-base"></i>
</a> </a>
</div> </div>
@@ -67,6 +70,22 @@
); );
} }
private async Task AddAllocationRange()
{
Func<CreateNodeAllocationRangeRequest, Task> onSubmit = async request =>
{
await ApiClient.Post($"api/admin/servers/nodes/{Node.Id}/allocations/range", request);
await ToastService.Success("Successfully created allocations");
await Table.Refresh(isSilent: false, bypassCache: true);
};
await ModalService.Launch<CreateMultipleAllocationModal>(parameters =>
{
parameters.Add("OnSubmit", onSubmit);
});
}
private async Task AddAllocation() private async Task AddAllocation()
{ {
Func<CreateNodeAllocationRequest, Task> onSubmit = async request => Func<CreateNodeAllocationRequest, Task> onSubmit = async request =>
@@ -77,10 +96,7 @@
await Table.Refresh(isSilent: false, bypassCache: true); await Table.Refresh(isSilent: false, bypassCache: true);
}; };
await ModalService.Launch<CreateAllocationModal>(parameters => await ModalService.Launch<CreateAllocationModal>(parameters => { parameters.Add("OnSubmit", onSubmit); });
{
parameters.Add("OnSubmit", onSubmit);
});
} }
private async Task UpdateAllocation(NodeAllocationDetailResponse allocation) private async Task UpdateAllocation(NodeAllocationDetailResponse allocation)
@@ -114,4 +130,19 @@
} }
); );
} }
private async Task DeleteAllAllocations()
{
await AlertService.ConfirmDanger(
"Delete all allocations",
"Do you really want to delete all allocations? This cannot be undone",
async () =>
{
await ApiClient.Delete($"api/admin/servers/nodes/{Node.Id}/allocations/all");
await ToastService.Success("Successfully deleted allocations");
await Table.Refresh(isSilent: false, bypassCache: true);
}
);
}
} }

View File

@@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace MoonlightServers.Shared.Http.Requests.Admin.NodeAllocations;
public class CreateNodeAllocationRangeRequest
{
[Required(ErrorMessage = "You need to provide an ip address")]
[RegularExpression(@"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$", ErrorMessage = "You need to provide a valid ip address")]
public string IpAddress { get; set; }
[Required(ErrorMessage = "You need to provide a start port")]
[Range(1, 65535, ErrorMessage = "You need to provide a valid start port")]
public int Start { get; set; }
[Required(ErrorMessage = "You need to provide a end port")]
[Range(1, 65535, ErrorMessage = "You need to provide a valid end port")]
public int End { get; set; }
}