2017-12-17 20:57:05 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 01:58:57 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
|
|
|
use Pterodactyl\Models\Node;
|
2018-01-01 21:11:44 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-09-13 19:21:44 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2018-01-01 21:11:44 +00:00
|
|
|
use Pterodactyl\Services\Nodes\NodeUpdateService;
|
|
|
|
use Pterodactyl\Services\Nodes\NodeCreationService;
|
|
|
|
use Pterodactyl\Services\Nodes\NodeDeletionService;
|
2017-12-17 20:57:05 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
|
2018-01-20 01:58:57 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest;
|
2021-01-05 21:52:49 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2018-01-20 01:58:57 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\DeleteNodeRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Nodes\UpdateNodeRequest;
|
2018-01-27 18:38:56 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-27 18:38:56 +00:00
|
|
|
class NodeController extends ApplicationApiController
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2018-01-01 21:11:44 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeCreationService
|
|
|
|
*/
|
|
|
|
private $creationService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeDeletionService
|
|
|
|
*/
|
|
|
|
private $deletionService;
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2018-01-01 21:11:44 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Nodes\NodeUpdateService
|
|
|
|
*/
|
|
|
|
private $updateService;
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
/**
|
|
|
|
* NodeController constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Nodes\NodeCreationService $creationService
|
|
|
|
* @param \Pterodactyl\Services\Nodes\NodeDeletionService $deletionService
|
|
|
|
* @param \Pterodactyl\Services\Nodes\NodeUpdateService $updateService
|
2017-12-17 20:57:05 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
|
|
|
|
*/
|
2018-01-01 21:11:44 +00:00
|
|
|
public function __construct(
|
|
|
|
NodeCreationService $creationService,
|
|
|
|
NodeDeletionService $deletionService,
|
|
|
|
NodeUpdateService $updateService,
|
|
|
|
NodeRepositoryInterface $repository
|
|
|
|
) {
|
2018-01-27 18:38:56 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2017-12-17 20:57:05 +00:00
|
|
|
$this->repository = $repository;
|
2018-01-01 21:11:44 +00:00
|
|
|
$this->creationService = $creationService;
|
|
|
|
$this->deletionService = $deletionService;
|
|
|
|
$this->updateService = $updateService;
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the nodes currently available on the Panel.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodesRequest $request
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2017-12-17 20:57:05 +00:00
|
|
|
* @return array
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
2018-01-13 20:08:19 +00:00
|
|
|
public function index(GetNodesRequest $request): array
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2021-01-05 21:52:49 +00:00
|
|
|
$perPage = $request->query('per_page', 10);
|
|
|
|
if ($perPage < 1) {
|
|
|
|
$perPage = 10;
|
|
|
|
} else if ($perPage > 100) {
|
|
|
|
throw new BadRequestHttpException('"per_page" query parameter must be below 100.');
|
|
|
|
}
|
|
|
|
|
2020-09-13 19:21:44 +00:00
|
|
|
$nodes = QueryBuilder::for(Node::query())
|
|
|
|
->allowedFilters(['uuid', 'name', 'fqdn', 'daemon_token_id'])
|
|
|
|
->allowedSorts(['id', 'uuid', 'memory', 'disk'])
|
2021-01-05 21:52:49 +00:00
|
|
|
->paginate($perPage);
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2018-01-13 20:08:19 +00:00
|
|
|
return $this->fractal->collection($nodes)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(NodeTransformer::class))
|
2018-01-13 20:08:19 +00:00
|
|
|
->toArray();
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return data for a single instance of a node.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\GetNodeRequest $request
|
2020-09-13 19:21:44 +00:00
|
|
|
* @param \Pterodactyl\Models\Node $node
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2017-12-17 20:57:05 +00:00
|
|
|
* @return array
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2017-12-17 20:57:05 +00:00
|
|
|
*/
|
2020-09-13 19:21:44 +00:00
|
|
|
public function view(GetNodeRequest $request, Node $node): array
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2020-09-13 19:21:44 +00:00
|
|
|
return $this->fractal->item($node)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(NodeTransformer::class))
|
2018-01-13 20:08:19 +00:00
|
|
|
->toArray();
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
2018-01-01 21:11:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new node on the Panel. Returns the created node and a HTTP/201
|
|
|
|
* status response on success.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\StoreNodeRequest $request
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2018-01-01 21:11:44 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
2021-01-01 22:55:30 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException*@throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-01-01 21:11:44 +00:00
|
|
|
*/
|
2018-01-13 20:08:19 +00:00
|
|
|
public function store(StoreNodeRequest $request): JsonResponse
|
2018-01-01 21:11:44 +00:00
|
|
|
{
|
2018-01-13 20:08:19 +00:00
|
|
|
$node = $this->creationService->handle($request->validated());
|
2018-01-01 21:11:44 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($node)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(NodeTransformer::class))
|
2018-01-01 21:11:44 +00:00
|
|
|
->addMeta([
|
2018-01-27 18:38:56 +00:00
|
|
|
'resource' => route('api.application.nodes.view', [
|
|
|
|
'node' => $node->id,
|
|
|
|
]),
|
2018-01-01 21:11:44 +00:00
|
|
|
])
|
|
|
|
->respond(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an existing node on the Panel.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\UpdateNodeRequest $request
|
2020-09-13 19:21:44 +00:00
|
|
|
* @param \Pterodactyl\Models\Node $node
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2018-01-01 21:11:44 +00:00
|
|
|
* @return array
|
|
|
|
*
|
2020-09-13 19:21:44 +00:00
|
|
|
* @throws \Throwable
|
2018-01-01 21:11:44 +00:00
|
|
|
*/
|
2020-09-13 19:21:44 +00:00
|
|
|
public function update(UpdateNodeRequest $request, Node $node): array
|
2018-01-01 21:11:44 +00:00
|
|
|
{
|
2018-09-03 21:54:50 +00:00
|
|
|
$node = $this->updateService->handle(
|
2020-09-13 19:21:44 +00:00
|
|
|
$node, $request->validated(), $request->input('reset_secret') === true
|
2018-01-27 18:38:56 +00:00
|
|
|
);
|
2018-01-01 21:11:44 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($node)
|
2018-01-27 18:38:56 +00:00
|
|
|
->transformWith($this->getTransformer(NodeTransformer::class))
|
2018-01-01 21:11:44 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a given node from the Panel as long as there are no servers
|
|
|
|
* currently attached to it.
|
|
|
|
*
|
2018-01-20 01:58:57 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\DeleteNodeRequest $request
|
2020-09-13 19:21:44 +00:00
|
|
|
* @param \Pterodactyl\Models\Node $node
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2020-09-13 19:21:44 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-01-01 21:11:44 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
|
|
|
|
*/
|
2020-09-13 19:21:44 +00:00
|
|
|
public function delete(DeleteNodeRequest $request, Node $node): JsonResponse
|
2018-01-01 21:11:44 +00:00
|
|
|
{
|
2020-09-13 19:21:44 +00:00
|
|
|
$this->deletionService->handle($node);
|
2018-01-01 21:11:44 +00:00
|
|
|
|
2020-09-13 19:21:44 +00:00
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
2018-01-01 21:11:44 +00:00
|
|
|
}
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|