Files

57 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2026-10-05 18:10:18 -04:00
<?php
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Admin\Nodes;
2026-10-11 02:54:15 -04:00
use Illuminate\Database\Eloquent\Collection;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Response;
2026-10-11 02:54:15 -04:00
use Illuminate\Support\Facades\DB;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
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;
2026-10-11 02:54:15 -04:00
use Pterodactyl\Models\Allocation;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Node;
#[Group('Admin API', 'Root administrator endpoints for managing panel configuration and resources.')]
#[Subgroup('Node Allocations', 'Create, update, retrieve, and delete node allocations.')]
class BulkDeleteAllocationsController extends AdminApiController
{
/**
* Delete selected unassigned allocations.
*/
#[Endpoint('Bulk delete node allocations', 'Deletes multiple unassigned allocations from a node by allocation ID.')]
#[ScribeResponse(status: 204, description: 'Allocations deleted.')]
#[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
{
2026-10-11 02:54:15 -04:00
// 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();
2026-10-05 18:10:18 -04:00
2026-10-11 02:54:15 -04:00
$allocations->each(fn (Allocation $allocation) => $deletions->delete($allocation));
return $allocations;
});
2026-10-05 18:10:18 -04:00
2026-10-11 02:54:15 -04:00
foreach ($allocations as $allocation) {
2026-10-05 18:10:18 -04:00
Activity::event('admin:node-allocation.delete')
->subject($allocation)
->property('address', $allocation->ip)
->property('port', $allocation->port)
->log();
}
return $this->returnNoContent();
}
}