Files
pterodactyl-panel/app/Http/Controllers/Api/Application/Nodes/NodeController.php
T

158 lines
7.7 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-01-19 19:58:57 -06:00
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
use Illuminate\Http\JsonResponse;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\QueryParam;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use Pterodactyl\Contracts\Nodes\CreatesNodes;
use Pterodactyl\Contracts\Nodes\DeletesNodes;
use Pterodactyl\Contracts\Nodes\UpdatesNodes;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
use Pterodactyl\Facades\Fractal;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Nodes\DeleteNodeRequest;
2018-01-19 19:58:57 -06:00
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest;
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest;
use Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest;
use Pterodactyl\Http\Requests\Api\Application\Nodes\UpdateNodeRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Node;
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
use Spatie\QueryBuilder\QueryBuilder;
use Throwable;
2026-10-05 18:10:18 -04:00
#[Group('Application API', 'Root administrator endpoints for managing panel resources using application API tokens.')]
#[Subgroup('Nodes', 'Create, update, retrieve, and delete Wings nodes and their allocations.')]
2018-01-27 12:38:56 -06:00
class NodeController extends ApplicationApiController
{
2026-10-05 18:10:18 -04:00
private const array ACTIVE_SERVERS_ERROR = [
'errors' => [
[
'code' => 'HasActiveServersException',
'status' => '400',
'detail' => 'A node must have no servers linked to it in order to be deleted.',
],
],
];
private const array CONFIGURATION_NOT_PERSISTED_ERROR = [
'errors' => [
[
'code' => 'ConfigurationNotPersistedException',
'status' => '400',
'detail' => 'The daemon configuration has been updated, however there was an error encountered while attempting to automatically update the configuration file on the Daemon. You will need to manually update the configuration file (config.yml) for the daemon to apply these changes.',
],
],
];
/**
2022-10-14 10:59:20 -06:00
* Return all the nodes currently available on the Panel.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('List nodes', 'Returns a paginated list of Wings nodes registered on the panel.')]
#[QueryParam('per_page', 'integer', 'Number of nodes to return per page, up to 100.', required: false, example: 50)]
#[QueryParam('filter[uuid]', 'string', 'Filter nodes by UUID.', required: false, example: '3d8dd7f9-07a1-4d65-8db0-8c8921f2f4f7')]
#[QueryParam('filter[name]', 'string', 'Filter nodes by name.', required: false, example: 'Node 1')]
#[QueryParam('filter[fqdn]', 'string', 'Filter nodes by fully qualified domain name.', required: false, example: 'node.example.com')]
#[QueryParam('filter[daemon_token_id]', 'string', 'Filter nodes by daemon token identifier.', required: false, example: 'abcdefghijklmnop')]
#[QueryParam('sort', 'string', 'Sort nodes by id, uuid, memory, or disk. Prefix with "-" for descending order.', required: false, example: '-id', enum: ['id', '-id', 'uuid', '-uuid', 'memory', '-memory', 'disk', '-disk'])]
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "allocations", "location", and "servers" when the key can read those resources.', required: false, example: 'location,allocations', enum: ['allocations', 'location', 'servers', 'location,allocations'])]
#[ResponseFromTransformer(NodeTransformer::class, Node::class, collection: true, factoryStates: ['withLocation'], resourceKey: 'node', paginate: [IlluminatePaginatorAdapter::class, 50])]
2018-01-13 14:08:19 -06:00
public function index(GetNodesRequest $request): array
{
2026-10-05 18:10:18 -04:00
$nodes = QueryBuilder::for(Node::query()
->withSum('servers as sum_memory', 'memory')
->withSum('servers as sum_disk', 'disk'))
->allowedFilters(['uuid', 'name', 'fqdn', 'daemon_token_id'])
->allowedSorts(['id', 'uuid', 'memory', 'disk'])
2026-10-05 18:10:18 -04:00
->paginate($request->perPage());
2026-10-05 18:10:18 -04:00
return Fractal::collection($nodes)
2018-01-27 12:38:56 -06:00
->transformWith($this->getTransformer(NodeTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
}
/**
* Return data for a single instance of a node.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Get node', 'Returns a single Wings node by internal numeric ID.')]
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "allocations", "location", and "servers" when the key can read those resources.', required: false, example: 'location,allocations', enum: ['allocations', 'location', 'servers', 'location,allocations'])]
#[ResponseFromTransformer(NodeTransformer::class, Node::class, factoryStates: ['withLocation'], resourceKey: 'node')]
public function view(GetNodeRequest $request, Node $node): array
{
2026-10-05 18:10:18 -04:00
return Fractal::item($node)
2018-01-27 12:38:56 -06:00
->transformWith($this->getTransformer(NodeTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
}
/**
2022-10-14 10:59:20 -06:00
* Create a new node on the Panel. Returns the created node and an HTTP/201
* status response on success.
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Create node', 'Creates a new Wings node and returns the created resource.')]
#[ResponseFromTransformer(NodeTransformer::class, Node::class, status: 201, description: 'Node created.', factoryStates: ['withLocation'], resourceKey: 'node', meta: ['resource' => 'https://panel.example.test/api/application/nodes/1'])]
public function store(StoreNodeRequest $request, CreatesNodes $nodes): JsonResponse
{
2026-10-05 18:10:18 -04:00
$node = $nodes->create($request->payload());
2026-10-05 18:10:18 -04:00
return Fractal::item($node)
2018-01-27 12:38:56 -06:00
->transformWith($this->getTransformer(NodeTransformer::class))
->addMeta([
2018-01-27 12:38:56 -06:00
'resource' => route('api.application.nodes.view', [
'node' => $node->id,
]),
])
->respond(201);
}
/**
* Update an existing node on the Panel.
*
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*
* @throws Throwable
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Update node', 'Updates an existing Wings node by internal numeric ID.')]
#[ResponseFromTransformer(NodeTransformer::class, Node::class, factoryStates: ['withLocation'], resourceKey: 'node')]
#[ScribeResponse(self::CONFIGURATION_NOT_PERSISTED_ERROR, status: 400, description: 'The panel saved the node changes but could not persist the configuration to Wings.')]
public function update(UpdateNodeRequest $request, UpdatesNodes $nodes, Node $node): array
{
2026-10-05 18:10:18 -04:00
$node = $nodes->update(
2021-01-23 12:33:34 -08:00
$node,
2026-10-05 18:10:18 -04:00
$request->payload(),
$request->boolean('reset_secret')
2018-01-27 12:38:56 -06:00
);
2026-10-05 18:10:18 -04:00
return Fractal::item($node)
2018-01-27 12:38:56 -06:00
->transformWith($this->getTransformer(NodeTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
}
/**
* Deletes a given node from the Panel as long as there are no servers
* currently attached to it.
*
2026-10-05 18:10:18 -04:00
* @throws HasActiveServersException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Delete node', 'Deletes a Wings node by internal numeric ID when no servers are attached.')]
#[ScribeResponse(status: 204, description: 'Node deleted.')]
#[ScribeResponse(self::ACTIVE_SERVERS_ERROR, status: 400, description: 'The node has active servers attached.')]
public function delete(DeleteNodeRequest $request, DeletesNodes $nodes, Node $node): JsonResponse
{
2026-10-05 18:10:18 -04:00
$nodes->delete($node);
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}