2018-01-19 19:58:57 -06:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2018-01-19 21:47:06 -06:00
namespace Pterodactyl\Http\Controllers\Api\Application\Servers ;
2018-01-19 19:58:57 -06:00
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Builder ;
2018-01-28 17:14:14 -06:00
use Illuminate\Http\JsonResponse ;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Response ;
use Illuminate\Validation\ValidationException ;
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\Servers\CreatesServers ;
use Pterodactyl\Contracts\Servers\DeletesServers ;
use Pterodactyl\Exceptions\DisplayException ;
use Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException ;
use Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException ;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer ;
use Pterodactyl\Facades\Fractal ;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController ;
2018-02-24 12:15:21 -06:00
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest ;
2018-01-19 21:47:06 -06:00
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest ;
2018-01-20 13:48:02 -06:00
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest ;
2018-01-28 17:14:14 -06:00
use Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server ;
use Pterodactyl\Transformers\Api\Application\ServerTransformer ;
use Spatie\QueryBuilder\QueryBuilder ;
use Throwable ;
2018-01-19 19:58:57 -06:00
2026-10-05 18:10:18 -04:00
#[ Group ( 'Application API' , 'Root administrator endpoints for managing panel resources using application API tokens.' )]
#[ Subgroup ( 'Servers' , 'Create, update, retrieve, manage, and delete servers.' )]
2018-01-19 21:47:06 -06:00
class ServerController extends ApplicationApiController
2018-01-19 19:58:57 -06:00
{
2026-10-05 18:10:18 -04:00
private const array DEPLOYMENT_ERROR = [
'errors' => [
[
'code' => 'NoViableNodeException' ,
'status' => '400' ,
'detail' => 'No nodes satisfying the requirements specified for automatic deployment could be found.' ,
],
],
];
2018-01-19 19:58:57 -06:00
2018-01-19 21:47:06 -06:00
/**
2022-10-14 10:59:20 -06:00
* Return all the servers that currently exist on the Panel.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2018-01-19 21:47:06 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'List servers' , 'Returns a paginated list of servers visible to the application API key.' )]
#[ QueryParam ( 'per_page' , 'integer' , 'Number of servers to return per page, up to 100.' , required : false , example : 50 )]
#[ QueryParam ( 'filter[uuid]' , 'string' , 'Filter servers by UUID.' , required : false , example : '4fcb1f44-0f90-4a1a-a8bf-7cc8a14d0f26' )]
#[ QueryParam ( 'filter[uuidShort]' , 'string' , 'Filter servers by short UUID identifier.' , required : false , example : '4fcb1f44' )]
#[ QueryParam ( 'filter[name]' , 'string' , 'Filter servers by name.' , required : false , example : 'Minecraft Server' )]
#[ QueryParam ( 'filter[description]' , 'string' , 'Filter servers by description.' , required : false , example : 'Production' )]
#[ QueryParam ( 'filter[image]' , 'string' , 'Filter servers by Docker image.' , required : false , example : 'ghcr.io/pterodactyl/yolks:java_23' )]
#[ QueryParam ( 'filter[external_id]' , 'string' , 'Filter servers by external identifier.' , required : false , example : 'billing-server-42' )]
#[ QueryParam ( 'search' , 'string' , 'Search term accepted by the list request. Must not exceed 100 characters.' , required : false , example : 'minecraft' )]
#[ QueryParam ( 'sort' , 'string' , 'Sort servers by id or uuid. Prefix with "-" for descending order.' , required : false , example : '-id' , enum : [ 'id' , '-id' , 'uuid' , '-uuid' ])]
#[ QueryParam ( 'include' , 'string' , 'Comma-separated relationships to include. Supports "allocations", "user", "subusers", "egg", "variables", "location", "node", "databases", and "transfer" when the key can read those resources.' , required : false , example : 'user,node,allocations' )]
#[ ResponseFromTransformer ( ServerTransformer :: class , Server :: class , collection : true , factoryStates : [ 'withRelationships' ], resourceKey : 'server' , paginate : [ IlluminatePaginatorAdapter :: class , 50 ])]
2018-01-19 21:47:06 -06:00
public function index ( GetServersRequest $request ) : array
2018-01-19 19:58:57 -06:00
{
2026-10-05 18:10:18 -04:00
$query = Server :: query () -> with ([ 'egg.variables' , 'serverVariables' , 'location' ]);
$search = $request -> validated ( 'search' );
if ( is_string ( $search ) && $search !== '' ) {
$query -> where ( function ( Builder $query ) use ( $search ) : void {
$query -> where ( 'uuid' , 'like' , "% { $search } %" )
-> orWhere ( 'uuidShort' , 'like' , "% { $search } %" )
-> orWhere ( 'name' , 'like' , "% { $search } %" )
-> orWhere ( 'description' , 'like' , "% { $search } %" )
-> orWhere ( 'external_id' , 'like' , "% { $search } %" );
});
}
$servers = QueryBuilder :: for ( $query )
2022-06-26 13:26:12 -04:00
-> allowedFilters ([ 'uuid' , 'uuidShort' , 'name' , 'description' , 'image' , 'external_id' ])
2020-09-13 12:21:44 -07:00
-> allowedSorts ([ 'id' , 'uuid' ])
2026-10-05 18:10:18 -04:00
-> paginate ( $request -> perPage ());
2018-01-19 19:58:57 -06:00
2026-10-05 18:10:18 -04:00
return Fractal :: collection ( $servers )
2018-01-19 21:47:06 -06:00
-> transformWith ( $this -> getTransformer ( ServerTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2018-01-19 21:47:06 -06:00
}
2018-01-28 17:14:14 -06:00
/**
* Create a new server on the system.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
* @throws ValidationException
* @throws DisplayException
* @throws NoViableAllocationException
* @throws NoViableNodeException
2018-01-28 17:14:14 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Create server' , 'Creates a new server using either explicit allocations or automatic deployment constraints.' )]
#[ ResponseFromTransformer ( ServerTransformer :: class , Server :: class , status : 201 , description : 'Server created.' , factoryStates : [ 'withRelationships' ], resourceKey : 'server' )]
#[ ScribeResponse ( self :: DEPLOYMENT_ERROR , status : 400 , description : 'Automatic deployment could not find a viable node or allocation.' )]
public function store ( StoreServerRequest $request , CreatesServers $creation ) : JsonResponse
2018-01-28 17:14:14 -06:00
{
2026-10-05 18:10:18 -04:00
$server = $creation -> create ( $request -> payload (), $request -> getDeploymentObject ());
2018-01-28 17:14:14 -06:00
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $server )
2018-01-28 17:14:14 -06:00
-> transformWith ( $this -> getTransformer ( ServerTransformer :: class ))
-> respond ( 201 );
}
2018-01-19 21:47:06 -06:00
/**
* Show a single server transformed for the application API.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2018-01-19 21:47:06 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Get server' , 'Returns a single server by internal numeric ID.' )]
#[ QueryParam ( 'include' , 'string' , 'Comma-separated relationships to include. Supports "allocations", "user", "subusers", "egg", "variables", "location", "node", "databases", and "transfer" when the key can read those resources.' , required : false , example : 'user,node,allocations' )]
#[ ResponseFromTransformer ( ServerTransformer :: class , Server :: class , factoryStates : [ 'withRelationships' ], resourceKey : 'server' )]
2022-05-22 14:10:01 -04:00
public function view ( GetServerRequest $request , Server $server ) : array
2018-01-19 21:47:06 -06:00
{
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $server )
2018-01-19 21:47:06 -06:00
-> transformWith ( $this -> getTransformer ( ServerTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2018-01-19 19:58:57 -06:00
}
2018-01-20 13:48:02 -06:00
/**
2022-10-14 10:59:20 -06:00
* Deletes a server.
*
2026-10-05 18:10:18 -04:00
* @throws DisplayException
2018-01-20 13:48:02 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Delete server' , 'Deletes a server. The force route bypasses recoverable Wings or database host deletion failures.' )]
#[ ScribeResponse ( status : 204 , description : 'Server deleted.' )]
public function delete ( ServerWriteRequest $request , DeletesServers $deletion , Server $server , string $force = '' ) : Response
2018-01-20 13:48:02 -06:00
{
2026-10-05 18:10:18 -04:00
$deletion -> withForce ( $force === 'force' ) -> delete ( $server );
2018-01-20 13:48:02 -06:00
return $this -> returnNoContent ();
}
2018-01-19 19:58:57 -06:00
}