2018-01-03 21:14:53 -06:00
<? 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\Locations ;
2018-01-03 21:14:53 -06:00
use Illuminate\Http\JsonResponse ;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Response ;
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\Locations\CreatesLocations ;
use Pterodactyl\Contracts\Locations\DeletesLocations ;
use Pterodactyl\Contracts\Locations\UpdatesLocations ;
use Pterodactyl\Exceptions\Service\Location\HasActiveNodesException ;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer ;
use Pterodactyl\Facades\Fractal ;
2018-01-27 12:38:56 -06:00
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Application\Locations\DeleteLocationRequest ;
2018-09-03 15:59:30 -07:00
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationRequest ;
2018-01-19 19:58:57 -06:00
use Pterodactyl\Http\Requests\Api\Application\Locations\GetLocationsRequest ;
2018-11-19 22:04:05 -07:00
use Pterodactyl\Http\Requests\Api\Application\Locations\StoreLocationRequest ;
2018-01-19 19:58:57 -06:00
use Pterodactyl\Http\Requests\Api\Application\Locations\UpdateLocationRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Location ;
use Pterodactyl\Transformers\Api\Application\LocationTransformer ;
use Spatie\QueryBuilder\QueryBuilder ;
2018-01-03 21:14:53 -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 ( 'Locations' , 'Create, update, retrieve, and delete physical or logical deployment locations.' )]
2018-01-27 12:38:56 -06:00
class LocationController extends ApplicationApiController
2018-01-03 21:14:53 -06:00
{
2026-10-05 18:10:18 -04:00
private const array ACTIVE_NODES_ERROR = [
'errors' => [
[
'code' => 'HasActiveNodesException' ,
'status' => '400' ,
'detail' => 'Cannot delete a location that has active nodes attached to it.' ,
],
],
];
2018-01-03 21:14:53 -06:00
/**
2022-10-14 10:59:20 -06:00
* Return all the locations currently registered on the Panel.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2018-01-03 21:14:53 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'List locations' , 'Returns a paginated list of all locations registered on the panel.' )]
#[ QueryParam ( 'per_page' , 'integer' , 'Number of locations to return per page, up to 100.' , required : false , example : 50 )]
#[ QueryParam ( 'filter[short]' , 'string' , 'Filter locations by short identifier.' , required : false , example : 'us-east' )]
#[ QueryParam ( 'filter[long]' , 'string' , 'Filter locations by description.' , required : false , example : 'US East Datacenter' )]
#[ QueryParam ( 'sort' , 'string' , 'Sort locations by id. Prefix with "-" for descending order.' , required : false , example : 'id' , enum : [ 'id' , '-id' ])]
#[ QueryParam ( 'include' , 'string' , 'Comma-separated relationships to include. Supports "nodes" and "servers" when the key can read those resources.' , required : false , example : 'nodes,servers' , enum : [ 'nodes' , 'servers' , 'nodes,servers' ])]
#[ ResponseFromTransformer ( LocationTransformer :: class , Location :: class , collection : true , resourceKey : 'location' , paginate : [ IlluminatePaginatorAdapter :: class , 50 ])]
2018-01-12 20:39:15 -06:00
public function index ( GetLocationsRequest $request ) : array
2018-01-03 21:14:53 -06:00
{
2020-09-13 11:59:52 -07:00
$locations = QueryBuilder :: for ( Location :: query ())
-> allowedFilters ([ 'short' , 'long' ])
-> allowedSorts ([ 'id' ])
2026-10-05 18:10:18 -04:00
-> paginate ( $request -> perPage ());
2018-01-03 21:14:53 -06:00
2026-10-05 18:10:18 -04:00
return Fractal :: collection ( $locations )
2018-01-27 12:38:56 -06:00
-> transformWith ( $this -> getTransformer ( LocationTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2018-01-03 21:14:53 -06:00
}
/**
* Return a single location.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2018-01-03 21:14:53 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Get location' , 'Returns a single location by internal numeric ID.' )]
#[ QueryParam ( 'include' , 'string' , 'Comma-separated relationships to include. Supports "nodes" and "servers" when the key can read those resources.' , required : false , example : 'nodes,servers' , enum : [ 'nodes' , 'servers' , 'nodes,servers' ])]
#[ ResponseFromTransformer ( LocationTransformer :: class , Location :: class , resourceKey : 'location' )]
2022-05-22 14:10:01 -04:00
public function view ( GetLocationRequest $request , Location $location ) : array
2018-01-03 21:14:53 -06:00
{
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $location )
2018-01-27 12:38:56 -06:00
-> transformWith ( $this -> getTransformer ( LocationTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2018-01-03 21:14:53 -06:00
}
/**
2022-10-14 10:59:20 -06:00
* Store a new location on the Panel and return an HTTP/201 response code with the
2018-01-12 20:39:15 -06:00
* new location attached.
2018-01-03 21:14:53 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Create location' , 'Creates a new location and returns the created resource.' )]
#[ ResponseFromTransformer ( LocationTransformer :: class , Location :: class , status : 201 , description : 'Location created.' , resourceKey : 'location' , meta : [ 'resource' => 'https://panel.example.test/api/application/locations/1' ])]
public function store ( StoreLocationRequest $request , CreatesLocations $locations ) : JsonResponse
2018-01-03 21:14:53 -06:00
{
2026-10-05 18:10:18 -04:00
$location = $locations -> create ( $request -> payload ());
2018-01-03 21:14:53 -06:00
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $location )
2018-01-27 12:38:56 -06:00
-> transformWith ( $this -> getTransformer ( LocationTransformer :: class ))
-> addMeta ([
'resource' => route ( 'api.application.locations.view' , [
'location' => $location -> id ,
]),
])
2018-01-03 21:14:53 -06:00
-> respond ( 201 );
}
/**
2018-01-12 20:39:15 -06:00
* Update a location on the Panel and return the updated record to the user.
*
2026-10-05 18:10:18 -04:00
* @return ApiPayload
2018-01-03 21:14:53 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Update location' , 'Updates an existing location by internal numeric ID.' )]
#[ ResponseFromTransformer ( LocationTransformer :: class , Location :: class , resourceKey : 'location' )]
public function update ( UpdateLocationRequest $request , UpdatesLocations $locations , Location $location ) : array
2018-01-03 21:14:53 -06:00
{
2026-10-05 18:10:18 -04:00
$location = $locations -> update ( $location , $request -> payload ());
2018-01-03 21:14:53 -06:00
2026-10-05 18:10:18 -04:00
return Fractal :: item ( $location )
2018-01-27 12:38:56 -06:00
-> transformWith ( $this -> getTransformer ( LocationTransformer :: class ))
2026-10-05 18:10:18 -04:00
-> toResponseArray ();
2018-01-03 21:14:53 -06:00
}
/**
2018-01-12 20:39:15 -06:00
* Delete a location from the Panel.
*
2026-10-05 18:10:18 -04:00
* @throws HasActiveNodesException
2018-01-03 21:14:53 -06:00
*/
2026-10-05 18:10:18 -04:00
#[ Endpoint ( 'Delete location' , 'Deletes a location by internal numeric ID.' )]
#[ ScribeResponse ( status : 204 , description : 'Location deleted.' )]
#[ ScribeResponse ( self :: ACTIVE_NODES_ERROR , status : 400 , description : 'The location has active nodes attached.' )]
public function delete ( DeleteLocationRequest $request , DeletesLocations $locations , Location $location ) : Response
2018-01-03 21:14:53 -06:00
{
2026-10-05 18:10:18 -04:00
$locations -> delete ( $location );
2018-01-03 21:14:53 -06:00
return response ( '' , 204 );
}
}