2018-01-20 01:58:57 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
|
2018-01-20 01:58:57 +00:00
|
|
|
|
2018-01-20 19:48:02 +00:00
|
|
|
use Illuminate\Http\Response;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-09-13 19:21:44 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Services\Servers\ServerCreationService;
|
2018-01-20 19:48:02 +00:00
|
|
|
use Pterodactyl\Services\Servers\ServerDeletionService;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
|
2021-03-23 21:57:29 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
2018-02-24 18:15:21 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServerRequest;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\GetServersRequest;
|
2018-01-20 19:48:02 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\ServerWriteRequest;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\StoreServerRequest;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
2018-01-20 01:58:57 +00:00
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
class ServerController extends ApplicationApiController
|
2018-01-20 01:58:57 +00:00
|
|
|
{
|
2021-03-05 15:46:14 +00:00
|
|
|
private ServerCreationService $creationService;
|
|
|
|
private ServerDeletionService $deletionService;
|
2021-01-05 21:52:49 +00:00
|
|
|
|
2018-01-20 01:58:57 +00:00
|
|
|
/**
|
|
|
|
* ServerController constructor.
|
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
public function __construct(
|
|
|
|
ServerCreationService $creationService,
|
2021-01-13 16:43:57 +00:00
|
|
|
ServerDeletionService $deletionService
|
2021-01-05 21:52:49 +00:00
|
|
|
) {
|
2018-01-20 19:48:02 +00:00
|
|
|
parent::__construct();
|
2018-01-20 03:47:06 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->creationService = $creationService;
|
2018-01-20 19:48:02 +00:00
|
|
|
$this->deletionService = $deletionService;
|
2018-01-20 01:58:57 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
/**
|
|
|
|
* Return all of the servers that currently exist on the Panel.
|
2021-03-05 15:46:14 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-01-20 03:47:06 +00:00
|
|
|
*/
|
|
|
|
public function index(GetServersRequest $request): array
|
2018-01-20 01:58:57 +00:00
|
|
|
{
|
2021-01-05 21:52:49 +00:00
|
|
|
$perPage = $request->query('per_page', 10);
|
2021-03-23 21:57:29 +00:00
|
|
|
if ($perPage < 1 || $perPage > 100) {
|
|
|
|
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
|
2021-01-05 21:52:49 +00:00
|
|
|
}
|
|
|
|
|
2020-09-13 19:21:44 +00:00
|
|
|
$servers = QueryBuilder::for(Server::query())
|
2021-06-05 15:43:57 +00:00
|
|
|
->allowedFilters(['uuid', 'uuidShort', 'name', 'image', 'external_id'])
|
2021-05-20 22:00:46 +00:00
|
|
|
->allowedSorts(['id', 'uuid', 'uuidShort', 'name', 'owner_id', 'node_id', 'status'])
|
2021-01-05 21:52:49 +00:00
|
|
|
->paginate($perPage);
|
2018-01-20 01:58:57 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($servers)
|
2018-01-20 03:47:06 +00:00
|
|
|
->transformWith($this->getTransformer(ServerTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
/**
|
|
|
|
* Create a new server on the system.
|
|
|
|
*
|
2019-11-16 21:33:01 +00:00
|
|
|
* @throws \Throwable
|
2018-01-28 23:14:14 +00:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableAllocationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
|
|
|
|
*/
|
|
|
|
public function store(StoreServerRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
$server = $this->creationService->handle($request->validated(), $request->getDeploymentObject());
|
|
|
|
|
|
|
|
return $this->fractal->item($server)
|
|
|
|
->transformWith($this->getTransformer(ServerTransformer::class))
|
2021-05-19 02:53:42 +00:00
|
|
|
->respond(Response::HTTP_CREATED);
|
2018-01-28 23:14:14 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
/**
|
|
|
|
* Show a single server transformed for the application API.
|
2021-03-05 15:46:14 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-01-20 03:47:06 +00:00
|
|
|
*/
|
2021-01-01 22:55:30 +00:00
|
|
|
public function view(GetServerRequest $request, Server $server): array
|
2018-01-20 03:47:06 +00:00
|
|
|
{
|
2021-01-01 22:55:30 +00:00
|
|
|
return $this->fractal->item($server)
|
2018-01-20 03:47:06 +00:00
|
|
|
->transformWith($this->getTransformer(ServerTransformer::class))
|
2018-01-20 01:58:57 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
2018-01-20 19:48:02 +00:00
|
|
|
|
|
|
|
/**
|
2021-03-05 15:46:14 +00:00
|
|
|
* Deletes a server.
|
|
|
|
*
|
2018-01-20 19:48:02 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2021-03-05 15:46:14 +00:00
|
|
|
* @throws \Throwable
|
2018-01-20 19:48:02 +00:00
|
|
|
*/
|
|
|
|
public function delete(ServerWriteRequest $request, Server $server, string $force = ''): Response
|
|
|
|
{
|
|
|
|
$this->deletionService->withForce($force === 'force')->handle($server);
|
|
|
|
|
|
|
|
return $this->returnNoContent();
|
|
|
|
}
|
2018-01-20 01:58:57 +00:00
|
|
|
}
|