Add endpoint to get all nodes meeting memory & disk requirements for a server; closes #1012

This commit is contained in:
Dane Everitt 2021-01-10 13:08:43 -08:00
parent ef3f8586c5
commit ff21d83e2d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
5 changed files with 87 additions and 5 deletions

View file

@ -0,0 +1,51 @@
<?php
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
use Pterodactyl\Http\Requests\Api\Application\Nodes\GetDeployableNodesRequest;
class NodeDeploymentController extends ApplicationApiController
{
/**
* @var \Pterodactyl\Services\Deployment\FindViableNodesService
*/
private $viableNodesService;
/**
* NodeDeploymentController constructor.
*
* @param \Pterodactyl\Services\Deployment\FindViableNodesService $viableNodesService
*/
public function __construct(FindViableNodesService $viableNodesService)
{
parent::__construct();
$this->viableNodesService = $viableNodesService;
}
/**
* 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.
*
* @param \Pterodactyl\Http\Requests\Api\Application\Nodes\GetDeployableNodesRequest $request
* @return array
*
* @throws \Pterodactyl\Exceptions\Service\Deployment\NoViableNodeException
*/
public function __invoke(GetDeployableNodesRequest $request): array
{
$data = $request->validated();
$nodes = $this->viableNodesService->setLocations($data['location_ids'] ?? [])
->setMemory($data['memory'])
->setDisk($data['disk'])
->handle($request->input('page') ?? 0);
return $this->fractal->collection($nodes)
->transformWith($this->getTransformer(NodeTransformer::class))
->toArray();
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Application\Nodes;
class GetDeployableNodesRequest extends GetNodesRequest
{
/**
* @return string[]
*/
public function rules(): array
{
return [
'page' => 'integer',
'memory' => 'required|integer|min:0',
'disk' => 'required|integer|min:0',
'location_ids' => 'array',
'location_ids.*' => 'integer',
];
}
}