2021-01-10 13:08:43 -08:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2021-01-10 13:08:43 -08:00
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 ;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer ;
use Pterodactyl\Facades\Fractal ;
2021-01-10 13:08:43 -08:00
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController ;
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetDeployableNodesRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Node ;
use Pterodactyl\Services\Deployment\FindViableNodesService ;
use Pterodactyl\Transformers\Api\Application\NodeTransformer ;
2021-01-10 13:08:43 -08:00
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.' )]
2021-01-10 13:08:43 -08:00
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.' ,
],
],
];
2021-01-10 13:08:43 -08:00
/**
* 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
*
* @return ApiPayload
*
* @throws NoViableNodeException
2021-01-10 13:08:43 -08:00
*/
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 ])]
#[ 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 )]
#[ ResponseFromTransformer ( NodeTransformer :: class , Node :: class , description : 'Deployable nodes returned.' , collection : true , factoryStates : [ 'withLocation' ], resourceKey : 'node' )]
#[ ScribeResponse ( self :: NO_VIABLE_NODE_ERROR , status : 400 , description : 'No nodes can satisfy the requested deployment constraints.' )]
public function __invoke ( GetDeployableNodesRequest $request , FindViableNodesService $viableNodesService ) : array
2021-01-10 13:08:43 -08:00
{
2026-10-05 18:10:18 -04:00
$payload = $request -> payload ();
$nodes = $viableNodesService
-> setLocations ( $payload [ 'location_ids' ])
-> setMemory ( $payload [ 'memory' ])
-> setDisk ( $payload [ 'disk' ])
-> handle ( $payload [ 'per_page' ], $payload [ 'page' ]);
2021-01-10 13:08:43 -08:00
2026-10-05 18:10:18 -04:00
return Fractal :: collection ( $nodes )
2021-01-10 13:08:43 -08:00
-> transformWith ( $this -> getTransformer ( NodeTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2021-01-10 13:08:43 -08:00
}
}