Files

74 lines
4.2 KiB
PHP
Raw Permalink Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
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 Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException;
2026-10-11 21:59:15 -04:00
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromResource;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetDeployableNodesRequest;
2026-10-11 21:59:15 -04:00
use Pterodactyl\Http\Resources\Api\ApiResourceCollection;
use Pterodactyl\Http\Resources\Api\Application\NodeResource;
2026-10-12 02:24:41 -04:00
use Pterodactyl\Models\Egg;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Node;
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.')]
class NodeDeploymentController extends ApplicationApiController
{
2026-10-05 18:10:18 -04:00
private const array NO_VIABLE_NODE_ERROR = [
'errors' => [
[
'code' => 'NoViableNodeException',
'status' => '400',
'detail' => 'No nodes satisfying the requirements specified for automatic deployment could be found.',
],
],
];
/**
* Finds any nodes that are available using the given deployment criteria. This works
* similarly to the server creation process, but allows you to pass the deployment object
* to this endpoint and get back a list of all Nodes satisfying the requirements.
*
2026-10-05 18:10:18 -04:00
* @throws NoViableNodeException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('List deployable nodes', 'Returns nodes that can satisfy the requested memory, disk, and optional location deployment constraints.')]
#[QueryParam('memory', 'integer', 'Memory required for the deployment, in megabytes.', required: true, example: 1024)]
#[QueryParam('disk', 'integer', 'Disk required for the deployment, in megabytes.', required: true, example: 10240)]
#[QueryParam('location_ids', 'integer[]', 'Restrict results to these location IDs.', required: false, example: [1, 2])]
2026-10-12 02:24:41 -04:00
#[QueryParam('tags', 'string[]', "Deploy tags (IDs or slugs) the deployment declares. They must match a node's deployment tags exactly: a node reserved for a tag missing here is left out, and every tag listed must be reserved on the node. Without tags, only nodes with no reservations are returned.", required: false, example: ['premium'])]
#[QueryParam('egg', 'integer', "Only return nodes that accept this egg. A node's egg tags are OR-matched against the egg's tags: a node with none accepts every egg, otherwise it needs one tag in common with the egg.", required: false, example: 1)]
2026-10-05 18:10:18 -04:00
#[QueryParam('page', 'integer', 'Page number to return. When omitted, all viable nodes are returned without pagination metadata.', required: false, example: 1)]
#[QueryParam('per_page', 'integer', 'Number of nodes to return per page when page is supplied, up to 100.', required: false, example: 50)]
2026-10-11 21:59:15 -04:00
#[ResponseFromResource(NodeResource::class, Node::class, description: 'Deployable nodes returned.', collection: true, factoryStates: ['withLocation'], resourceKey: 'node')]
2026-10-05 18:10:18 -04:00
#[ScribeResponse(self::NO_VIABLE_NODE_ERROR, status: 400, description: 'No nodes can satisfy the requested deployment constraints.')]
2026-10-12 00:09:41 -04:00
public function __invoke(GetDeployableNodesRequest $request): ApiResourceCollection
{
2026-10-05 18:10:18 -04:00
$payload = $request->payload();
2026-10-12 00:09:41 -04:00
$query = Node::query()
->viableFor($payload['memory'], $payload['disk'])
->inLocations($payload['location_ids'])
2026-10-12 02:24:41 -04:00
->forDeployTags($payload['tags']);
if ($payload['egg'] !== null) {
$query->forEggTags(Egg::query()->findOrFail($payload['egg'])->tagSlugs());
}
2026-10-12 00:09:41 -04:00
$nodes = $payload['page'] === null
? $query->get()
: $query->paginate($payload['per_page'] ?? 50, ['*'], 'page', $payload['page']);
throw_if($nodes->isEmpty(), NoViableNodeException::class, trans('exceptions.deployment.no_viable_nodes'));
2026-10-11 21:59:15 -04:00
return NodeResource::collection($nodes);
}
}