bulk allocation delete

This commit is contained in:
robert dennis
2026-10-12 05:18:55 -04:00
parent d5b3f4fdce
commit b5430a32d3
2 changed files with 27 additions and 5 deletions
@@ -4,7 +4,9 @@ declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Admin\Nodes;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
@@ -13,6 +15,7 @@ use Pterodactyl\Contracts\Allocations\DeletesAllocations;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController;
use Pterodactyl\Http\Requests\Api\Admin\Nodes\Allocations\BulkDeleteAllocationRequest;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Node;
#[Group('Admin API', 'Root administrator endpoints for managing panel configuration and resources.')]
@@ -27,13 +30,20 @@ class BulkDeleteAllocationsController extends AdminApiController
#[ScribeResponse(AllocationController::SERVER_USING_ALLOCATION_ERROR, status: 400, description: 'One of the allocations is currently assigned to a server.')]
public function __invoke(BulkDeleteAllocationRequest $request, DeletesAllocations $deletions, Node $node): Response
{
$allocations = $node->allocations()
->whereIn('id', $request->validated('ids', []))
->get();
// All or nothing: one allocation a server holds rejects the whole selection, rather
// than leaving the ones before it deleted.
$allocations = DB::transaction(function () use ($request, $deletions, $node): Collection {
$allocations = $node->allocations()
->whereIn('id', $request->validated('ids', []))
->lockForUpdate()
->get();
$allocations->each(fn (Allocation $allocation) => $deletions->delete($allocation));
return $allocations;
});
foreach ($allocations as $allocation) {
$deletions->delete($allocation);
Activity::event('admin:node-allocation.delete')
->subject($allocation)
->property('address', $allocation->ip)
@@ -130,6 +130,18 @@ test('bulk delete allocations', function (): void {
$this->assertDatabaseHas('allocations', ['id' => $keep->id]);
});
test('bulk delete removes nothing when one allocation is assigned', function (): void {
$free = Allocation::factory()->times(2)->create(['node_id' => $this->node->id]);
$assigned = $this->createServerModel(['node_id' => $this->node->id])->allocation;
$this->deleteJson(route('api.admin.nodes.allocations.bulk-delete', ['node' => $this->node->id]), ['ids' => [$free[0]->id, $assigned->id, $free[1]->id]])
->assertBadRequest()
->assertJsonPath('errors.0.code', 'ServerUsingAllocationException');
foreach ([...$free, $assigned] as $allocation) {
$this->assertDatabaseHas('allocations', ['id' => $allocation->id]);
}
});
test('ip block delete allocations', function (): void {
$block = Allocation::factory()->times(2)->create(['node_id' => $this->node->id, 'ip' => '10.0.0.5']);
$other = Allocation::factory()->create(['node_id' => $this->node->id, 'ip' => '10.0.0.6']);